In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor to share with you how to use Python batch zoom pictures of the relevant knowledge, detailed content, clear logic, I believe that most people are too aware of this knowledge, so share this article for your reference, I hope you will learn something after reading this article, let's take a look at it.
Guide reading
In order to solve the problem, after the picture is resized, the ratio remains the same (so that it will not be deformed)
The picture needs to be compressed, so the problem mentioned above can be solved by using the resize function in the Image module to prevent the word file from being too large. Let's first get the full paths of all the pictures under the pictures folder in the order of "modification time".
Actual demonstration phase
What we need to do is change figure 1 into figure 2. Figure 1:
Figure 2:
Make a proportional reduction.
# sort the pictures by modification time (so as to be consistent with the description of the pictures), and store the paths in the list, so that when inserting the pictures one by one later, call import ospath = "pictures" list_p = [path+ "\\" + i for i in os.listdir (path)] # to get the file names of the pictures and splice the full path list_p.sort (key=lambda path: os.path.getmtime (path)) # to sort the files in the list by their modification time. The os.path.getmtime () function is used to get the last modification time of the file. All the image paths stored in list_p are 'pictures\\ z.jpgtrees,' pictures\\ x.jpgpaths, 'pictures\\ w.jpgtrees,' pictures\\ y.jpgtrees, 'pictures\\ orange-flowers-2471898.jpg',' pictures\\ lime-631389.jpg', 'pictures\\ v.jpg`
Then import the Image module. The for loop traverses the paths of all the pictures, and then reads the image size, x corresponds to width, y corresponds to height. The units here are pixels. The key parameter for compressing the image is the compressed standard width X. the cell width of the picture placed in our Word template is 6.25cm. The resolution of the compressed image is 96, and when the pixel is 230, the corresponding length is 6.1 cm. For the above conversion, please refer to the link www.gaitubao.com/tools/pixel. . However, after being compressed by 230pixel width and automatically populated into the Word document, the width of the image has become 8cm, which has exceeded the width of the cell. Through debugging, the standard width is finally set to 165pixel. After filling in the word, the width is 5.82cm, which is more suitable for the cell width. This difference may be caused by the inaccuracy of the conversion. After the standard width is determined, the height is determined according to the standard width multiplied by the original aspect ratio, so that the proportional compression is maintained and the image will not be deformed. Compress the resize and save it to a new folder.
# reduce the size of the picture proportionally from PIL import Imagefor infile in list_p: im = Image.open (infile) (xPowery) = im.size # read the size of the picture (in pixels) x = 165.Definitions the reduced standard width out = int (y * x / x) # calculate the reduced height based on the standard width out = im.resize ((xerox), Image.ANTIALIAS) # change the size Maintain high quality out.save ('pictures_new\ {}' .format (infile.split ("\\") [- 1]))
In a few simple steps, we got the desired result and compressed the image into:
Before compression:
Results:
Introduce the library in detail
The above introduction is not very simple, but it is only a function of PIL. Let's introduce it in detail below.
Python Imaging Library (PIL) adds image processing ability to Python.
This library provides a wide range of file format support, efficient internal presentation, and very powerful image processing capabilities.
Let's take a look at several possible uses of this library:
1 Image storage
PIL is designed for image archiving and batch processing, and you can use it to create thumbnails, convert formats, print pictures, and so on. The current version can verify and read a large number of image formats. Writes are intentionally designed to write only to commonly used file formats.
2 the image shows that the current version includes Tk PhotoImage and BitmapImage interfaces, as well as Windows DIB interface, which is helpful for use under Windows. To facilitate testing, the show () method is also provided to save the image to disk and display it.
3 Image processing this library contains basic image processing functions, including point manipulation, filtering using the built-in convolution kernel, and color space conversion. This library also supports changing image size, rotation, and free transformation. There is a histogram method that allows you to count images, which can be used for contrast enhancement and global statistical analysis.
These are all the contents of the article "how to use Python to scale pictures in batch". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.