In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use Python code to compress 500 pictures in batches, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
These days, I am using Github page + hexo to build a personal website. In order to continue the style, I want to migrate the pictures with the article. At this time, there is a problem: the pictures I use are all high-definition images, which will seriously slow down the loading speed if you put them on the website. Therefore, you need to compress the picture before uploading it.
I summarize the requirements as follows:
There are about 200 images in batch compression, which will be added later.
It's compression, not cutting, not changing the size of the picture.
Most of the original images are 10m-30m, and the goal is to compress them into less than 1m. The smaller the better.
According to these clues, I search for "batch compressed pictures", "picture compression tool", "batch processing pictures".
The idea at first is to find a lightweight image compression tool and simply deal with it. However, I do not know whether the search posture is wrong, or the posture of filtering information is wrong, the results are not satisfactory.
The tools found are both local and online, but they are not ideal after testing: some software is found to be paid only after download, some directly lead to program jam when in use, some compression ratio is not enough to need multiple compression, some require that the size of the original picture should not exceed 5m, some require batch processing no more than 20, and some do not support batch compression. Friends in the group also helped recommend "PS+ batch", acdsee, and even mobile app snapseed, which didn't agree with me.
After spending a lot of time, I came across an article that used Python to compress the picture. Why didn't I think of it when I woke up the dreamer?
PIL is a powerful image processing library, but only supports Python 2, and has long stopped updating. Some developers have improved and maintained Pillow based on it to support Python 3. I've heard of it before, but I haven't used it. So I checked several tutorials and documents. After reading it, I found that the main way to compress the picture is to scale, cut and change the format, which is not what I want.
Finally, I finally found a very convenient and satisfying solution, and let's get down to business. (don't disrelish me for being verbose, the process of exploration is also very interesting.) Shh, in fact, it is only a few lines of code because the method to be introduced below is too simple. I really can't help but force it. )
-A careful dividing line-
Tinypng website provides online image compression service, which is one of the best use of all image compression tools, but there are restrictions: a maximum of 20 pieces can be processed in batches, and the size of each piece is not allowed to exceed 5m.
The site is so conscientious that it opens up free API, and API removes the size limit of each image, limiting it to 500 images per month. This is more than enough for me.
Here's how to use it. The first step is to register on its website and get your own API_KEY. The use of mailbox registration is very simple.
Then install package:
Pip install-upgrade tinify
Then there is the processing of the picture:
Import tinify
Import os
Tinify.key = 'enter your key' here
Path = "C:\\ Users\\ yunpoyue\\ Pictures\\ cat" # the path where the picture is stored
For dirpath, dirs, files in os.walk (path):
For file in files:
Imgpath = os.path.join (dirpath, file)
Print ("compressing..." + imgpath)
Tinify.from_file (imgpath) to_file (imgpath)
Less than 10 lines of code, easily compress pictures in batches, simply don't be too cool! Images of 20 M can be compressed to 2 M, with a compression ratio of an astonishing 90%, with gratifying results.
Its API also provides image clipping, watermarking, saving compressed images to cloud service providers (Amazon Cloud, Google Cloud) and other functions, very powerful. Except that the compression process is a little slow, the other is impeccable.
After some exploration and comparison, I am sure that this is the best plan at present, so I strongly share it with you.
The above is how to use Python code to compress 500 pictures in batches. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow 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.