2bit/2bit.py

50 lines
1.5 KiB
Python
Raw Normal View History

2016-07-08 20:13:22 +02:00
bits = 2
outfile = "out.png"
per_color = False
dither = False
2016-07-08 20:13:22 +02:00
# End of the configuration section
import PIL.Image, sys, random
2016-07-08 20:57:10 +02:00
if len(sys.argv) >= 3:
outfile = sys.argv[2]
if len(sys.argv) >= 4:
bits = int(sys.argv[3])
args = [f.lower() for f in sys.argv]
if "--dither" in args:
dither = float(args[args.index("--dither") + 1].replace("%",""))
if dither > 1:
dither /= 100
per_color = "--per-color" in args
2016-07-08 20:13:22 +02:00
def binprecision(start, bits, length):
end = bin(int(start))[2:]
while len(end) < bits:
end = '0' + end
return end[:length]
image = PIL.Image.open(sys.argv[1])
mode = "L" # 1x8 bit unsigned integer
if per_color:
mode = "RGB"
elif bits == 1:
mode = "1" # 1x1 bit unsigned integer
2016-07-08 20:13:22 +02:00
out = PIL.Image.new(mode,image.size)
colors = [int(i*255.0/(2**bits-1)) for i in range(2**bits)]
for x in range(image.width):
for y in range(image.height):
pos = (x,y)
color = image.getpixel(pos)
if len(color) == 4:
color = color[:3] # Exclude alpha layer
color = list(color)
if not per_color:
color = [float(sum(color))/len(color)]
for z in range(len(color)):
if dither:
color[z] += 255 * random.uniform(-dither,dither)
color[z] = min(color[z], 255)
color[z] = max(color[z], 0)
index = int(binprecision(color[z], 8, bits), 2)
color[z] = colors[index]
out.putpixel(pos, tuple(color))
2016-07-08 20:13:22 +02:00
out.save(outfile)