Miscellaneous cleanup

- Remove old dither code from dither.py
- Commit an old color-distance experiment
This commit is contained in:
2022-06-26 13:18:40 -07:00
parent 835cf49be8
commit cdfce3a4f8
3 changed files with 51 additions and 35 deletions
-35
View File
@@ -108,38 +108,3 @@ def pattern_dither(img, palette, *, pat_size=4, amount=0.75):
result.putpalette([c for rgb in old_palette for c in rgb])
result.putdata([old_palette_indexes[i] for i in output_indexes])
return result
def dither_pixel(img, xy, pal):
global bayer
pal2 = [np.array(rgb_to_linear(c)) for c in pal]
counts = [0]*len(pal)
x, y = xy
def diff(c1, c2):
return sum((c1-c2)**2)
def nearest(rgb):
cost, index = min((diff(rgb,c), i) for i,c in enumerate(pal2))
return index
color = np.array(rgb_to_linear(img.getpixel(xy)))
err = np.array([0.0,0.0,0.0])
for _ in range(bayer.size):
err += color
i = nearest(err)
counts[i] += 1
err -= pal2[i]
thresh = bayer[y % bayer.shape[0]][x % bayer.shape[1]]
for i, count in enumerate(counts):
thresh -= count
if thresh < 0:
return i
dithered = Image.new('P', resized.size)
dithered.putpalette([x for rgb in PICO_RGB[:16] for x in rgb])
def do_it():
global dithered, resized
for x in tqdm(range(128)):
for y in range(128):
i = dither_pixel(resized, (x,y), PICO_RGB[:16])
dithered.putpixel((x,y), i)
# import cProfile
# cProfile.run('do_it()')
#do_it()