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.
from PIL import Image
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')
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))
new_image = Image.new('RGB', (width, height))
load()
method of the PIL library to get the pixel data of the original image and save it to a list. 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')
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')
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!