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

How to deal with picture size scaling function in batch by Python

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains the "Python how to batch processing picture size zoom function", 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 "Python how to batch deal with picture size scaling function" it!

The code is as follows

/ / python codeimport osimport cv2''' sets the image path, which contains 14 photos in jpg format, whose names are 0.jpg, 1.jpg, 2.jpgMagnee. Reagle 14.jpgpicture pictures DatadIR = "D:\ Code\ ToolBox"''set the target pixel size Set here to 300 'generate path' using the join method of the os.path module''path=os.path.join (DATADIR)' using the os.listdir (path) function to return the names of all files under the path path, as well as the name of the folder, for example, after executing the following code, img_list is a list The value is ['0.jpgZhen1. JpgEncyclopedia 10.jpgZhen1]. The value is [' 0.jpgZhen1. "10.jpgEng" 11.jpgEng "12.jpgZhi" 13.jpgZhi "14.jpgLiao" 2.jpgLiao "3.jpgLiao" 4.jpgLiao, "5.jpgLiao", "6.jpgLiao", "7.jpgLianling" 8.jpgforth, "9.jpgLay"] Note that this order is not arranged in the order from small to large.''img_list=os.listdir (path) ind=0for I in img_list:''' calls cv2.imread to read the picture. The read format is IMREAD_COLOR'''img_array=cv2.imread (os.path.join (path,i), cv2.IMREAD_COLOR)''calling the cv2.resize function resize Picture' new_array=cv2.resize (img_array, (IMG_SIZE) IMG_SIZE) img_name=str (ind) + '.jpg' generate the target path for image storage 'save_path='D:\\ Code\\ resized\\' + str (ind) + '.jpg'ind=ind+1''' calls the imwrite function of cv.2 to save the picture' 'cv2.imwrite (save_path,new_array)

Found that the online batch jpg to png code in the folder full of images, in the rename when the system can not find the specified file problem.

Import os' image path to rename''file_path = "pngpairdataset/test" files = os.listdir (file_path) for file in files:if file.endswith (' jpg'): # specify the renamed path src = os.path.join (file_path, file) r_name = file.split ('.') [0] +. Png'dct = os.path.join (file_path, r_name) os.rename (src, dct) Thank you for reading The above is the content of "Python how to batch deal with picture size scaling function". After the study of this article, I believe you have a deeper understanding of how Python deals with picture size scaling function in batches, and the specific use needs 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: 0

*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