Created
October 31, 2024 08:24
-
-
Save gyillikci/c88c96d21f51daad8b19fd3de9b71031 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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