Skip to content

Instantly share code, notes, and snippets.

@gyillikci
Created October 31, 2024 08:24
Show Gist options
  • Save gyillikci/c88c96d21f51daad8b19fd3de9b71031 to your computer and use it in GitHub Desktop.
Save gyillikci/c88c96d21f51daad8b19fd3de9b71031 to your computer and use it in GitHub Desktop.
import cv2
import os
def resize_images_in_directory(input_dir, output_dir, width, height):
# Create the output directory if it doesn't exist
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Iterate over all files in the input directory
for filename in os.listdir(input_dir):
# Check if the file is an image
if filename.endswith(('.png', '.jpg', '.jpeg', '.bmp', '.tiff')):
# Read the image
img = cv2.imread(os.path.join(input_dir, filename))
# Resize the image
resized_img = cv2.resize(img, (width, height))
# Save the resized image to the output directory
cv2.imwrite(os.path.join(output_dir, filename), resized_img)
print(f"Resized and saved {filename}")
# Example usage
input_directory = 'input_images'
output_directory = 'output_images'
new_width = 800
new_height = 600
resize_images_in_directory(input_directory, output_directory, new_width, new_height)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment