From f8359d0812d1ef43281278677d36990d2c26e0c3 Mon Sep 17 00:00:00 2001 From: Niles Rogoff Date: Fri, 8 Jul 2016 14:13:22 -0400 Subject: [PATCH] Initial commit --- 2bit.py | 38 ++++++++++++++++++++++++++++++++++++++ README.md | 9 +++++++++ 2 files changed, 47 insertions(+) create mode 100644 2bit.py create mode 100644 README.md diff --git a/2bit.py b/2bit.py new file mode 100644 index 0000000..538e249 --- /dev/null +++ b/2bit.py @@ -0,0 +1,38 @@ +bits = 2 +outfile = "out.png" +print_debug = False + +# End of the configuration section + +import PIL.Image, sys +def debug(*args): + if print_debug: print(*args) +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" +if bits == 1: + mode = "1" +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) +for x in range(image.width): + for y in range(image.height): + pos = (x,y) + color = image.getpixel(pos) + debug(color) + if len(color) == 4: + color = color[:3] # Exclude alpha layer + 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]) + debug() + debug("------------") + debug() +out.save(outfile) diff --git a/README.md b/README.md new file mode 100644 index 0000000..2ebcc5f --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# Image downsampling converter + +Inspired by [2bit](http://2bit.neocities.org/) + +Usage: `python3 2bit.py my_image_file.png` + +Warning: Will probably turn transparency black + +You can change the number of bits at the top of the file for different results. 1 would be just black and white, while 8 would be the original image in greyscale.