Image Processing Automation with Python (Resize, Watermark, Convert)
If you work with images regularly, automating the repetitive tasks saves hours.
Image Automation Tools
from PIL import Image
class ImageProcessor:
@staticmethod
def resize(input_path, output_path, width=None, height=None):
img = Image.open(input_path)
if width and height:
img = img.resize((width, height), Image.LANCZOS)
elif width:
r = width / img.width
img = img.resize((width, int(img.height * r)), Image.LANCZOS)
elif height:
r = height / img.height
img = img.resize((int(img.width * r), height), Image.LANCZOS)
img.save(output_path)
@staticmethod
def batch_resize(input_dir, output_dir, width):
import os; os.makedirs(output_dir, exist_ok=True)
for f in os.listdir(input_dir):
if f.lower().endswith(('.png', '.jpg', '.jpeg')):
ImageProcessor.resize(
os.path.join(input_dir, f),
os.path.join(output_dir, f),
width=width
)
Features
- Batch resize images
- Add watermarks
- Convert between formats
- Compress for web
- Create thumbnails
Follow for Python automation tutorials!
Top comments (0)