How to change the pixel value size of an image in Python?

In Python, you can use the PIL library (Pillow) to resize the pixel values of an image. Here is a simple example code demonstrating how to adjust the pixel values of an image to 50×50 pixels:

First, make sure you have installed the Pillow library. You can install it using the following command:

pip install Pillow

Next, you can use the following code to alter the pixel values of the image:

from PIL import Image

# 打开图片
img = Image.open('image.jpg')

# 调整图片大小为50x50像素
img_resized = img.resize((50, 50))

# 保存调整后的图片
img_resized.save('resized_image.jpg')

print('图片的像素值大小已经调整为50x50像素')

In this example, we begin by opening an image (‘image.jpg’), then we use the resize() method to adjust the pixel size of the image to 50×50 pixels, and save it as a new image (‘resized_image.jpg’).

You can adjust the image path and target pixel size in the code as needed.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds