来自图像的Python数组是单位数字,而不是RGB值

发布于 2025-02-13 16:54:45 字数 1613 浏览 2 评论 0原文

我有一个我正在通过PIL处理的图像,我正在尝试恢复到一个数组,但是我的输出即将成为每个像素的单位数字,我期望RGB值有RGB值。有什么想法吗?我偷偷地怀疑这是位图文件,但不必不必将整个INT集重新映射到其RGB代码...

代码的一部分:

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)

输出样本:

   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

下面是我正在使用的图像:

  1. SRC,与我在量化函数
  2. 输入图像中使用的调色板的图像 - 数组返回RGB值
  3. 输入图像量化函数后 - 当前,数组返回单个值,而不是RGB值

1)src -bitmap file

​href =“ https://i.sstatic.net/jlreg.png” rel =“ nofollow noreferrer”> 3)量化IMG

谢谢!

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

1) src - bitmap file

2) pre-quant img

3) quantized img

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅哥哥的热头脑 2025-02-20 16:54:46

color_image.quantize(Palette = SRC,抖动= 1)将带有指定数量颜色数的图像转换为'P'模式。 P模式图像是托盘的图像IE,而不是存储RGB,仅存储1个字节,而该1个字节是调色板中的索引。因此,您需要将“ P”模式图像转换为RGB

样本

import numpy as py
from PIL import Image

from matplotlib.pyplot import imshow
import numpy as np

src = Image.open("VHBgl.png")
color_image = Image.open("OUiTd.jpg") 
newimage = color_image.quantize(palette=src, dither=1)
rgb_newimage = newimage.convert('RGB')

imshow(np.asarray(rgb_newimage))

print (np.asanyarray(src).shape)
print (np.asanyarray(color_image).shape)
print (np.asanyarray(newimage).shape)
print (np.asanyarray(rgb_newimage).shape)

输出:

”在此处输入图像描述”

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

docs

color_image.quantize(palette=src, dither=1) will convert the image to ‘P’ mode with the specified number of colors. P mode image is a palletised image i.e instead of storing RGB, only 1 byte is stored and this 1 byte is the index into the color palette. So you will need to convert back the ‘P’ mode image to RGB

Sample

import numpy as py
from PIL import Image

from matplotlib.pyplot import imshow
import numpy as np

src = Image.open("VHBgl.png")
color_image = Image.open("OUiTd.jpg") 
newimage = color_image.quantize(palette=src, dither=1)
rgb_newimage = newimage.convert('RGB')

imshow(np.asarray(rgb_newimage))

print (np.asanyarray(src).shape)
print (np.asanyarray(color_image).shape)
print (np.asanyarray(newimage).shape)
print (np.asanyarray(rgb_newimage).shape)

output:

enter image description here

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

Docs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文