#!/usr/bin/env python

from gimpfu import *

def checker_pattern(image, drawable, darkness=30):
    """
    Apply a checker pattern effect by darkening every second pixel
    Args:
        image: The input image
        drawable: The input drawable
        darkness: How much to darken the pixels (percentage)
    """
    # Make sure the image is visible
    pdb.gimp_image_undo_group_start(image)

    # Get the image's dimensions
    width = drawable.width
    height = drawable.height

    # Convert darkness percentage to factor (0-1)
    darkness_factor = darkness / 100.0

    # Get the current selection or the entire image if no selection
    has_selection, x1, y1, x2, y2 = pdb.gimp_selection_bounds(image)
    if not has_selection:
        x1, y1 = 0, 0
        x2, y2 = width, height

    # Create pixel regions for reading and writing
    src_rgn = drawable.get_pixel_rgn(x1, y1, x2-x1, y2-y1, False, False)
    dst_rgn = drawable.get_pixel_rgn(x1, y1, x2-x1, y2-y1, True, True)

    # Process the image row by row
    bpp = drawable.bpp  # bytes per pixel

    for y in range(y1, y2):
        row = src_rgn[x1:x2, y:y+1]  # Get one row of pixels
        pixel_data = bytearray(row)

        for x in range(x2-x1):
            # Determine if this pixel should be darkened
            if (x + y) % 2 == 0:  # Creates checker pattern
                pos = x * bpp
                # Modify each color component
                for b in range(bpp):
                    if b < 3:  # Don't modify alpha channel if it exists
                        pixel_data[pos + b] = int(pixel_data[pos + b] * (1 - darkness_factor))

        # Update the row in the destination region
        dst_rgn[x1:x2, y:y+1] = bytes(pixel_data)

    # Update the drawable
    drawable.flush()
    drawable.merge_shadow(True)
    drawable.update(x1, y1, x2-x1, y2-y1)

    # End the undo group
    pdb.gimp_image_undo_group_end(image)

register(
    "python-fu-checker-pattern",
    "Apply checker pattern effect",
    "Darkens every second pixel to create a checker pattern effect",
    "Assistant",
    "Assistant",
    "2024",
    "<Image>/Filters/Pattern/Checker Pattern...",
    "*",
    [
        (PF_SLIDER, "darkness", "Darkness (%)", 30, (0, 100, 1))
    ],
    [],
    checker_pattern
)

main()
Edit Report
Pub: 29 Oct 2024 00:25 UTC
Views: 17