# For "little endian", uncomment the following line # Assume (b7, b6) are pixel0, (b5, b4) are pixel1… # and values, because PIL does not yet support # setup: create a map with tuples as keys Return nvert("RGB").quantize(palette=pal_image)įor "added value", here follows code to produce the packed string (4 pixels per byte): import itertools as it Of course, turns out I was too enthusiastic and -as Thomas discovered- the Image.quantize method can take a palette image as argument and do the quantization with far better results than my ad-hoc method above: def cga_quantize(image): If you need help about what the code does, please ask and I will explain. It's trivial to take the output of image2cga (yields a sequence of 0-3 values) and pack every four values to a byte. This creates a PNG palette image with only the first four palette entries set to your colours. Newpathname= os.path.join(dirname, "cga-%s.png" % name) Out_im= Image.new("P", inp_im.size, None) Inp_im= Image.open(imgfn) # assume it's RGB "Convert an RGB image to (K, R, G, Y) CGA image" G_bit, g_error= estimate_color(g, 2, g_error) R_bit, r_error= estimate_color(r, 1, r_error) If index % im_width = 0: # start of a line "Produce a sequence of CGA pixels from image im"įor index, (r, g, b) in enumerate(im.getdata()): So, you have to accept that your output image will hardly approximate the input image, unless there is no blue component to start with. First: your four colour palette (black, green, red, yellow) has no blue component.
0 Comments
Leave a Reply. |