我想在Python枕头上做一些图像叠加。我想做的是获取大量图像(比如10张),然后对每个像素取中值:http://petapixel.com/2013/05/29/a-look-at-reducing-noise-in-photographs-using-median-blending/。在
现在,我可以用一种难以置信的蛮力方式(使用getpixel和put pixel)来完成,但这需要很长时间。在
以下是我目前所掌握的情况:
import os
from PIL import Image
files = os.listdir("./")
new_im = Image.new('RGB', (4000,3000))
ims={}
for i in range(10,100):
ims[i]={}
im=Image.open("./"+files[i])
for x in range(400):
ims[i][x]={}
for y in range(300):
ims[i][x][y]=im.getpixel((x,y))
for x in range(400):
for y in range(300):
these1=[]
these2=[]
these3=[]
for i in ims:
these1.append(ims[i][x][y][0])
these2.append(ims[i][x][y][1])
these3.append(ims[i][x][y][2])
these1.sort()
these2.sort()
these3.sort()
new_im.putpixel((x,y),(these1[len(these1)/2],these2[len(these2)/2],these3[len(these3)/2]))
new_im.show()
你可以用数组对很多循环进行矢量化。例如,
np.array(im)
将返回一个像素数组,其形状为(400300,3)。所以把所有的东西都存储在一个数组中。在现在您可以用自己喜欢的方法计算中值,但是numpy也有一个method。在
^{pr2}$这里需要注意的两件事是
np.median()
将返回一个float类型的数组,我们希望将其转换为无符号int8。另一件事是,如果元素的数量是偶数,则中值被计算为两个中间值的平均值,最终可能是一个奇数除以2,例如13.5。但当它被转换成整数时,它将被舍入。这种微小的精度损失不应在视觉上影响你的结果。在相关问题 更多 >
编程相关推荐