>>11294nothing that a python script can't fix
install python (x64 installer):
https://www.python.org/downloads/windows/install PIL/pillow image library in cmd:
pip install pillow
copy and save the below text file to C:\scripts\compress_images.py,
and run this script in cmd with
py C:\scripts\compress_images.py
import os
import shutil
from PIL import Image
def has_transparency(img):
"""Return True if the image has an alpha channel or transparent palette."""
if img.mode == 'RGBA':
return True
if img.mode == 'P' and 'transparency' in img.info:
return True
if img.mode in ('LA', 'PA'):
return True
return False
def main():
print("""
================================================================
PNG Compressor & Image Copier
================================================================
This script will:
- Ask you for a folder path containing images
- Create an 'output' subfolder inside that folder
- Leave all your original images untouched
- For PNG files:
* If the PNG has transparency (alpha) -> copy original PNG to output
* Else convert PNG to JPG
* If the resulting JPG is LARGER than the original PNG,
delete the JPG and copy the original PNG instead
- For JPG, JPEG, GIF, WEBP files:
* Copy them unchanged to the output folder
* (If a JPG shares a name with a PNG, the JPG is skipped
because the PNG will produce a JPG or copy itself)
Purpose: Safely compress PNGs that are better as JPGs, while
preserving transparent images and never increasing file size.
================================================================
""")
# Ask for directory
while True:
root_dir = input("Paste (right-click) directory path containing images (or enter q to exit): ").strip()
if not root_dir:
print("No directory entered. Exiting.")
return
# Check for exit command
if root_dir.lower() == 'q':
print("Exiting.")
return
# Remove surrounding quotes if present
if root_dir.startswith('"') and root_dir.endswith('"'):
root_dir = root_dir[1:-1]
if os.path.isdir(root_dir):
break
print(f"Directory not found: {root_dir}")
print("Please try again.")
output_dir = os.path.join(root_dir, "output")
os.makedirs(output_dir, exist_ok=True)
# Get all files
all_files = [f for f in os.listdir(root_dir) if os.path.isfile(os.path.join(root_dir, f))]
# Separate PNGs and other images
png_files = []
other_images = []
for f in all_files:
lower = f.lower()
if lower.endswith('.png'):
png_files.append(f)
elif lower.endswith(('.jpg', '.jpeg', '.gif', '.webp')):
other_images.append(f)
# Copy non-PNG images first (but skip JPG/JPEG if a PNG with same base name exists)
for img in other_images:
base = os.path.splitext(img)[0].lower()
ext = os.path.splitext(img)[1].lower()
if ext in ('.jpg', '.jpeg'):
if any(os.path.splitext(p)[0].lower() == base for p in png_files):
print(f"Skipping copy of {img} (PNG with same name will produce JPG)")
continue
src_path = os.path.join(root_dir, img)
dst_path = os.path.join(output_dir, img)
shutil.copy2(src_path, dst_path)
print(f"Copied unchanged: {img}")
# Now process PNG files
if not png_files:
print("No PNG files found.")
return
for png_file in png_files:
png_path = os.path.join(root_dir, png_file)
base_name = os.path.splitext(png_file)[0]
jpg_path = os.path.join(output_dir, base_name + ".jpg")
try:
with Image.open(png_path) as img:
if has_transparency(img):
print(f"Alpha detected: {png_file} -> copying PNG")
shutil.copy2(png_path, os.path.join(output_dir, png_file))
continue
rgb_img = img.convert('RGB')
rgb_img.save(jpg_path, 'JPEG', quality=80, optimize=True)
png_size = os.path.getsize(png_path)
jpg_size = os.path.getsize(jpg_path)
if jpg_size > png_size:
print(f"JPG larger ({jpg_size} > {png_size}) for {png_file} -> reverting to PNG")
os.remove(jpg_path)
shutil.copy2(png_path, os.path.join(output_dir, png_file))
else:
print(f"Converted {png_file} (JPG: {jpg_size}, PNG: {png_size})")
except Exception as e:
print(f"Error processing {png_file}: {e} -> copying original PNG")
shutil.copy2(png_path, os.path.join(output_dir, png_file))
print("\nDone! Check the 'output' folder for results.")
if __name__ == '__main__':
main()