PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 28, 2022

[FIXED] How do I reduce the color palette of an image in Python 3?

 July 28, 2022     image, python-3.x, python-imaging-library     No comments   

Issue

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.


Solution

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

#!/usr/bin/env python3

from PIL import Image

# Create new 1x1 palette image
CGA = Image.new('P', (1,1))

# Make a CGA palette and push it into image
CGAcolours = [ 
   0x00, 0x00, 0x00,
   0x00, 0x00, 0xaa,
   0x00, 0xaa, 0x00,
   0x00, 0xaa, 0xaa,
   0xaa, 0x00, 0x00,
   0xaa, 0x00, 0xaa,
   0xaa, 0x55, 0x00,
   0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55,
   0x55, 0x55, 0xff,
   0x55, 0xff, 0x55,
   0x55, 0xff, 0xff,
   0xff, 0x55, 0x55,
   0xff, 0x55, 0xff,
   0xff, 0xff, 0x55,
   0xff, 0xff, 0xff]
CGA.putpalette(CGAcolours)

# Open our image of interest
im = Image.open('swirl.jpg')

# Quantize to our lovely CGA palette, without dithering
res = im.quantize(colors=len(CGAcolours), palette=CGA, dither=Image.Dither.NONE)

res.save('result.png')

That transforms this:

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 Rubik's Cube palette:

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



Answered By - Mark Setchell
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing