我是python新手。我想知道你是否可以给我一个想法,关于我如何平均每个连续的两个图像数组在10000图像的矩阵。我想降低我电影的节奏。我找到了下面的代码,但我想平均一个矩阵的图像,而不是png或jpeg格式。
import os, numpy, PIL
from PIL import Image
# Access all PNG files in directory
allfiles=os.listdir(os.getcwd())
imlist=[filename for filename in allfiles if filename[-4:] in[".tif",".TIF"]]
# Assuming all images are the same size, get dimensions of first image
w,h = Image.open(imlist[0]).size
N = len(imlist)
# Create a numpy array of floats to store the average (assume RGB images)
arr = numpy.zeros((h,w,3),numpy.float)
# Build up average pixel intensities, casting each image as an array of floats
for im in imlist:
imarr = numpy.array(Image.open(im),dtype=numpy.float)
arr = arr+imarr/N
# Round values in array and cast as 16-bit integer
arr = numpy.array(numpy.round(arr),dtype=numpy.uint16)
# Generate, save and preview final image
out = Image.fromarray(arr,mode="RGB")
out.save("Average.tif")
提前谢谢你
Python
你需要一个方法得到[[img0, img1], [img2, img3], [img4, img5], ...]
。
所以你需要生成如下数字:
0, 1
2, 3
4, 5
...
998, 999
生成这些对:
pairs = [[2 * i, 2 * i + 1] for i in range(500)]
现在您可以遍历pairs
并选择以下图像:
for pair in pairs:
average(imgs[pair[0]], imgs[pair[1]])
PS:请注意这将是非常缓慢的。较好的方法是使用numpy
、reshape
和mean()
。
Numpy
假设您可以访问numpy
,并且您有1000
图像列表作为imgs
。我正在生成1000个25x25数组作为图像:
import numpy as np
imgs = np.array([np.ones((25, 25)) * i for i in range(1000)])
image_pairs = imgs.reshape((500, 2, 25, 25))
print(np.mean(image_pairs, axis=1))