" TypeError:期望的整数参数,浮点数"在PIL

时间:2017-02-04 18:52:11

标签: python python-3.x python-imaging-library

我有程序在这里我在堆栈溢出时遇到了一个问题。它应该调整图像的对比度,但是我得到以下错误:

Traceback (most recent call last):
  File "<string>", line 420, in run_nodebug
  File "<module1>", line 20, in <module>
  File "<module1>", line 16, in change_contrast
  File "C:\EduPython\App\lib\site-packages\PIL\Image.py", line 1512, in putpixel
    return self.im.putpixel(xy, value)
TypeError: integer argument expected, got float

帖子已经很老了所以我不认为写这篇文章的人会看到我的请求,所以我在这里发帖。 这是代码:

from PIL import Image

def change_contrast(img, level):
    def truncate(v):
        return 0 if v < 0 else 255 if v > 255 else v


    img = Image.open("C:\\Users\\omar\\Desktop\\Site\\Images\\obama.png")
    img.load()

    factor = (259 * (level+255)) / (255 * (259-level))
    for x in range(img.size[0]):
        for y in range(img.size[1]):
            color = img.getpixel((x, y))
            new_color = tuple(truncate(factor * (c-128) + 128) for c in color)
            img.putpixel((x, y), new_color)

    return img

result = change_contrast('test_image1.jpg', 128)
result.save('test_image1_output.jpg')
print('done')

1 个答案:

答案 0 :(得分:1)

嗯,什么&#39; truncate

尝试说出您int所在的truncate