Python Pillow Image 对象和 numpy 数组之间的转换会改变维度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19016144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conversion between Pillow Image object and numpy array changes dimension
提问by Jongsu Liam Kim
I am using Pillow and numpy, but have a problem with conversion between Pillow Image object and numpy array.
我正在使用 Pillow 和 numpy,但在 Pillow Image 对象和 numpy 数组之间的转换存在问题。
when I execute following code, the result is weird.
当我执行以下代码时,结果很奇怪。
im = Image.open(os.path.join(self.img_path, ifname))
print im.size
in_data = np.asarray(im, dtype=np.uint8)
print in_data.shape
result is
结果是
(1024, 768)
(768, 1024)
Why dimension is changed?
为什么要改变维度?
采纳答案by prgao
im maybe column-major while arrays in numpy are row-major
我可能是列优先,而 numpy 中的数组是行优先
do in_data = in_data.T
to transpose the python array
做in_data = in_data.T
转置蟒阵列
probably should check in_data with matplotlib
's imshow
to make sure the picture looks right.
可能应该用matplotlib
's检查 in_data 以imshow
确保图片看起来正确。
But do you know that matplotlib comes with its own loading functions that gives you numpy arrays directly? See: http://matplotlib.org/users/image_tutorial.html
但是你知道 matplotlib 自带加载函数,直接给你 numpy 数组吗?请参阅:http: //matplotlib.org/users/image_tutorial.html
回答by Nathan Harmon
If your image is greyscale do:
如果您的图像是灰度的,请执行以下操作:
in_data = in_data.T
but if you are working with rbg images you want to make sure your transpose operation is along only two axis:
但如果您正在处理 rbg 图像,您要确保您的转置操作仅沿两个轴:
in_data = np.transpose(in_data, (1,0,2))
回答by ?mig?o
actually this is because most image libraries give you images that are transpozed compared to numpy arrays. this is (i think) because you write image files line by line, so the first index (let's say x
) refers to the line number (so x
is the vertical axis) and the second index (y
) refers to the subsequent pixel in line (so y
is the horizontal axis), which is against our everyday coordinates sense.
实际上这是因为与 numpy 数组相比,大多数图像库为您提供了转置的图像。这是(我认为)因为您逐行写入图像文件,所以第一个索引(假设x
)指的是行号(x
垂直轴也是如此),第二个索引 ( y
) 指的是行中的后续像素(所以y
是横轴),这违背了我们日常的坐标感。
If you want to handle it correctly you need to remember to write:
如果你想正确处理它,你需要记住写:
image = library.LoadImage(path)
array = (library.FromImageToNumpyArray(image)).T
and consequently:
因此:
image = library.FromNumpyArrayToImage(array.T)
library.WriteImage(image, path)
Which works also for 3D images. But i'm not promising this is the case for ALL image libraries - just these i worked with.
这也适用于 3D 图像。但我不保证所有图像库都是这种情况 - 只是我使用过的这些。