【发布时间】:2020-04-08 12:24:18
【问题描述】:
我正在制作一个需要我将图像转换为图像的每像素 RGB 值的固定大小数组的项目。读取图像如下:
import matplotlib.pyplot as plt
image = plt.imread("dummy_image.jpg") #Example image with a shape of 32x32
print(image.shape)
应该输出:
(32, 32, 3)
问题是我循环了数百张图像,对于每张图像,我分配了一个固定大小的数组(例如 64x64x3)并通过循环单独分配每个像素的值。
import numpy as np
arr1 = np.zeros((64, 64, 3))
#Then I looped for each pixel value and assigned it to arr1
x, y, w = image.shape
for i in range(x):
for j in range(y):
arr1[i][j] = image[i][j]
您可以推断,这段代码需要很长时间才能完成。更重要的是,由于每个图像的大小都是不同的,我无法合并它并为numpy.pad 创建一个函数以将图像大小固定为零。
谁能给我提示或技巧,让我以更节省内存和/或节省时间的方式做到这一点?
【问题讨论】:
-
一定要用python吗?
标签: python image numpy matplotlib