Python 将图像 (png) 转换为矩阵,然后转换为一维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15612373/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:33:09  来源:igfitidea点击:

Convert Image ( png ) To Matrix And Then To 1D Array

pythonimage-processingnumpy

提问by Ofir Attia


I have 5 pictures and i want to convert each image to 1d array and put it in a matrix as vector.
I want to be able to convert each vector to image again.


我有 5 张图片,我想将每个图像转换为一维数组并将其作为向量放入矩阵中。
我希望能够再次将每个向量转换为图像。

img = Image.open('orig.png').convert('RGBA')
a = np.array(img)

I'm not familiar with all the features of numpy and wondered if there other tools I can use.
Thanks.

我不熟悉 numpy 的所有功能,想知道是否还有其他工具可以使用。
谢谢。

回答by unutbu

import numpy as np
from PIL import Image

img = Image.open('orig.png').convert('RGBA')
arr = np.array(img)

# record the original shape
shape = arr.shape

# make a 1-dimensional view of arr
flat_arr = arr.ravel()

# convert it to a matrix
vector = np.matrix(flat_arr)

# do something to the vector
vector[:,::10] = 128

# reform a numpy array of the original shape
arr2 = np.asarray(vector).reshape(shape)

# make a PIL image
img2 = Image.fromarray(arr2, 'RGBA')
img2.show()

回答by R.jzadeh

I used to convert 2D to 1D image-array using this code:

我曾经使用以下代码将 2D 转换为 1D 图像数组:

import numpy as np
from scipy import misc
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt

face = misc.imread('face1.jpg');
f=misc.face(gray=True)
[width1,height1]=[f.shape[0],f.shape[1]]
f2=f.reshape(width1*height1);

but I don't know yet how to change it back to 2D later in code, Also note that not all the imported libraries are necessary, I hope it helps

但我还不知道以后如何在代码中将其改回 2D,另外请注意,并非所有导入的库都是必需的,希望对您有所帮助

回答by RickWe

import matplotlib.pyplot as plt

img = plt.imread('orig.png')
rows,cols,colors = img.shape # gives dimensions for RGB array
img_size = rows*cols*colors
img_1D_vector = img.reshape(img_size)
# you can recover the orginal image with:
img2 = img_1D_vector.reshape(rows,cols,colors)

Note that img.shapereturns a tuple, and multiple assignment to rows,cols,colorsas above lets us compute the number of elements needed to convert to and from a 1D vector.

请注意,img.shape返回一个元组,并且rows,cols,colors如上所述的多重赋值让我们计算与一维向量相互转换所需的元素数量。

You can show img and img2 to see they are the same with:

您可以显示 img 和 img2 以查看它们与以下内容相同:

plt.imshow(img) # followed by 
plt.show() # to show the first image, then 
plt.imshow(img2) # followed by
plt.show() # to show you the second image.

Keep in mind in the python terminal you have to close the plt.show()window to come back to the terminal to show the next image.

请记住,在 python 终端中,您必须关闭plt.show()窗口才能返回终端以显示下一张图像。

For me it makes sense and only relies on matplotlib.pyplot. It also works for jpg and tif images, etc. The png I tried it on has float32 dtype and the jpg and tif I tried it on have uint8 dtype (dtype = data type); each seems to work.

对我来说这是有道理的,只依赖于 matplotlib.pyplot。它也适用于 jpg 和 tif 图像等。我试过的 png 有 float32 dtype,我试过的 jpg 和 tif 有 uint8 dtype(dtype = 数据类型);每个似乎都有效。

I hope this is helpful.

我希望这是有帮助的。