Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method for python batch to enlarge a picture?

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "python batch to enlarge the picture of what is the method", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "what is the method of python batch to enlarge the picture?"

Python batch picture magnification

Sometimes for network recognition, putting the original picture into the network can not achieve the desired effect, but sometimes if the picture is enlarged, the recognition rate can achieve unexpected results. Now provide a kind of code to batch enlarge the pictures in the file:

Import osfrom PIL import Imageimport sys # get all the files in the path directory def get_imlist (path): return [os.path.join (path) F) for f in os.listdir (path)] def change_size (path): directorys=get_imlist (path) for directory in directorys: # skip print (directory) if not if not a picture file (directory.endswith ('.jpg') or directory.endswith ('.png') or directory.endswith ('.bmp')): # # b pass else: Img=Image.open (directory) s = "/" # get the file name (including suffix) oimage_name= directory [directory.rfind (s) + 1:] (oimage_width Oimage_height) = img.size new_width=oimage_width * 3 new_height=oimage_height * 3 out=img.resize ((new_width,new_height), Image.ANTIALIAS) out.save ("% s"% oimage_name) # directly replace if _ _ name__ = ='_ _ main__': change_size ("F:\ desktop\\ test") python image scaling, batch processing (step by step)

Problem: realize the proportional scaling of the picture of pixel naughn to 30cm 30

0. Environmental requirements: Python3.x needs to install pillow module:

Pip install pillow

1. Zoom a picture:

From PIL import Image img = Image.open ("2.png") # read the picture out = img.resize ((30Magne30)) # scale to 30mm 30 pixels out.save ("2.png") # Save the picture (overwrite the original image)

2. Batch processing:

/ * directory structure E:\ data_eg\ lip_train:-folder1:-pic1.png-pic2.png-pic3.png-folder2: -pic1.png-pic2.png-pic3.png-pic4.png-pic5.png-folder3: -pic1.png-pic2.png*/ import osfrom PIL import Image path_target = "E:\ data_eg\ lip_train" # folder under the liptrain path Under the folder are multiple pictures path_list=os.walk (path_target) # use the os.walk () method to traverse all paths File # filenames list# root save all picture names secondary directory for root, dirs, filenames in path_list: for filename in filenames: # iterate through the list file_path = os.path.join (root) containing all file names Filename) # synthesize the absolute path of each picture print (file_path) # print the absolute path to the screen img = Image.open (file_path) # Open the picture with this absolute path out = img.resize ((30Magne30)) # scale to 30mm 30 pixels # out = img.convert ('L') # convert to grayscale image out.save (file_path) # Save Coverage of the original picture thank you for reading, the above is the "python batch to enlarge the picture of what is the method" of the content, after the study of this article, I believe you batch python will be enlarged picture of what this method is a more profound understanding of the specific use of the situation also need to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 229

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report