Это видео недоступно.
Сожалеем об этом.
How to Bulk Resize Images Using Python
HTML-код
- Опубликовано: 4 июл 2023
- How to Bulk Resize Images Using Python
Greetings, in this Python tutorial we shall be looking at how to mass/bulk resize images. This is a really useful Python program to make. This will preserve folder structure for the images you want to resize, making this perfect for bulk resizing images you have organised.
I use this program at my day job as it can save a lot of time for optimising a game when we are in the final stages of production.
This bulk image resizing program requires the Pillow (PIL) module. This can be installed with pip install pillow
The pillow (PIL) module allows us to mass resize images. The os library allows us to keep the same folder structure the the images we resized using Python.
Thanks for watching this Pyhton tutorial on how to bulk/mass resize images.
Subscribe to keep notified when I upload: tinyurl.com/Su...
How to Bulk Resize Images Using Python
from PIL import Image
import os
input_dir = "input"
output_dir = "output"
def resize_image(image_path, output_folder, factor):
try:
with Image.open(image_path) as img:
new_size=(int(img.size[0]*factor),int(img.size[1]* factor))
output_path=os.path.join(output_folder, os.path.basename(image_path))
img.save(output_path)
except Exception as e:
print(f"Error resizing image{image_path}: {e}")
def create_output_directory(path):
if not os.path.exists(path):
os.makedirs(path)
def get_output_directory(input_directory):
relative_path = os.path.relpath(input_directory, input_dir)
return os.path.join(output_dir, relative_path)
for subdir, dirs, files in os.walk(input_dir):
for file in files:
if file.endswith(".JPG"):
input_file_path = os.path.join(subdir, file)
output_directory=get_output_directory(subdir)
create_output_directory(output_directory)
output_file_path=os.path.join(output_directory, file)
if not os.path.exists(output_file_path):
resize_image(input_file_path, output_directory, 0.5)