Added support for changing bits per color

This commit is contained in:
Niles Rogoff 2016-07-08 19:21:09 -04:00
parent 274604d63c
commit 796269d58a
5 changed files with 26 additions and 12 deletions

24
2bit.py
View File

@ -1,6 +1,7 @@
bits = 2
outfile = "out.png"
print_debug = False
per_color = False
# End of the configuration section
@ -9,6 +10,7 @@ if len(sys.argv) >= 3:
outfile = sys.argv[2]
if len(sys.argv) >= 4:
bits = int(sys.argv[3])
per_color = "--per-color" in [f.lower() for f in sys.argv]
def debug(*args):
if print_debug: print(*args)
def binprecision(start, bits, length):
@ -17,9 +19,11 @@ def binprecision(start, bits, length):
end = '0' + end
return end[:length]
image = PIL.Image.open(sys.argv[1])
mode = "L"
if bits == 1:
mode = "1"
mode = "L" # 1x8 bit unsigned integer
if per_color:
mode = "RGB"
elif bits == 1:
mode = "1" # 1x1 bit unsigned integer
out = PIL.Image.new(mode,image.size)
colors = [int(i*255.0/(2**bits-1)) for i in range(2**bits)]
debug(image.width, image.height)
@ -30,12 +34,16 @@ for x in range(image.width):
debug(color)
if len(color) == 4:
color = color[:3] # Exclude alpha layer
color = float(sum(color))/len(color)
color = list(color)
if not per_color:
color = [float(sum(color))/len(color)]
debug(color)
debug(bin(int(color)))
index = int(binprecision(color, 8, bits), 2)
debug(index)
out.putpixel(pos, colors[index])
for z in color: debug(bin(int(z)))
for z in range(len(color)):
index = int(binprecision(color[z], 8, bits), 2)
debug(index)
color[z] = colors[index]
out.putpixel(pos, tuple(color))
debug()
debug("------------")
debug()

View File

@ -8,13 +8,19 @@ files = glob.glob("*")
del files[files.index("run.py")]
if not os.path.isdir("out"): os.mkdir("out")
i = 0
max_i = len(files) * 8
max_i = len(files) * (8+8)
for f in files:
if (os.path.isdir(f)): continue
for bits in range(8):
for bits in range(8+8):
bits += 1
percolor = []
pc = ""
if bits > 8:
percolor = ["--per-color"]
pc = "-per-color"
print(str(int(float(1000*i)/max_i)/10) + "% done")
outfilename = "out/" + f.split(".")[:-1] + "-output-" + str(bits) + "bits.png"
bits_formatted = str((bits-1)%8+1)
outfilename = "out/" + ".".join(f.split(".")[:-1]) + "-output" + pc + "-" + bits_formatted + "bits.png"
print("On " + outfilename)
subprocess.call(["python3", "../2bit.py", f, outfilename, str(bits)])
subprocess.call(["python3", "../2bit.py", f, outfilename, bits_formatted, *percolor])
i += 1

Binary file not shown.

Before

Width:  |  Height:  |  Size: 110 KiB

BIN
test/test8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 549 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 71 KiB