Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
713 B
1
Indexable
Never
import os
import subprocess
from PIL import Image

# Get the list of image files to process
image_files = ["image1.png", "image2.jpg", "image3.gif"]

# Iterate over the list of image files
for image_file in image_files:
  # Open the image file using PIL
  im = Image.open(image_file)

  # Resize the image by 50%
  im = im.resize((im.width // 2, im.height // 2), Image.ANTIALIAS)

  # Save the resized image to a temporary file
  temp_file = "temp.png"
  im.save(temp_file)

  # Use pngquant to reduce the file size
  output_file = os.path.splitext(image_file)[0] + "pquant.png"
  subprocess.run(["pngquant", "--quality=65-80", temp_file, "-o", output_file])

  # Delete the temporary file
  os.remove(temp_file)