TypeError: ‘float’ object cannot be interpreted as an integer
Debug方法:将上述代码中“/”改为“//” 即可
*原因:py3.xx中 / 符号是保留小数的,返回的是float类型,而在py2.xx中 / 是取整,是int类型。
AttributeError: 'dict' object has no attribute 'iteritems'
Debug方法:用 items() 替换 iteritems()
*原因:在py2.xx中,iteritems() 用于返回本身字典列表操作后的迭代器,而在py3.xx中则废除了这一用法。
附图片去雾处理程序。
from PIL import Image
from guidedfilter import *
def getDark(input_img, filter, frame):
"""得 到 暗 化 后 的 图 片"""
size = input_img.size
output = []
for x in range(size[1]):
temp = []
for y in range(size[0]):
temp.append(min(input_img.getpixel((y, x))))
output.append(temp)
output = filter2d(output, filter, frame)
output_img = Image.new('L', size)
for x in range(size[1]):
for y in range(size[0]):
output_img.putpixel((y, x), output[x][y])
return output_img
def getLight(srcImage, darkImage, cut):
"""从 图 片 中 获 取 大 气 光"""
size = darkImage.size
light = []
for x in range(size[0]):
for y in range(size[1]):
light.append(darkImage.getpixel((x, y)))
light.sort()
light.reverse()
threshold = light[int(cut * len(light))]
atmosphere = {}
for x in range(size[0]):
for y in range(size[1]):
if darkImage.getpixel((x, y)) >= threshold:
atmosphere.update({(x, y): sum(srcImage.getpixel((x, y))) / 3.0})
pos = sorted(atmosphere.items(), key = lambda item: item[1], reverse = True)[0][0]
return srcImage.getpixel(pos)
def getTransmission(input_img, light, omiga):
"""从 图 片 中 获 取 传 输 函 数"""
size = input_img.size
output = []
for x in range(size[1]):
temp = []
for y in range(size[0]):
temp.append(min(input_img.getpixel((y, x))) / float(min(light)))
output.append(temp)
transmission = []
for x in range(size[1]):
temp = []
for y in range(size[0]):
temp.append(1 - omiga * minimizeFilter(output, (x, y), (10, 10)))
transmission.append(temp)
return transmission
def getRadiance(input_img, transmission, light, t0):
"""从 该 图 片 中 获 取 光 彩"""
size = input_img.size
output = Image.new('RGB', size)
for x in range(size[1]):
for y in range(size[0]):
r, g, b = input_img.getpixel((y, x))
r = int((r - light[0]) / float(max(t0, transmission[x][y])) + light[0])
g = int((g - light[1]) / float(max(t0, transmission[x][y])) + light[1])
b = int((b - light[2]) / float(max(t0, transmission[x][y])) + light[2])
output.putpixel((y, x), (r, g, b))
return output
def ensure(n):
if n < 0:
n = 0
if n > 255:
n = 255
return int(n)
if __name__ == '__main__':
image = Image.open('2.png')
image = image.convert('RGB')
dark = getDark(image, minimizeFilter, (10, 10))
dark.save('2_dark.png')
light = getLight(image, dark, 0.001)
transmission = getTransmission(image, light, 0.9)
tranImage = Image.new('L', image.size)
grayImage = image.convert('L')
for x in range(image.size[0]):
for y in range(image.size[1]):
tranImage.putpixel((x, y), int(transmission[y][x] * 255))
guided = guidedFilter(grayImage, tranImage, 25, 0.001)
guidedImage = Image.new('L', image.size)
for x in range(image.size[0]):
for y in range(image.size[1]):
guidedImage.putpixel((x, y), ensure(guided[y][x]))
guided[y][x] /= 255.0
#guidedImage.show()
guidedImage.save('2_guided.png')
output = getRadiance(image, guided, light, 0.1)
output.save('2_haze.png')