为什么 ''ValueError: image is readonly''在 PIL [英] Why is ''ValueError: image is readonly'' in PIL
本文介绍了为什么 ''ValueError: image is readonly''在 PIL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想更改灰度 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)
特别是,你的代码变成
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")
这篇关于为什么 ''ValueError: image is readonly''在 PIL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文