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

标签 python image-processing numpy


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

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

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

最佳答案

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()

关于python - 将图像 ( png ) 转换为矩阵,然后转换为一维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15612373/

相关文章:

python - Matplotlib : display array values with imshow

python - 打开串行端口或使用 pyserial 从串行端口读取时遇到问题

python - 删除停用词 - Python

c - 如何用Matlab或C控制鼠标指针?

python - 使用 Python OpenCV 将二维码旋转到正确的位置

python - 用于生成 NumPy 矩阵的矢量化解决方案

python - 使用 Python 进行高效重采样

python - Django 设置模块和相对路径

python - 如何获取 row_number 是 pyspark 数据帧

python - 如何在python中将视频转换为时空卷