[英]Convert a image (png and jpg) to a multi-dimensional list and backwards in python
I use PIL in order to convert imagse to monochrome and afterwards to a list of lists, but I am not sure how to do so with rgb images. 我使用PIL将图像转换为单色,然后转换为列表列表,但我不知道如何使用rgb图像。
Can someone give me a direction how to convert images to a multi-dimensional list and backwards an python? 有人可以给我一个方向如何将图像转换为多维列表并向后转换python?
Let's start with a known sample image. 让我们从一个已知的样本图像开始。 Here's a small 3x2 one to actually work with and a larger one just so you can see it:
这是一个小的3x2实际工作和一个更大的一个,所以你可以看到它:
Small: 小:
Large: 大:
You can open an image and make it into an efficient, fast numpy
multi-dimensional array like this: 你可以打开一个图像,并使其成为一个高效,快捷
numpy
多维数组是这样的:
#!/usr/local/bin/python3
import numpy as np
from PIL import Image
# Open image from disk
im = Image.open('image.png')
na = np.array(im)
That will look like this: 这将是这样的:
array([[[255, 0, 0], # Red
[ 0, 255, 0], # Green
[ 0, 0, 255]], # Blue
[[ 0, 0, 0], # Black
[255, 255, 255], # White
[126, 126, 126]]], dtype=uint8) # Mid-grey
And convert it back to a PIL Image and save like this (just append this code to the code above): 并将其转换回PIL图像并像这样保存(只需将此代码附加到上面的代码中):
# Convert array back to Image
resultim = Image.fromarray(na)
resultim.save('result.png')
Some notes : 一些说明 :
Note 1 注1
If you expect and want an RGB888 image, and you are opening a PNG image, you may get a palettised image which doesn't have RGB values for each pixel, but instead has an index into a palette for each pixel and everything will go wrong! 如果你期望并想要一个RGB888图像,并且你正在打开一个PNG图像,你可能得到一个没有每个像素RGB值的palettised图像,而是每个像素的调色板都有一个索引,一切都会出错!
By way of example, here is the same image as above but when the generating application saved it as a palettised image: 举例来说,这里是与上面相同的图像,但是当生成应用程序将其保存为palettised图像时:
array([[0, 1, 2],
[3, 4, 5]], dtype=uint8)
And here what is returned from im.getpalette()
: 这里是从
im.getpalette()
返回的im.getpalette()
:
[255,
0,
0,
0,
255,
0,
0,
0,
255,
0,
0,
0,
255,
255,
255,
126,
126,
126,
...
...
So, the moral of the story is... if you are expecting an RGB888 image, use: 所以,故事的寓意是......如果您期待RGB888图像,请使用:
Image.open('image.png').convert('RGB')
Note 2 笔记2
Likewise, if you open a PNG file that contains transparency, it will have 4 channels, the last being alpha/transparency, and you should call convert('RGB')
if you wish to discard the alpha channel. 同样,如果你打开一个包含透明度的PNG文件,它将有4个通道,最后一个是alpha / transparency,如果你想丢弃alpha通道,你应该调用
convert('RGB')
。
Note 3 注3
You can abbreviate the loading and saving into single lines if you don't want the intermediate image: 如果您不想要中间图像,可以将加载和保存缩写为单行:
# Load and make array in one go
na = np.array(Image.open('image.png').convert('RGB'))
# Convert back to PIL Image and save in one go
Image.fromarray(na).save('result.png')
Keywords : Image, image processing, numpy, array, ndarray, PIL, Pillow, Python, Python3, palette, PNG, JPG 关键词 :图像,图像处理,numpy,数组,ndarray,PIL,Pillow,Python,Python3,调色板,PNG,JPG
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.