Home > Software engineering >  Python array from image is single digits, not RGB values
Python array from image is single digits, not RGB values

Time:07-09

I have an image that I've processed through PIL that I'm trying to recover to an array, but my output is coming to be single digits per pixel where I'm expecting an RGB value. Any thoughts? I have a sneaking suspicion that it's the bitmap file but would love not to have to remap the entire set of int to their RGB code...

Portion of code:

import PIL
import numpy as py
from PIL import Image
from PIL import ImagePalette as ip

        src = Image.open(img_src)                   //quanticization palette--.bmp file
        color_image = Image.fromarray(color_image)  //called from earlier code
        print("newarray")
        newimage = color_image.quantize(palette=src, dither=1)
        print("quantized")

        #newimage.show()

        downsample = newimage.resize((640,360))
        #downsample.save("EMTest_down.png")
        # need to figure out how to group the colors better...

        upsample = downsample.resize((w,h), PIL.Image.Resampling.BICUBIC )
        #upsample.save("EMtest_up.png")       
        print("exported")
        upsample.show()


        ## Shape the image info to align with depth
        rgb = np.asanyarray(upsample)

Output sample:

   array([[4., 4., 3., ..., 5., 5., 5.],
   [4., 4., 3., ..., 5., 5., 5.],
   [5., 5., 3., ..., 3., 5., 5.],
   ...,
   [7., 7., 7., ..., 4., 4., 4.],
   [6., 6., 6., ..., 4., 4., 4.],
   [6., 6., 6., ..., 4., 4., 4.]], dtype=uint8) ndarray

Below are the images I'm working with:

  1. src, the image with the palette I'm using in the quantize function
  2. input image - array returns RGB values
  3. input image after quantize function - currently, array returns single values instead of RGB values

enter image description here

(311, 175)
(886, 1536, 3)
(886, 1536)
(886, 1536, 3)

Docs

  • Related