give me a python script that takes input folder path as static then downscale every…
- User
give me a python script that takes input folder path as static
then downscale every image in that folder with 50% and save as best quality jpg and overwrite image
- Assistant
Here is a Python script that uses the Pillow library to perform the task described:
import os from PIL import Image def downscale_images_in_folder(folder_path): for filename in os.listdir(folder_path): if filename.endswith(".jpg") or filename.endswith(".png"): image_path = os.path.join(folder_path, filename) with Image.open(image_path) as img: width, height = img.size new_img = img.resize((width//2, height//2), Image.ANTIALIAS) new_img.save(image_path, "JPEG", quality=95) print("Downscaled: ", filename) folder_path = 'your_folder_path_here' # replace with your folder path downscale_images_in_folder(folder_path)This script will open every .jpg and .png file in the given folder, downscale it by 50%, save it as a high-quality jpeg, and overwrite the original file. Replace 'your_folder_path_here' with the path to the folder you want to process.
Please note that this script will overwrite the original images, make sure you have backups if you need the original images.
Before running the script, ensure you have the Pillow library installed in your Python environment. If not, install it using pip:
pip install pillowPlease be careful when using this, especially if you are downsizing important images. Make sure to have a backup before you perform downsizing and overwriting.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.