zhiqingstudy

Be a young person with knowledge and content

Sometimes, you will encounter the task of modifying the size of pictures in batches. If there are few picture files, you can modify them manually. However, if there is a large number of picture files, manual modification is not only time-consuming and laborious, but also may miss files. At this time, you only need to develop a python file to run, and a large number of picture files can be modified in a few minutes, It greatly improves the work efficiency and reduces the error rate.

So how can we use Python program development to modify the size of pictures in batches, modify the width of pictures to make the height adaptive, or modify the height of pictures to make the width adaptive?

Question: There are a large number of jpg format image files in a file. The size of the image is different. How can I change the width of the image to 500px, height adaptive, or the height of the image to 500px, width adaptive?

Knowledge points investigated: Operation of file path and picture modification

Idea: First obtain the image path in batch, then obtain the width and height of the image, and the aspect ratio (to adapt to the image), then modify the image size, and finally save the file.

Program code:

from pathlib import Path
from PIL import Image #Import path module and image processing module
p=Path(‘C:\\Users\\Administrator\\Desktop\\图片文件’) #Here is the path of the image file. Be sure to pay attention to the file path format
file=p.glob(‘*.jpg’) #Get all jpg images in the file path
for i in file: #For loop to get all image paths
img=Image.open(i) #Open Picture
w,h=img.size #Get the width and height of the picture
scale=w/h #Get the aspect ratio of the picture, which is used to modify the picture adaption
if scale<=1: #If the image width is greater than the height, change the width to 500, and the height is adaptive
h=500
w=h*scale
else: #If the height of the picture is greater than the width, change the height to 500, and the width is adaptive
w=500
h=w/scale
newimg=img.resize((int(w),int(h))) #Assign the modified width and height to a new picture
newimg.save(i) #Save a new file with the same file name and path as the original file name
print(i) #Print a new file name to show that the program is running

After the program runs, you can modify all the jpg format pictures in the folder. The width or height of the pictures are adaptive. However, it should be noted that such modification is based on the original file. After the modification, the original file will be completely overwritten by the new file, so you should make a backup of the original file in time.

comment
head sculpture
Code:
Related

Why you shouldn't stay at a job for more than 2 years?

3 harsh facts long-distance relationships

how to keep your girlfriend interested in a long-distance relationship




Unless otherwise specified, all content on this website is original. If the reprinted content infringes on your rights, please contact the administrator to delete it
Contact Email:2380712278@qq.com

Filing number:皖ICP备19012824号