📚
GIMPのPythonスクリプトで画像生成:初心者向け完全ガイド
はじめに
GIMPは強力な画像編集ソフトウェアですが、Pythonスクリプトを使用することで、その機能をさらに拡張できます。本記事では、GIMPのPythonスクリプトを使って、カスタムメッセージを含む画像を自動生成する方法を解説します。
このガイドは、プログラミング初心者でも理解できるよう、ステップバイステップで説明していきます。
デモ動画
GIMPとPythonスクリプトの基礎
GIMPでPythonスクリプトを使用すると、画像処理を自動化できます。以下は、GIMPとPythonスクリプトの関係を示す簡単な図です。
- GIMP: 画像編集ソフトウェア
 - Python-Fu: GIMPとPythonを繋ぐインターフェース
 - Pythonスクリプト: 自動化のための命令セット
 - 自動化された画像処理: スクリプトによる画像操作
 - 生成された画像: 処理結果の出力
 
スクリプトの全体構造
まず、スクリプト全体の構造を理解しましょう。
- ライブラリのインポート: 必要な機能を読み込みます。
 - main関数の定義: 主要な処理を行う関数を定義します。
 - キャンバス作成: 新しい画像を作成します。
 - テキスト追加: メッセージをキャンバスに追加します。
 - 画像保存: 生成した画像を保存します。
 - スクリプト登録: 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)
このコードは以下のことを行っています:
- キャンバスのサイズ(幅1000px、高さ600px)を設定
 - 背景色を白に設定
 - 新しい画像を作成
 - 背景レイヤーを追加
 - 背景を白で塗りつぶす
 
テキストの追加
次に、キャンバスにテキストを追加します。
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
)
このコードの流れは以下のとおりです:
- メッセージ、フォントサイズ、色を設定
 - テキストレイヤーを作成
 - テキストを中央揃えに設定
 - テキストをキャンバスの中央に配置
 
画像の保存
生成した画像を保存します。
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)
保存プロセスは以下のとおりです:
- 保存先ディレクトリとファイル名を設定
 - レイヤーを結合
 - PNG形式で保存
 - 保存場所をメッセージで表示
 
スクリプトの登録と実行
最後に、スクリプトを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()
この部分で以下のことを行っています:
- スクリプトの名前と説明を設定
 - 著作権情報を追加
 - GIMPのメニューにスクリプトを登録
 - スクリプトのメイン関数を指定
 
全体コード
#!/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で以下の操作を自動化できます:
- 新しいキャンバスの作成
 - カスタムメッセージの追加
 - 画像の保存
 
初心者の方でも、このガイドを参考にしながらスクリプトを理解し、自分のニーズに合わせてカスタマイズすることができます。GIMPとPythonの組み合わせは、画像処理の可能性を大きく広げてくれます。
スクリプトを修正して、異なるサイズ、色、フォント、メッセージを試してみてください。GIMPのPythonスクリプティングの世界を探索し、あなたの創造性を解き放ちましょう!
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
Discussion