【问题标题】:Average Picture of float Pictures Python浮动图片的平均图片 Python
【发布时间】:2014-10-28 13:14:42
【问题描述】:

我想做类似这篇文章的事情:sample code

如何更改给定代码以适用于“tiff”格式的浮动图像?

这是我尝试过的:

import os, numpy, PIL
from PIL import Image

# Access all PNG files in directory
os.chdir("c://users//Student///Desktop//bilder//" )
print "Current working dir : %s" % os.getcwd()

allfiles=os.listdir(os.getcwd())
imlist=[filename for filename in allfiles if  filename[-5:] in [".tiff",".tiff"]]
#imlist=[filename for filename in allfiles]

# 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,1),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+=imarr/N

# Round values in array and cast as 8-bit integer
arr=numpy.array(numpy.round(arr),dtype=numpy.uint8)

# Generate, save and preview final image
out=Image.fromarray(arr,mode="F")
out.save("Average.tiff")
out.show()

产生此错误:

File "C:\Users\Student\im_average.py", line 25, in <module>
    arr+=imarr/N
ValueError: non-broadcastable output operand with shape (250,250,1) doesn't match the broadcast shape (250,250,250)

我不熟悉 numpy 数组,所以欢迎任何帮助。

【问题讨论】:

    标签: python image numpy python-imaging-library


    【解决方案1】:

    执行 imarr/N 将就地执行并将 dtype 转换为数组的整数。所以,使用 N=float(N)

    参考http://docs.scipy.org/doc/numpy/user/basics.indexing.html#assigning-values-to-indexed-arrays

    请注意,如果将较高类型分配给较低类型(例如将浮点数分配给整数)甚至是异常(将复数分配给浮点数或整数),分配可能会导致更改。

    【讨论】:

    • 这似乎可以做到。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 2013-06-21
    • 2011-03-28
    相关资源
    最近更新 更多