📜  如何使用Python操作图像的像素值?

📅  最后修改于: 2022-05-13 01:54:58.266000             🧑  作者: Mango

如何使用Python操作图像的像素值?

众所周知,彩色图像是各种像素的集合。如果我们改变像素值,图像将变成不同颜色的图像。手动执行这项繁琐的任务非常糟糕,因为图像可能包含数百万个像素。因此,我们将编写一个Python脚本来轻松完成此任务。

在开发图像数据的预测模型时,我们有时需要操作图像。为此, Python有一个令人惊叹的库,名为Python Imaging Library(PIL) 这个库包含一些我们可以提取图像像素图的方法,并且简单地在循环的帮助下,我们可以迭代它的每个像素并根据我们的需要改变它的像素值。像素是图像的最小基本成分,类似地,像素图可以被认为是表示图像的像素矩阵。

方法

  1. 首先,我们需要一个图像文件作为输入。这个图像文件可以通过Image.new() 方法或通过Image.open()从本地机器导入 方法这两种情况都已在下面的示例中显示。 (不是强制性的,但为了方便起见,我们将图像以“input.png”的名称保存,特别是为了查看差异。)
  2. 其次,我们需要在Image.load() 方法的帮助下提取输入图像的像素图(像素值矩阵),以便我们可以操纵我们想要的像素。 Image.size方法返回图像(像素图或矩阵)的宽度和高度(列和行)。然后在循环的帮助下,我们将迭代并更改我们想要的像素值。
  3. 最后,在更新或更改像素值后,我们将获得输出图像。 (同样不是强制性的,但为了方便起见,我们将在Image.save()方法的帮助下以“output.png”的名称保存输出图像。 我们还可以使用Image.show()方法在输出屏幕上看到图像

示例1:使用本地机器上的一张图片,将其一半变成灰度图

将图像转换为灰度图像的平均公式:

G = (R+G+B) / 3

上面的公式理论上是正确的,但一个更改进的公式(加权法,也称为光度法,根据波长对红、绿、蓝进行加权)如下:



G = (0.299R + 0.587G + 0.114B)

输入图像:

输入图像

Python3
from PIL import Image
  
# Import an image from directory:
input_image = Image.open("gfg.png")
  
# Extracting pixel map:
pixel_map = input_image.load()
  
# Extracting the width and height 
# of the image:
width, height = input_image.size
  
# taking half of the width:
for i in range(width//2):
    for j in range(height):
        
        # getting the RGB pixel value.
        r, g, b, p = input_image.getpixel((i, j))
          
        # Apply formula of grayscale:
        grayscale = (0.299*r + 0.587*g + 0.114*b)
  
        # setting the pixel value.
        pixel_map[i, j] = (int(grayscale), int(grayscale), int(grayscale))
  
# Saving the final output
# as "grayscale.png":
input_image.save("grayscale", format="png")
  
# use input_image.show() to see the image on the
# output screen.


Python3
from PIL import Image
  
# Create an image as input:
input_image = Image.new(mode="RGB", size=(400, 400),
                        color="blue")
  
# save the image as "input.png"
#(not mandatory)
input_image.save("input", format="png")
  
# Extracting pixel map:
pixel_map = input_image.load()
  
# Extracting the width and height
# of the image:
width, height = input_image.size
z = 100
for i in range(width):
    for j in range(height):
        
        # the following if part will create
        # a square with color orange
        if((i >= z and i <= width-z) and (j >= z and j <= height-z)):
            
            # RGB value of orange.
            pixel_map[i, j] = (255, 165, 0)
  
        # the following else part will fill the
        # rest part with color light salmon.
        else:
            
            # RGB value of light salmon.
            pixel_map[i, j] = (255, 160, 122)
  
# The following loop will create a cross
# of color blue.
for i in range(width):
    
    # RGB value of Blue.
    pixel_map[i, i] = (0, 0, 255)
    pixel_map[i, width-i-1] = (0, 0, 255)
  
# Saving the final output
# as "output.png":
input_image.save("output", format="png")
  
# use input_image.show() to see the image on the
# output screen.


输出:

输出图像。

注意:这里图像的一半已转换为灰度,但只需将(width//2)更改为 (width)即可使用相同的代码完成整个图像。要了解有关此的更多信息,请参阅Image.getpixel()方法。

示例 2:操作像素值

输入图像:

输入图像。

蟒蛇3

from PIL import Image
  
# Create an image as input:
input_image = Image.new(mode="RGB", size=(400, 400),
                        color="blue")
  
# save the image as "input.png"
#(not mandatory)
input_image.save("input", format="png")
  
# Extracting pixel map:
pixel_map = input_image.load()
  
# Extracting the width and height
# of the image:
width, height = input_image.size
z = 100
for i in range(width):
    for j in range(height):
        
        # the following if part will create
        # a square with color orange
        if((i >= z and i <= width-z) and (j >= z and j <= height-z)):
            
            # RGB value of orange.
            pixel_map[i, j] = (255, 165, 0)
  
        # the following else part will fill the
        # rest part with color light salmon.
        else:
            
            # RGB value of light salmon.
            pixel_map[i, j] = (255, 160, 122)
  
# The following loop will create a cross
# of color blue.
for i in range(width):
    
    # RGB value of Blue.
    pixel_map[i, i] = (0, 0, 255)
    pixel_map[i, width-i-1] = (0, 0, 255)
  
# Saving the final output
# as "output.png":
input_image.save("output", format="png")
  
# use input_image.show() to see the image on the
# output screen.

输出:

输出图像