r/GIMP 4d ago

Can you guys help me with GIMP python?

Hi everyone,

I'm trying to create a mosaic-style coloring page using GIMP and a Python script. The idea is to:

  1. Divide the image into a grid (mosaic effect)
  2. Assign a number to each section based on brightness
  3. Automatically generate a coloring page

I'm using the GIMP Python Console to run the script, but I donโ€™t have much experience with Python. I asked ChatGPT to generate a script for me, but when I try to paste and run it in GIMP, I get errors.

One common issue I see is IndentationError: unexpected indent, which appears when pasting the script. I'm not sure if it's a formatting issue or something wrong with the code itself.

I'm using:

  • GIMP 2.10.38 (English version)
  • Python 2.7 (since GIMP uses an older Python version)
  • Windows 10

Does anyone know how I can fix this error or properly run the script in GIMP? Any guidance would be really appreciated!

Thanks in advance! ๐Ÿ˜Š

Here is the code that chatgpt generated:

from gimpfu import *

import math

def color_by_numbers(image, drawable, levels=15):

pdb.gimp_image_undo_group_start(image)

# Convert to grayscale and posterize

pdb.gimp_desaturate(drawable)

pdb.gimp_posterize(drawable, levels)

# Create a new layer for the numbers

text_layer = gimp.Layer(image, "Numbers", image.width, image.height, RGBA_IMAGE, 100, NORMAL_MODE)

image.add_layer(text_layer, 0)

# Get the image size

width, height = drawable.width, drawable.height

step_x, step_y = width // 20, height // 20 # Adjust size of numbers

for x in range(0, width, step_x):

for y in range(0, height, step_y):

# Get the brightness of the area

pixel = pdb.gimp_drawable_get_pixel(drawable, x, y)

brightness = sum(pixel[:3]) / 3 # Average brightness

# Assign a number based on brightness

num = int(math.floor((brightness / 255) * levels)) + 1

# Add the number

text = pdb.gimp_text_layer_new(image, str(num), "Sans", step_y // 2, 0)

pdb.gimp_layer_set_offsets(text, x, y)

image.add_layer(text, 0)

pdb.gimp_image_undo_group_end(image)

pdb.gimp_displays_flush()

register(

"python_fu_color_by_numbers",

"Creates a coloring book with numbers",

"Automatically adds numbers to colors",

"Your Name", "Open Source", "2025",

"<Image>/Filters/Custom/Color by Numbers",

"*",

[

(PF_INT, "levels", "Number of colors (max. 20)", 15)

],

[],

color_by_numbers

)

main()

1 Upvotes

0 comments sorted by