This tutorial will show you how to resize multiple images in a few lines of Python code using the Pillow Python Library PIL.
Assumptions:
- Have a Python environment set up on your PC. If you do not know how to do this, can you check out this video tutorial on setting up a Python environment for Windows OS. Mac and Linux OS comes with Python already installed.
- Have average knowledge of Python codes and how to execute
#import libraries
import os
from PIL import Image
#assign image path to variabe f
#note that file path differs based on OS
f = r'C://Users/xx/Desktop/testimages'
#use os.listdir to read all image in dir
for file in os.listdir(f):
f_img = f+"/"+file
img = Image.open(f_img)
img = img.resize((2296,1724))
img.save(f_img)
To put it succinctly, computation truly delivers immense power.
The Python kernel took roughly 30 seconds to process the code once it was written, which took a few minutes. Similar to spoken languages, writing comparable code may take a little longer at first, but with practice, you get faster and faster.
In conclusion, it would likely take days to complete the same task manually, not to mention the physical and mental exhaustion.
For resizing several images, you can rely on other scripts and programs, however using Python made the process simple and error-free. In addition, scripting Python scripts can be almost zen-like at times and is excellent coding practice. It is definitely something you should try.
You can also sign-up for our Introduction to Python for Data Science course and learn all the basics of Python Programming