Python-更改图像的RGB值并另存为图像 [英] Python -- change the RGB values of the image and save as a image
问题描述
我已经可以读取图像的每个像素的RGB,但是我不知道如何将RGB的值更改为一半并另存为图像.谢谢.
I can read every pixel' RGB of the image already, but I don't know how to change the values of RGB to a half and save as a image.Thank you in advance.
from PIL import *
def half_pixel(jpg):
im=Image.open(jpg)
img=im.load()
print(im.size)
[xs,ys]=im.size #width*height
# Examine every pixel in im
for x in range(0,xs):
for y in range(0,ys):
#get the RGB color of the pixel
[r,g,b]=img[x,y]
推荐答案
您可以在PIL中做所有想做的事情.
You can do everything you are wanting to do within PIL.
如果要将每个像素的值减少一半,可以执行以下操作:
If you are wanting to reduce the value of every pixel by half, you can do something like:
import PIL
im = PIL.Image.open('input_filename.jpg')
im.point(lambda x: x * .5)
im.save('output_filename.jpg')
您可以在此处查看有关点操作的更多信息: https ://pillow.readthedocs.io/zh-CN/latest/handbook/tutorial.html#point-operations
You can see more info about point operations here: https://pillow.readthedocs.io/en/latest/handbook/tutorial.html#point-operations
此外,您还可以执行以下任意像素操作:
im[row, col] = (r, g, b)
Additionally, you can do arbitrary pixel manipulation as:
im[row, col] = (r, g, b)
这篇关于Python-更改图像的RGB值并另存为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!