Python - 更改图像的RGB值并另存为图像

时间:2022-03-18 13:20:59

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.

我已经可以读取每个像素的RGB图像,但我不知道如何将RGB的值更改为一半并保存为图像。请提前告诉您。

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] 

4 个解决方案

#1


0  

You can do everything you are wanting to do within PIL.

您可以在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')

You can see more info about point operations here: https://pillow.readthedocs.io/en/latest/handbook/tutorial.html#point-operations

您可以在此处查看有关点操作的更多信息:https://pillow.readthedocs.io/en/latest/handbook/tutorial.html#point-operations

Additionally, you can do arbitrary pixel manipulation as: im[row, col] = (r, g, b)

此外,您可以执行任意像素操作:im [row,col] =(r,g,b)

#2


1  

There are many ways to do this with Pillow. You can use Image.point, for example.

Pillow有很多方法可以做到这一点。例如,您可以使用Image.point。

# Function to map over each channel (r, g, b) on each pixel in the image
def change_to_a_half(val):
    return val // 2

im = Image.open('./imagefile.jpg')
im.point(change_to_a_half)

The function is actually only called 256 times (assuming 8-bits color depth), and the resulting map is then applied to the pixels. This is much faster than running a nested loop in python.

该函数实际上仅被调用256次(假设8位颜色深度),然后将得到的映射应用于像素。这比在python中运行嵌套循环要快得多。

#3


0  

If you have Numpy and Matplotlib installed, one solution would be to convert your image to a numpy array and then e.g. save the image with matplotlib.

如果您安装了Numpy和Matplotlib,一种解决方案是将您的图像转换为numpy数组,然后例如使用matplotlib保存图像。

import matplotlib.pyplot as plt
import numpy as np
from PIL import Image

img = Image.open(jpg)
arr = np.array(img)
arr = arr/2 # divide each pixel in each channel by two 
plt.imsave('output.png', arr.astype(np.uint8))

Be aware that you need to have a version of PIL >= 1.1.6

请注意,您需要拥有PIL> = 1.1.6的版本

#4


-1  

  • get the RGB color of the pixel

    获取像素的RGB颜色

    [r,g,b]=img.getpixel((x, y))
    
  • update new rgb value

    更新新的rgb值

     r = r + rtint
     g = g + gtint
     b = b + btint
     value = (r,g,b)
    
  • assign new rgb value back to pixel

    将新的rgb值分配回像素

    img.putpixel((x, y), value)
    

#1


0  

You can do everything you are wanting to do within PIL.

您可以在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')

You can see more info about point operations here: https://pillow.readthedocs.io/en/latest/handbook/tutorial.html#point-operations

您可以在此处查看有关点操作的更多信息:https://pillow.readthedocs.io/en/latest/handbook/tutorial.html#point-operations

Additionally, you can do arbitrary pixel manipulation as: im[row, col] = (r, g, b)

此外,您可以执行任意像素操作:im [row,col] =(r,g,b)

#2


1  

There are many ways to do this with Pillow. You can use Image.point, for example.

Pillow有很多方法可以做到这一点。例如,您可以使用Image.point。

# Function to map over each channel (r, g, b) on each pixel in the image
def change_to_a_half(val):
    return val // 2

im = Image.open('./imagefile.jpg')
im.point(change_to_a_half)

The function is actually only called 256 times (assuming 8-bits color depth), and the resulting map is then applied to the pixels. This is much faster than running a nested loop in python.

该函数实际上仅被调用256次(假设8位颜色深度),然后将得到的映射应用于像素。这比在python中运行嵌套循环要快得多。

#3


0  

If you have Numpy and Matplotlib installed, one solution would be to convert your image to a numpy array and then e.g. save the image with matplotlib.

如果您安装了Numpy和Matplotlib,一种解决方案是将您的图像转换为numpy数组,然后例如使用matplotlib保存图像。

import matplotlib.pyplot as plt
import numpy as np
from PIL import Image

img = Image.open(jpg)
arr = np.array(img)
arr = arr/2 # divide each pixel in each channel by two 
plt.imsave('output.png', arr.astype(np.uint8))

Be aware that you need to have a version of PIL >= 1.1.6

请注意,您需要拥有PIL> = 1.1.6的版本

#4


-1  

  • get the RGB color of the pixel

    获取像素的RGB颜色

    [r,g,b]=img.getpixel((x, y))
    
  • update new rgb value

    更新新的rgb值

     r = r + rtint
     g = g + gtint
     b = b + btint
     value = (r,g,b)
    
  • assign new rgb value back to pixel

    将新的rgb值分配回像素

    img.putpixel((x, y), value)