Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python to convert color formats?

Tags:

python

I'm working on a Python tool to convert image data into these color formats:

  • RGB565
  • RGBA5551
  • RGBA4444.

What's the simplest way to achieve this?

I've used the Python Imaging Library (PIL) frequently. So I know how to load an image and obtain each pixel value in RGBA8888 format. And I know how to write all the conversion code manually from that point.

Is there an easier way? Perhaps some type of 'formatter' plugin for PIL?

Does PIL already support some of the formats I'm targeting? I can't ever figure out which formats PIL really supports without digging though all of the source code.

Or is there a better library than PIL to accomplish this in Python?

Any tips would be appreciated. Thanks!

like image 892
Jon-Eric Avatar asked Apr 13 '26 04:04

Jon-Eric


2 Answers

Changing something from 8 to 5 bits is trivial. In 8 bits the value is between 0 and 255, in 5 bits it's between 0 and 31, so all you need to do is divide the value with 8. Or 4 in the case for green in RGB565 mode. Or 16 in RGBA4444 mode as it uses 4 bits per channel, etc.

Edit: Reading through your question again, I think there is a confusion (either with me or you). RGB555 and RGBA4444 etc are not really formats, like GIF or JPG, they are color spaces. That conversion is trivial (see above). What file format you want to save it in later is another question. Most file formats have limited support for color spaces. I think for example that JPEG always saves it in YCbCr (but I could be mistaken), GIF uses a palette (which in turn always is RGB888, I think) etc.

like image 95
Lennart Regebro Avatar answered Apr 15 '26 18:04

Lennart Regebro


There's a module called Python Colormath which provides a lot of different conversions. Highly recommended.

like image 41
mikl Avatar answered Apr 15 '26 18:04

mikl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!