How to pixel fill an image using Python

WBOY
Release: 2023-08-18 12:01:43
Original
1449 people have browsed it

How to pixel fill an image using Python

How to use Python to perform pixel filling on pictures

Introduction:
In image processing, pixel filling is a common technique used to change the pixel values, thereby modifying and enhancing the image. As a powerful programming language, Python also has rich libraries and tools for image processing. This article will introduce how to use Python to fill pixels in images and provide code examples.

  1. Import the necessary libraries
    Before we begin, we need to import the PIL library, which is one of the libraries commonly used in Python image processing. By using PIL library we can easily read, modify and save images.
from PIL import Image
Copy after login
  1. Open the image file
    Use the open() function of the PIL library to open the image file and assign it to a variable for subsequent processing. .
image = Image.open('image.jpg')
Copy after login
  1. Get image information
    Through the size attribute of the PIL library, we can get the width and height of the image and print it out.
width, height = image.size
print('图像宽度:%d,图像高度:%d' % (width, height))
Copy after login
  1. Create a new image
    Next, we create a new image object and set its size to be the same as the original image.
new_image = Image.new('RGB', (width, height))
Copy after login
  1. Get the pixel data of the original image
    Use the load() method of the PIL library to get the pixel data of the original image and save it to a list.
pixels = image.load()
Copy after login
  1. Fill each pixel
    By traversing each pixel of the original image, we can fill it. In this example, we perform a simple color fill on each pixel, setting its RGB values ​​to red, green, and blue respectively.
for i in range(width):
    for j in range(height):
        new_image.putpixel((i, j), (255, 0, 0))  # 红色填充
Copy after login
  1. Save New Image
    Finally, we save the filled image to a new file.
new_image.save('new_image.jpg')
Copy after login

Complete code:

from PIL import Image

image = Image.open('image.jpg')
width, height = image.size
print('图像宽度:%d,图像高度:%d' % (width, height))

new_image = Image.new('RGB', (width, height))
pixels = image.load()

for i in range(width):
    for j in range(height):
        new_image.putpixel((i, j), (255, 0, 0))  # 红色填充

new_image.save('new_image.jpg')
Copy after login

Conclusion:
Through the above steps, we have learned how to use Python to fill pixels in images. In fact, during the pixel filling process, we can perform various processing on the image according to our needs to achieve different effects. I hope this article will be helpful to your learning and practice in image processing.

The above is the detailed content of How to pixel fill an image using Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template