来自图像的Python数组是单位数字,而不是RGB值
我有一个我正在通过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
下面是我正在使用的图像:
- SRC,与我在量化函数
- 输入图像中使用的调色板的图像 - 数组返回RGB值
- 输入图像量化函数后 - 当前,数组返回单个值,而不是RGB值
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:
- src, the image with the palette I'm using in the quantize function
- input image - array returns RGB values
- input image after quantize function - currently, array returns single values instead of RGB values
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
color_image.quantize(Palette = SRC,抖动= 1)
将带有指定数量颜色数的图像转换为'P'模式。 P模式图像是托盘的图像IE,而不是存储RGB,仅存储1个字节,而该1个字节是调色板中的索引。因此,您需要将“ P”模式图像转换为RGB样本
输出:
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 RGBSample
output:
Docs