我看过numpy-->PIL int type issue , 但它没有回答我更简单的问题,因为它不使用 numpy
.考虑这个例子:
import Image
import math
img = Image.new('L', (100, 50), 'white')
a = 0.1 # factor
for x in xrange(img.size[0]):
for y in xrange(img.size[1]):
# val: 0 to 255; 255/2 = 127.5;
val = int( 127.5*math.sin(a*y) + 127.5 )
print x, y, val, type(x), type(y), type(val)
img.putpixel((x, y), (val, val, val))
img.save('singrad.png', 'png')
这失败了:
$ python test.py
0 0 127 <type 'int'> <type 'int'> <type 'int'>
Traceback (most recent call last):
File "test.py", line 11, in <module>
img.putpixel((x, y), (val, val, val))
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1267, in putpixel
return self.im.putpixel(xy, value)
TypeError: an integer is required
我不明白什么是“需要整数”,- 考虑到 putpixel
的所有参数报告为 <type 'int'>
?
我如何让它工作?
最佳答案
您正在创建模式为“L”(单个 8 位像素值)的图像,因此您输入的值必须是 < 255 的整数。您正在输入一个需要“RGB”模式的元组。我认为将您的代码更改为 img.putpixl((x,y), val)
可以解决问题。
关于python - PIL简单渐变和TypeError : an integer is required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30779114/