0

Is it possible to remape (for example) a 256-colors-gif image into a 128-colors or 64-colors gif image in Python?

I tried it using the imageio library

import imageio

def myfunction(inputgif, outputgif, duration):
  gifimage = imageio.mimread(inputgif)
  for i in range(len(gifimage)):
     gifimage[i] = gifimage[i]/4

  with imageio.get_writer(outputgif, mode='I', duration=duration) as writer:
      for x in range(len(gifimage)):
        writer.append_data(gifimage[x])
  writer.close()

It's working, but, the problem is that the image file still thinks that "his universe" is 256 possible values for the color (from 0 to 255), not from 0 to 64 (since I divided all the values by 4). Which makes the image goes only darker, instead of using a 64 (4 bits) possible colors.

My goal is to obtain a lossy data compression by decreasing number of colors.

Madno
  • 910
  • 2
  • 12
  • 27
  • You know, that a sane approach will use some kind of vector-quantization (to optimize the kept colors) and a custom-color palette (only using the previously calculated colors)? This is very different from your code! I don't know what you expect with your approach (it does a linear color-transform in some space; far away from lossy depending on your expectation). There are probably many libs doing exactly this, but implementing this all yourself with python might be some work. – sascha Apr 23 '17 at 12:49
  • @sascha thank you. Can you give me some examples on the libraries which I can use please? – Madno Apr 23 '17 at 12:54
  • 1
    Well, one of the most popular ones (in the open-source world): [ImageMagick](http://www.imagemagick.org/Usage/quantize/). – sascha Apr 23 '17 at 12:57
  • Why do you want to compress ? –  Apr 23 '17 at 16:08
  • see [Effective gif/image color quantization?](http://stackoverflow.com/a/30265253/2521214) – Spektre Apr 24 '17 at 06:32

1 Answers1

0

The GIF format only supports palette entries on 8 bits.

Even if it supported less, the savings would be marginal: 12.5% for 7 bits, 25% for 6 bits.

  • But I can generate a .gif image which is smaller than the original if I changed the color palette from 256 to 128 or 64, 32.. etc? That's my goal. I found a small option in the imageio library which is "palettesize=n" to set that setting. – Madno Apr 24 '17 at 08:33
  • You don't understand: GIF does not support that. –  Apr 24 '17 at 08:36
  • What I know is that is supports "maximum 8", but I can see the results in my eyes. I changed the palettesize for a .gif image and I obtained a very smaller .gif image. – Madno Apr 24 '17 at 08:38