用 PIL 放置像素会出错
Putting pixels with PIL gives error
这里是新手。
我试着用随机 RGB 颜色放置一些像素(比如添加噪声):
from PIL import Image
import random
img=Image.open('pic.bmp')
randomenter=int(input('Enter numpix: '))
for numpix in range(0, randomenter):
x=random.randint(0,int(img.size[0]))
y=random.randint(0,int(img.size[1]))
r=random.randint(0,255)
g=random.randint(0,255)
b=random.randint(0,255)
img.putpixel((x,y),(r,g,b))
img.show()
与 randomenter=100
有时会起作用。较高的值会给我一个错误:
Traceback (most recent call last):
File "D:\studysem\GiMS\labslaba3.py", line 11, in <module>
img.putpixel((x,y),(r,g,b))
File "C:\Python34\lib\site-packages\pillow-3.3.1-py3.4-win-amd64.egg\PIL\Image.py", line 1512, in putpixel
return self.im.putpixel(xy, value)
IndexError: image index out of range
我做错了什么?
图片 (800, 500)
值
如何发布@ŁukaszRogalsk,它通过将 x
和 y
编辑为 img.size[0]-1
和 img.size[1]-1
来解决
这里是新手。 我试着用随机 RGB 颜色放置一些像素(比如添加噪声):
from PIL import Image
import random
img=Image.open('pic.bmp')
randomenter=int(input('Enter numpix: '))
for numpix in range(0, randomenter):
x=random.randint(0,int(img.size[0]))
y=random.randint(0,int(img.size[1]))
r=random.randint(0,255)
g=random.randint(0,255)
b=random.randint(0,255)
img.putpixel((x,y),(r,g,b))
img.show()
与 randomenter=100
有时会起作用。较高的值会给我一个错误:
Traceback (most recent call last):
File "D:\studysem\GiMS\labslaba3.py", line 11, in <module>
img.putpixel((x,y),(r,g,b))
File "C:\Python34\lib\site-packages\pillow-3.3.1-py3.4-win-amd64.egg\PIL\Image.py", line 1512, in putpixel
return self.im.putpixel(xy, value)
IndexError: image index out of range
我做错了什么?
图片 (800, 500)
值
如何发布@ŁukaszRogalsk,它通过将 x
和 y
编辑为 img.size[0]-1
和 img.size[1]-1