将图像(png)转换为矩阵,然后转换为一维数组 [英] Convert Image ( png ) To Matrix And Then To 1D Array
本文介绍了将图像(png)转换为矩阵,然后转换为一维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有5张图片,我想将每个图像转换为1d数组并将其作为矢量放置在矩阵中.
我希望能够再次将每个矢量转换为图像.
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.
img = Image.open('orig.png').convert('RGBA')
a = np.array(img)
我不熟悉numpy的所有功能,想知道是否还有其他工具可以使用.
谢谢.
I'm not familiar with all the features of numpy and wondered if there other tools I can use.
Thanks.
推荐答案
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()
这篇关于将图像(png)转换为矩阵,然后转换为一维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文