Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Create 8 bit compressed RGB with Pillow

$
0
0

For my college project, I need to convert a 4-byte colored png file to a 1-byte colored png file using Keil.

I've converted a png file to a hex file using Python, then loaded a hex file in Keil, progressed the data, and got the output.

In Keil, a hex file with a 4-byte RGBA was changed to a 1-byte RGB(RRR_GGG_BB in each byte) by removing the alpha channel and concatenating 3 or 2 MSB of each color.

Compressing process MUST be done in keil.

So the output hex file has 1-byte RGB data.

I put this data in python, reshape the array, but I can't make it to PNG file.

Here's the code that I tried.

from intelhex import IntelHexfrom PIL import Imageimport numpy as npih = IntelHex()ih.loadhex('memoutput.hex')pydict=ih.todict()rgbcomp = np.fromiter(pydict.values(), dtype=int)rgbcomp = np.reshape(rgbcomp,(640,960))img_2 = Image.fromarray(rgbcomp, 'P')# https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modesimg_2.show()

Each data in rgbcomp array has 1-byte RGB data(R[7:5]+G[7:5]+B[7:6]).

Does anyone knows how to make reshaped array to 256 color PNG using pillow?

It should be look like true-color image converted into 256 color gif image.

Please help.

++ Added at 14:15, May 23, 2024
Here's the file of keil's output memoutput.hex

And also I've almost done that make pltePix - targetting the number of palette, plte - palette that decompose RRRGGGBB to R[7:0], G[7:0], B[7:0]

from intelhex import IntelHexfrom PIL import Imageimport numpy as npih = IntelHex()ih.loadhex('memoutput.hex')pydict=ih.todict() #hex to dictrgbcomp = np.fromiter(pydict.values(), dtype=int) #array of values in dictuniqRgbComp = np.unique(rgbcomp) #int list of uniq colorsplte = np.zeros((len(uniqRgbComp),3)) #plte(24bit rgb) = uniqRgbComp(8bit rgb)for i in range(len(uniqRgbComp)):   plte[i] = np.array([ int(uniqRgbComp[i]&0b11100000), int( ((uniqRgbComp[i])&0b00011100) << 3 ), int( (uniqRgbComp[i]&0b00000011) << 6 ) ])pltePix = np.zeros(len(rgbcomp)) #Pixel data points palettefor i in range(len(rgbcomp)):    pltePix[i] = np.argwhere(uniqRgbComp==rgbcomp[i])[0]pltePix = np.reshape(pltePix, (640, 960))

The last, I just need to write PNG file using pltePix(pixel data), and plte(palette).

Can I get help to write 1-byte RGB image with these two arrays?

The original image is:original_image

The 256 color image will looks like(generated by photoshop, 3:3:2 color will looks different):8bit_image


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>