Log in

Previous Entry | Next Entry

GIMP-Python script for dithering pixel art

Here's a Python script for GIMP that takes an indexed color image and replaces every other color with a checkerboard dithering of its neighbors. The intended use was to create grayscale 4 color images with this style of dithering, by first creating a hand-tuned 7 color image, then applying the script. After it runs, you can delete the in-between tones from the palette. It should work for any odd number of colors, n>=3.

Having scripted Gimp in both Python and Scheme, I definitely found Python more pleasant.

This is the kind of silly thing I used to do, to pass the time:

#!/usr/bin/env python

# Pixel art dither script for GIMP
# Author: ahefner@gmail.com

import math
from gimpfu import *
from array import array

def python_8bit_dither(img, srclayer):


        layer = srclayer.copy()
        img.add_layer(layer, 0) 
        width = layer.width
        height = layer.height

        rgn = layer.get_pixel_rgn(0, 0, width, height, TRUE, FALSE)
        src_pixels = array("B", rgn[0:width, 0:height])

#        cmbytes, cmdata = pdb.gimp_image_get_colormap(img)
#        colors = cmbytes / 3
#        pdb.gimp_message(str(cmdata))

        i = 0
        ph = 0
        for y in range(height):
            ph = y & 1
            for x in range(width):
                c = src_pixels[i]
                if (c & 1):
                    c = c-1 if (ph == 0) else c+1
                src_pixels[i] = c
                ph = ph ^ 1
                i = i + 1

        rgn[0:width, 0:height] = src_pixels.tostring()

#        layer.merge_shadow()

    except Exception, err:
        pdb.gimp_message("ERR: " + str(err))

        "Dither odd colors in an indexed-color image",
        "Dither odd colors in an indexed-color image",
        "Andy Hefner",
        "Andy Hefner",
        "/Filters/Mine/8-Bit Dither Helper",



Tyler Barnes
Jun. 13th, 2013 09:18 pm (UTC)
I'm not really a GIMP user I use PS, but have found no other type of scripts like this one that do what I need them to do. What file extension do I save this file as, where should I save/install it, and how do I run it in GIMP? Thank you for this script and thank you for your time.
Sep. 3rd, 2013 01:18 am (UTC)
Here's a link to an updated version, as the old one doesn't work on slightly newer GIMP versions for some reason: https://raw.github.com/ahefner/asm6502/master/hacks/dither1.py

Just save it as dither1.py to your Gimp plug-ins folder. On my machine that's /home/hefner/.gimp-2.6/plug-ins/ but that depends on your OS. When you restart Gimp it should appear in the Filters menu under a folder named "Hefner". It will only be enabled for Indexed-Color images.

Obviously I've only tried this with GIMP 2.6, which is still out of date. Hopefully newer versions don't somehow break it again.