diff --git a/2bit.py b/2bit.py index e3dfe0b..015e0ea 100644 --- a/2bit.py +++ b/2bit.py @@ -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() diff --git a/test/run.py b/test/run.py index b3a0652..10d3f3e 100644 --- a/test/run.py +++ b/test/run.py @@ -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 diff --git a/test/test8.jpg b/test/test8.jpg deleted file mode 100644 index b882f21..0000000 Binary files a/test/test8.jpg and /dev/null differ diff --git a/test/test8.png b/test/test8.png new file mode 100644 index 0000000..b2e3b5c Binary files /dev/null and b/test/test8.png differ diff --git a/test/test9.jpg b/test/test9.jpg deleted file mode 100644 index 16c95a3..0000000 Binary files a/test/test9.jpg and /dev/null differ