Home > Enterprise >  How do I reduce the color palette of an image in Python 3?
How do I reduce the color palette of an image in Python 3?

Time:07-24

I've seen answers for Python 2, but support for it has ended and the Image module for Python2 is incompatible with aarch64. I'm just looking to convert some images to full CGA color, without the hassle of manually editing the image.

Thanks in advance!

Edit: I think I should mention that I'm still pretty new to Python. I think I should also mention I'm trying to use colors from a typical CGA adapter.

CodePudding user response:

Here's one way to do it with PIL - you may want to check I have transcribed the list of colours accurately from enter image description here

into this:

enter image description here


Or, if you don't really want to write any Python, you can do it on the command-line with ImageMagick in a highly analogous way:

# Create 16-colour CGA swatch in file "cga16.gif"
magick xc:'#000000' xc:'#0000AA' xc:'#00AA00' xc:'#00AAAA' \
       xc:'#AA0000' xc:'#AA00AA' xc:'#AA5500' xc:'#AAAAAA' \
       xc:'#555555' xc:'#5555FF' xc:'#55FF55' xc:'#55FFFF' \
       xc:'#FF5555' xc:'#FF55FF' xc:'#FFFF55' xc:'#FFFFFF' \
        append cga16.gif

That creates this 16x1 swatch (enlarged so you can see it):

enter image description here

# Remap colours in "swirl.jpg" to the CGA palette
magick swirl.jpg  dither -remap cga16.gif resultIM.png

enter image description here


Doubtless the code could be converted for EGA, VGA and the other early PC hardware palettes... or even to the enter image description here

Resulting in:

enter image description here


Or the Bootstrap palette:

enter image description here

Or the Google palette:

enter image description here

  • Related