Last active
November 14, 2024 06:37
-
-
Save prashant3285/5ea494ba6e4c78016b7b715a727c11a5 to your computer and use it in GitHub Desktop.
PNG/JPEG image resize(600 x 600px) and convert to AVIF
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
| # pip install pillow | |
| # pip install pillow-avif-plugin | |
| # add square large size files JPG and PNG in Input folder and run the script | |
| # converted files will be stored in output folder | |
| # change folder path at the end of file | |
| import os | |
| from PIL import Image | |
| import glob | |
| import pillow_avif | |
| print("Script Triggered") | |
| def resize_and_convert(input_folder, output_folder, target_size=(600, 600)): | |
| # Ensure the output folder exists | |
| if not os.path.exists(output_folder): | |
| os.makedirs(output_folder) | |
| # Get all image files (png, jpg, jpeg) in the input folder | |
| image_files = glob.glob(os.path.join(input_folder, "*.[pj]*[np]*[g]*")) # Includes .png, .jpg, .jpeg | |
| # Loop through all image files | |
| for file_path in image_files: | |
| try: | |
| # Open the image | |
| with Image.open(file_path) as img: | |
| # Resize the image to 600x600 | |
| img_resized = img.resize(target_size) | |
| # Define the output path (change extension to .avif) | |
| file_name = os.path.splitext(os.path.basename(file_path))[0] + ".avif" | |
| output_path = os.path.join(output_folder, file_name) | |
| # Save the image as AVIF format | |
| img_resized.save(output_path, format="AVIF") | |
| print(f"Converted {file_path} to {output_path}") | |
| except Exception as e: | |
| print(f"Error processing {file_path}: {e}") | |
| if __name__ == "__main__": | |
| input_folder = "C:/Users/Username/Desktop/Input" # Replace with your input folder path | |
| output_folder = "C:/Users/Username/Desktop/Output" # Replace with your output folder path | |
| resize_and_convert(input_folder, output_folder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment