3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GIMPのPythonスクリプトで画像生成:初心者向け完全ガイド

Last updated at Posted at 2024-07-19

はじめに

GIMPは強力な画像編集ソフトウェアですが、Pythonスクリプトを使用することで、その機能をさらに拡張できます。本記事では、GIMPのPythonスクリプトを使って、カスタムメッセージを含む画像を自動生成する方法を解説します。

このガイドは、プログラミング初心者でも理解できるよう、ステップバイステップで説明していきます。

デモ動画

GIMPとPythonスクリプトの基礎

GIMPでPythonスクリプトを使用すると、画像処理を自動化できます。以下は、GIMPとPythonスクリプトの関係を示す簡単な図です。

  • GIMP: 画像編集ソフトウェア
  • Python-Fu: GIMPとPythonを繋ぐインターフェース
  • Pythonスクリプト: 自動化のための命令セット
  • 自動化された画像処理: スクリプトによる画像操作
  • 生成された画像: 処理結果の出力

スクリプトの全体構造

まず、スクリプト全体の構造を理解しましょう。

  1. ライブラリのインポート: 必要な機能を読み込みます。
  2. main関数の定義: 主要な処理を行う関数を定義します。
  3. キャンバス作成: 新しい画像を作成します。
  4. テキスト追加: メッセージをキャンバスに追加します。
  5. 画像保存: 生成した画像を保存します。
  6. スクリプト登録: GIMPのメニューにスクリプトを登録します。

それでは、各部分を詳しく見ていきましょう。

キャンバスの作成

まず、新しいキャンバスを作成します。

width = 1000
height = 600
bg_color = (255, 255, 255)  # 白色

image = gimp.Image(width, height, RGB)
background = gimp.Layer(image, "Background", width, height, RGB_IMAGE, 100, NORMAL_MODE)
image.add_layer(background, 0)

gimp.set_background(bg_color)
background.fill(BACKGROUND_FILL)

このコードは以下のことを行っています:

  1. キャンバスのサイズ(幅1000px、高さ600px)を設定
  2. 背景色を白に設定
  3. 新しい画像を作成
  4. 背景レイヤーを追加
  5. 背景を白で塗りつぶす

テキストの追加

次に、キャンバスにテキストを追加します。

message = "!! Hello from GIMP Python !!"
font_size = 50
font_color = (255, 0, 0)  # 赤色

gimp.set_foreground(font_color)

text_layer = pdb.gimp_text_layer_new(image, message, "BIZ UDMincho Medium", font_size, 0)
image.add_layer(text_layer, 0)

pdb.gimp_text_layer_set_justification(text_layer, 2)  # CENTER_JUSTIFY
text_layer.set_offsets(
    (width - text_layer.width) // 2,
    (height - text_layer.height) // 2
)

このコードの流れは以下のとおりです:

  1. メッセージ、フォントサイズ、色を設定
  2. テキストレイヤーを作成
  3. テキストを中央揃えに設定
  4. テキストをキャンバスの中央に配置

画像の保存

生成した画像を保存します。

output_dir = gimp.directory
filename = "generated_message0.png"
fullpath = os.path.join(output_dir, filename)

merged_layer = pdb.gimp_image_merge_visible_layers(image, CLIP_TO_IMAGE)

pdb.file_png_save(image, merged_layer, fullpath, filename, 0, 9, 0, 0, 0, 0, 0)

gimp.message("Image saved to: " + fullpath)

保存プロセスは以下のとおりです:

  1. 保存先ディレクトリとファイル名を設定
  2. レイヤーを結合
  3. PNG形式で保存
  4. 保存場所をメッセージで表示

スクリプトの登録と実行

最後に、スクリプトをGIMPに登録します。

register(
    "python_fu_create_canvas_with_message0",
    "Create new canvas with message and save",
    "Creates a new canvas, adds a predefined message to it, and saves the image",
    "am(author)",
    "am(copyright)",
    "2024/07/18",
    "Canvas with Message (Save)",
    "",
    [],
    [],
    plugin_main,
    menu="<Image>/File/Create/"
)

main()

この部分で以下のことを行っています:

  1. スクリプトの名前と説明を設定
  2. 著作権情報を追加
  3. GIMPのメニューにスクリプトを登録
  4. スクリプトのメイン関数を指定

全体コード

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from gimpfu import *
import os

def plugin_main():
    # キャンバスのサイズと色を定義
    width = 1000
    height = 600
    bg_color = (255, 255, 255)  # 白色

    # スクリプト内で定義されたメッセージ
    message = "!! Hello from GIMP Python !!"
    font_size = 50
    font_color = (255, 0, 0)  # 赤色

    # 新しい画像を作成
    image = gimp.Image(width, height, RGB)
    
    # 背景レイヤーを追加
    background = gimp.Layer(image, "Background", width, height, RGB_IMAGE, 100, NORMAL_MODE)
    image.add_layer(background, 0)
    
    # 背景色で塗りつぶす
    gimp.set_background(bg_color)
    background.fill(BACKGROUND_FILL)

    # 現在のフォアグラウンドカラーを保存
    old_fg = gimp.get_foreground()
    
    # フォアグラウンドカラーを設定
    gimp.set_foreground(font_color)
    
    # 新しいテキストレイヤーを作成
    text_layer = pdb.gimp_text_layer_new(image, message, "BIZ UDMincho Medium", font_size, 0)
    
    # テキストレイヤーを画像に追加
    image.add_layer(text_layer, 0)
    
    # テキストレイヤーを画像の中央に配置
    pdb.gimp_text_layer_set_justification(text_layer, 2)  # CENTER_JUSTIFY
    text_layer.set_offsets(
        (width - text_layer.width) // 2,
        (height - text_layer.height) // 2
    )
    
    # フォアグラウンドカラーを元に戻す
    gimp.set_foreground(old_fg)
    
    # 画像を保存
    output_dir = gimp.directory  # GIMPのデフォルトディレクトリ
    filename = "generated_message0.png"
    fullpath = os.path.join(output_dir, filename)
    
    # レイヤーを結合
    merged_layer = pdb.gimp_image_merge_visible_layers(image, CLIP_TO_IMAGE)
    
    # 画像を保存
    pdb.file_png_save(image, merged_layer, fullpath, filename, 0, 9, 0, 0, 0, 0, 0)
    
    # 保存場所をコンソールに出力
    gimp.message("Image saved to: " + fullpath)
    
    # 新しい画像を表示(CLIモードでは表示されません)
    gimp.Display(image)
    
    # 画像を更新
    gimp.displays_flush()

register(
    "python_fu_create_canvas_with_message0",
    "Create new canvas with message and save",
    "Creates a new canvas, adds a predefined message to it, and saves the image",
    "am(author)",
    "am(copyright)",
    "2024/07/18",
    "Canvas with Message (Save)",
    "",
    [],
    [],
    plugin_main,
    menu="<Image>/File/Create/"
)

main()

まとめ

このPythonスクリプトを使用することで、GIMPで以下の操作を自動化できます:

  1. 新しいキャンバスの作成
  2. カスタムメッセージの追加
  3. 画像の保存

初心者の方でも、このガイドを参考にしながらスクリプトを理解し、自分のニーズに合わせてカスタマイズすることができます。GIMPとPythonの組み合わせは、画像処理の可能性を大きく広げてくれます。

スクリプトを修正して、異なるサイズ、色、フォント、メッセージを試してみてください。GIMPのPythonスクリプティングの世界を探索し、あなたの創造性を解き放ちましょう!

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?