5

I wonder how I could create a image with a transparent background and only 2 indexed colours (red and blue) to minimise the file size?

More specifically I have two black and white images that I want to convert, one to transparent and blue and the other to transparent and red. Then I want to merge those 2 images. I could do that with a regular RGBA image, but I really want the colour to be indexed to minimise the file size.

Ideally with PIL, but other Python library could also work.

Tickon
  • 1,058
  • 1
  • 16
  • 25

2 Answers2

7

So I managed to do it, using "palette" image type, but the resulting file is not as small as I expected... Here's my code in case its useful for someone else, or if someone can improve on it.

from PIL import Image

im = Image.open("image1.png")
imP = im.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=3)
imP.putpalette([
    0, 0, 0, # index 0 is black background
    0, 0, 255, # index 1 is blue
    255, 0, 0, # index 2 is red ])

im2 = Image.open("image2.png")
imP2L = im2.convert('L') # need a greyscale image to create a mask
mask = Image.eval(imP2L, lambda a: 255 if a == 0 else 0)
imP.paste(2, mask) # Paste the color of index 2 using image2 as a mask
imP.save('out3.png', transparency = 0, optimize = 1) # Save and set index 0 as transparent
Tickon
  • 1,058
  • 1
  • 16
  • 25
  • From your question I thought you wanted full 8-bit transparency, not 1 transparent color. Of course that's easy! – Mark Ransom Oct 12 '11 at 13:50
2

Once you merge the two images, you won't have two colors any more - the colors will combine based on the transparency of each one at every pixel location. Worst case, you will have 256*256=65536 colors, which can't be indexed and wouldn't compress well if you did.

I would suggest saving as a PNG and let the lossless compression do its best.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Humm, could I make an image with a transparent background and only 1 colour indexed? – Tickon Oct 11 '11 at 23:03
  • @Tickon, PIL will allow you to create an image in `PA` mode but it won't let you save it. I really think you should save it as `RGBA` and if you really need it smaller, use a utility that is dedicated to that task. – Mark Ransom Oct 12 '11 at 02:24
  • I found a way to save a P mode image with transparency, see my answer. – Tickon Oct 12 '11 at 07:10