Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

python-fuで画像を弄る その2:画像の自動生成

実際のところ

from gimpfu import *

def create_image_with_text(filename, text):
    # Create a new image
    image = gimp.Image(320, 320, RGB)

    # Create a new layer
    layer = gimp.Layer(image, "Background", 320, 320, RGB_IMAGE, 100, NORMAL_MODE)

    # Add the layer to the image
    image.add_layer(layer, 0)

    # Set the gradient to "Full saturation spectrum CCW"
    pdb.gimp_context_set_gradient("Full saturation spectrum CCW")

    # Apply the gradient to the layer
    pdb.gimp_drawable_edit_gradient_fill(layer, 0, 0, 1, 1, 0, 0)

    # Create a new text layer
    text_layer = pdb.gimp_text_fontname(image, None, 0, 0, text, 10, True, 32, PIXELS, "Sans")

    # Add the text layer to the image
    image.add_layer(text_layer, 0)

    # Position the text layer in the center of the image
    text_layer.set_offsets((320 - text_layer.width) // 2, (320 - text_layer.height) // 2)

    # Save the image
    pdb.gimp_file_save(image, text_layer, filename, filename)

    # Delete the image
    gimp.delete(image)

# Call the function
create_image_with_text("Hello_World.png", "Hello, World")
create_image_with_text("Konichiwa_Sekai.png", "こんにちは世界")
create_image_with_text("Script_fu_wa_yoi_bunmei.png", "Script-fuは良い文明")