简体   繁体   English

Python - 更改像素值

[英]Python - change pixel value

I have used this code to change the pixel value of image,我用这段代码来改变图像的像素值,

im=Image.open("image.jpg")
pix=im.load()
pix[50,50]=(70,70,70)

it is work well with me, but when I use a png image everything is okay and the changes are saved to the image, but when I use a JPG image the changes are not saved to the image.它对我来说很好用,但是当我使用 png 图像时一切正常,更改会保存到图像中,但是当我使用 JPG 图像时,更改不会保存到图像中。 Is there something missing in the code, do I need to save the changes , and how would I do ??代码中是否缺少某些内容,我是否需要保存更改,我该怎么办?

Try with:尝试:

im.putpixel((50,50), (70, 70, 70))

or you can also do:或者你也可以这样做:

import numpy as np
from PIL import Image

# pix has 4 channels for png and 3 for jpg
pix = np.array(im)

pix[50, 50, 0] = 70      # 0 accesses the first channel
pix[50, 50, 1] = 70      # 1 accesses the second channel
pix[50, 50, 2] = 70      # 2 accesses the third channel

Image.fromarray(pix).save('new_img.jpg')

Save the image with the previous file name.用以前的文件名保存图像。 That worked for me well这对我很有效

im=Image.open("image.jpg")
pix=im.load()
pix[50,50]=(70,70,70)
img.save("image.jpg")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM