python - PIL简单渐变和TypeError : an integer is required

标签 python python-imaging-library

我看过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/

相关文章:

python - 直方图操作以删除不需要的数据

python速度处理每行VS block

python - 在 while 循环中更改 tkinter Canvas 中的图像

python - 提高 getpixel 和 putpixel 的速度

python - 是否可以从 scrapys 调度程序队列中删除请求?

python - 将 Tensorflow 与多处理一起使用时无法腌制 'weakref' 对象

python - 来自单独数组中纬度/经度坐标的(纬度,经度)的所有可能点

python - 重新采样表示图像的 numpy 数组

python - PIL 剪贴板图像到 Base64 字符串

python - PIL/Pillow 看似任意反转透明度