使用putdata使图像在python中变亮

时间:2014-10-23 14:36:13

标签: python computer-vision python-imaging-library

这就是问题:

用Python编写程序,打开文件'pixerror.png',删除S& P噪音并修正图像的亮度,将处理后的照片保存为其他名称。你必须看像素为像素,不能使用blurb函数。

我的第一个任务是让图像更亮。

我使用了this问题代码,但我无法解决我的错误。这个错误:

_

ruisclasse.py 41      
putdata        exceptions.SystemError: 
new style getargs format but argument is not a tuple

_

这是我的代码。

_

from __future__ import division  #for floating number
from PIL import Image
import cv2

filename='pixerror.png'
action = 'lighten'
extent = 10


#load the original image into a list
original_image = Image.open(filename, 'r')
pixels = original_image.getdata()


#initialise the new image
new_image = Image.new('RGB', original_image.size)
new_image_list = []

brightness_multiplier = 1.1

if action == 'lighten':
    brightness_multiplier += (extent/100)
else:
    brightness_multiplier -= (extent/100)



#for each pixel, append the brightened or darkened version to the new image list
for pixel in pixels:
    new_pixel = (int(pixel[0] * brightness_multiplier),
                 int(pixel[1] * brightness_multiplier),
                 int(pixel[2] * brightness_multiplier))

    #check the new pixel values are within rgb range
    new_pixel= [max(min(channel, 255), 0) for channel in new_pixel]

    new_image_list.append(new_pixel)



#save the new image
new_image.putdata(new_image_list)
new_image.save('colour_brightness.jpg')

cv2.waitKey(0)
cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:1)

new_pixel= [max(min(channel, 255), 0) for channel in new_pixel]
new_image_list.append(new_pixel)

您要将列表附加到new_image_list,但您应该添加元组。

new_pixel= [max(min(channel, 255), 0) for channel in new_pixel]
new_image_list.append(tuple(new_pixel))