Jump to content

png.py


Berat Erdem

Recommended Posts

Posted (edited)

1. 🤖 What does it do? (Automatic Image Optimization): Compress and optimize your PNG, JPG, and JPEG assets right from your desktop in seconds—no need for external online converters! ⚡

2. 🎯 Multi-File Support & Drag-and-Drop Convenience: Simply select multiple image files and drag-and-drop them onto the script all at once for instant batch processing! 🖱️📦

3. 💾 Dedicated Output Folder: Automatically creates a "COMPRESSED_IMAGES" folder in your directory to keep your optimized files organized without overwriting originals! 📁✨

4. 🎨 Smart Compression Tech: Uses adaptive color palette reduction for PNGs and optimal quality (80%) reduction for JPEGs to maintain visual quality while drastically reducing file sizes! 🖼️📉

5. 📊 Real-Time Savings Calculator: Instantly displays the before and exact after sizes along with the percentage of storage saved for each processed image! 💡

6. 🌐 100% Open Source & Local: Runs entirely locally on your machine with complete transparency and zero risk—use it safely for all your game assets! 💯
 

IMPORTANT 
Python 64 - BIT https://www.python.org/ftp/python/3.13.17/python-3.13.17-amd64.exe
Python 32 - BIT https://www.python.org/ftp/python/3.13.17/python-3.13.17.exe

Remember!
It will only work if you move the contents of the .lua file into a file with a .py extension.
If you try to run it as is, it won't work ❤️

If you do not do these things, it will not work, and the required library is as follows:

pip install Pillow

 

 

import sys
import os
from PIL import Image

def compress_images(file_paths):
    script_dir = os.path.dirname(os.path.abspath(__file__)) if __file__ else os.getcwd()
    output_dir = os.path.join(script_dir, "COMPRESSED_IMAGES")
    
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
        print(f"[+] '{output_dir}' folder created.\n")

    valid_extensions = ('.png', '.jpg', '.jpeg')
    valid_files = [f for f in file_paths if f.lower().endswith(valid_extensions)]

    if not valid_files:
        print("[-] Please drag and drop valid .png, .jpg, or .jpeg files!")
        return

    print(f"[i] Processing a total of {len(valid_files)} image(s)...\n")

    for file_path in valid_files:
        file_name = os.path.basename(file_path)
        ext = os.path.splitext(file_name)[1].lower()
        output_path = os.path.join(output_dir, file_name)

        try:
            original_size = os.path.getsize(file_path) / 1024

            with Image.open(file_path) as img:
                if ext == '.png':
                    img_converted = img.convert("P", palette=Image.ADAPTIVE, colors=256)
                    img_converted.save(output_path, "PNG", optimize=True)
                else:
                    if img.mode in ("RGBA", "P"):
                        img = img.convert("RGB")
                    img.save(output_path, "JPEG", optimize=True, quality=80)

            new_size = os.path.getsize(output_path) / 1024
            saved_ratio = 100 - (new_size / original_size * 100) if original_size > 0 else 0

            print(f"[OK] {file_name}")
            print(f"     Before: {original_size:.1f} KB | After: {new_size:.1f} KB | Saved: %{saved_ratio:.1f}\n")

        except Exception as e:
            print(f"[X] An error occurred while processing {file_name}: {e}\n")

if __name__ == "__main__":
    if len(sys.argv) > 1:
        compress_images(sys.argv[1:])
    else:
        print("Usage: Drag and drop one or more image files (.png, .jpg, .jpeg) onto this .py file.")
    
    input("\nPress Enter to exit...")
    #BERAT ERDEM
    #https://forum.multitheftauto.com/profile/90671-berat-erdem/
Edited by Berat Erdem

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...