The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Batch convert images to grayscale

To batch convert images to grayscale, you can use Python with the Pillow library, which makes it simple and efficient. Below is a Python script that takes all images from a source folder, converts them to grayscale, and saves them to a destination folder.

Requirements:

  • Python installed on your machine

  • Pillow library (pip install Pillow)

Python Script:

python
import os from PIL import Image # Set source and destination directories source_dir = 'path/to/source/folder' dest_dir = 'path/to/destination/folder' # Create destination directory if it doesn't exist os.makedirs(dest_dir, exist_ok=True) # Loop through all files in the source directory for filename in os.listdir(source_dir): if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.tiff')): img_path = os.path.join(source_dir, filename) img = Image.open(img_path).convert('L') # Convert to grayscale save_path = os.path.join(dest_dir, filename) img.save(save_path) print("Batch conversion complete.")

Notes:

  • Replace 'path/to/source/folder' and 'path/to/destination/folder' with your actual folder paths.

  • This script supports common image formats.

  • .convert('L') is the key function to turn an image grayscale.

Let me know if you want this done with a GUI, shell script, or another programming language.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About