为什么 ''ValueError: image is readonly''在 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")