Skip to content

Commit

Permalink
Advanced dithering example
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Nov 5, 2020
1 parent 225d7b1 commit 47b2e76
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
61 changes: 61 additions & 0 deletions examples/7color/advanced/dither.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python3

print("""dither.py
Advanced dithering example using Hitherdither by Henrik Blidh:
https://github.com/hbldh/hitherdither
You should read the code, read the README ^ and edit accordingly.
Many of these dithering processes are slow, you might want to pre-prepare an image!
You will need to install hitherdither from GitHub:
sudo python3 -m pip install git+https://www.github.com/hbldh/hitherdither
usage: ./dither.py <image_file> <saturation>
- Image file should be RGB and can be a jpg, PNG or otherwise.
- Saturation should be from 0.0 to 1.0 and changes the palette that's used to dither the image.
a higher saturation will generally result in a more saturated end image due to how colours are mixed.
""")

import sys

from PIL import Image
from inky.inky_uc8159 import Inky
import hitherdither

inky = Inky()
saturation = 0.5 # Saturation of palette
thresholds = [64, 64, 64] # Threshold for snapping colours, I guess?

if len(sys.argv) == 1:
print("""
Usage: {file} image-file
""".format(file=sys.argv[0]))
sys.exit(1)

if len(sys.argv) > 2:
saturation = float(sys.argv[2])

palette = hitherdither.palette.Palette(inky._palette_blend(saturation, dtype='uint24'))

image = Image.open(sys.argv[1]).convert("RGB")
# VERY slow (1m 40s on a Pi 4) - see https://github.com/hbldh/hitherdither for a list of methods
#image_dithered = hitherdither.diffusion.error_diffusion_dithering(image, palette, method="stucki", order=2)

# Usably quick, your vanilla dithering
#image_dithered = hitherdither.ordered.bayer.bayer_dithering(image, palette, thresholds, order=8)

# Usuably quick, half-tone comic-book feel, use order=4 for small dots and order=8 dot bigguns
image_dithered = hitherdither.ordered.cluster.cluster_dot_dithering(image, palette, thresholds, order=8)

# VERY slow
#image_dithered = hitherdither.ordered.yliluoma.yliluomas_1_ordered_dithering(image, palette, order=8)

inky.set_image(image_dithered.convert("P"))
inky.show()
12 changes: 9 additions & 3 deletions library/inky/inky_uc8159.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,20 @@ def __init__(self, resolution=(600, 448), colour='multi', cs_pin=CS0_PIN, dc_pin

self._luts = None

def _palette_blend(self, saturation):
def _palette_blend(self, saturation, dtype='uint8'):
saturation = float(saturation)
palette = []
for i in range(7):
rs, gs, bs = [c * saturation for c in SATURATED_PALETTE[i]]
rd, gd, bd = [c * (1.0 - saturation) for c in DESATURATED_PALETTE[i]]
palette += [int(rs + rd), int(gs + gd), int(bs + bd)]
palette += [255, 255, 255]
if dtype == 'uint8':
palette += [int(rs + rd), int(gs + gd), int(bs + bd)]
if dtype == 'uint24':
palette += [(int(rs + rd) << 16) | (int(gs + gd) << 8) | int(bs + bd)]
if dtype == 'uint8':
palette += [255, 255, 255]
if dtype == 'uint24':
palette += [0xffffff]
return palette

def setup(self):
Expand Down

0 comments on commit 47b2e76

Please sign in to comment.