![]() Width_size = int((float(image.size) * float(height_percent))) Height_percent = (fixed_height / float(image.size)) Path = 'C:/Users/techverse/Desktop/images/'Įxport_path = 'C:/Users/techverse/Desktop/images/resized/' So here’s a small yet powerful piece of code to help you resize and compress images with python. However, I won’t recommend setting the quality too low or the output image won’t look good. If you want higher compression, you can reduce the quality value. You can change the output format from webp to any other image format of your choice. In the below code, you can adjust the path, export_path, and fixed_height variable according to your requirements. Also don’t forget to create the export path before running the code. You can choose the format according to your requirements. pip install pillowįor this guide, I will be choosing the webp format. How to Bulk Resize and Compress Images with Python and Pillowīefore we proceed ahead with resizing the images, let’s make sure you have Python 3 installed and then install the Pillow library for python using the following command. It is commonly used to resize and compress images. It is a powerful tool for image archiving and batch processing applications. Pillow supports more than 30 of the most popular image formats available right now. Python has this incredibly useful Pillow library for image processing. After looking around for possible solutions, I zeroed in on python and the Pillow library. It wasn’t going to be an easy job resizing all these jpeg images. The problem was, this site had more than 20GB of images. Google’s PageSpeed tool was suggesting me to serve optimized images on the website to decrease the page loading speed. One of my websites was suffering due to unoptimized images. Bulk resize and compress images with python and pillow. ![]() At the same time, the webp format doesn’t give away with the quality of the image. Thanks to advancements in image compression, we now have image formats such as webp which require very less storage space in comparison to the popular formats of yesteryear such as jpeg and png. Generally, images have to be optimized through resizing and compression to save bandwidth both for the webserver and viewer. The main reason being they had too many unoptimized images. As a web developer, I have come across websites that performed very poorly in terms of loading speed.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |