为什么PIL中出现"ValueError:图像为只读" [英] Why is ''ValueError: image is readonly'' in PIL
本文介绍了为什么PIL中出现"ValueError:图像为只读"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想更改pgm灰度图像中的像素.当我编译以下代码时,它显示图像是只读的.我无法更改图像的像素.如何解决此错误?
这是我的代码:
I want to change pixel in a grayscale pgm image. When I compile the following code it shows image is read-only. I can not change the pixel of the image. How can I fix this error?
Here are my codes:
from PIL import Image
img = Image.open('Image.pgm')
pixval= img.load()
columnsize, rowsize = img.size
img1 = Image.open('Image.pgm')
pix1 = img1.load()
for i in range(rowsize):
for j in range(columnsize):
pix1[j,i]=250
img1.save("share1.pgm")
推荐答案
要更改像素,请使用以下API
In order to change a pixel, use the following API
image.putpixel((j, i), 250)
尤其是,您的代码将变为
In particular, your code becomes
from PIL import Image
img = Image.open('Image.pgm')
pixval = img.load()
columnsize, rowsize = img.size
for i in range(rowsize):
for j in range(columnsize):
image.putpixel((j, i), 250)
img1.save("share1.pgm")
这篇关于为什么PIL中出现"ValueError:图像为只读"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文