Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization Python script #443

Open
VilterPD opened this issue Jan 29, 2024 · 3 comments
Open

Optimization Python script #443

VilterPD opened this issue Jan 29, 2024 · 3 comments

Comments

@VilterPD
Copy link

VilterPD commented Jan 29, 2024

Hey, I'm quite new to GitHub, so I dont really know how to do this, but I wrote a little script to make the Previews load a little Faster:

import os
import subprocess
from concurrent.futures import ThreadPoolExecutor

def compress_image(path, quality):
    subprocess.run(["jpegoptim", f"--max={quality}", path])

def convert_to_progressive_jpeg(path):
    subprocess.run(["mogrify", "-interlace", "Plane", "-quality", "85%", path])

def optimize_png(path):
    subprocess.call(["pngquant", "--force", "--ext", ".png", path])

def process_file(root, file):
    full_path = os.path.join(root, file)
    if file.endswith(".jpg") or file.endswith(".jpeg"):
        # Konvertiere zuerst in ein progressives JPEG
        convert_to_progressive_jpeg(full_path)
        # Dann führe die Kompression durch
        if "64" in file:
            compress_image(full_path, 40) 
        elif "256" in file:
            compress_image(full_path, 50)
        elif "512" in file:
            compress_image(full_path, 60) 
        elif "1024" in file:
            compress_image(full_path, 60)
    elif file.endswith(".png"):
        # Optimize PNG files
        optimize_png(full_path)

def main():
    directory_path = **"Path to Previews folder"**
    with ThreadPoolExecutor(max_workers=10) as executor:
        for root, dirs, files in os.walk(directory_path):
            for file in files:
                executor.submit(process_file, root, file)

if __name__ == "__main__":
    main()

I'm sure its not optimal, but it does speed up significantly, since I don't have a huge Upload speed and am behind a firewall with a badly optimized reverse proxy. AVIF Previews would probably be better, but this works now.

It relies on ImageMagick, jpegoptim and pngquant.

If this is the wrong place to post this, Im sure some kind stranger will tell me where to put it.

@VilterPD
Copy link
Author

I made it mostly for the square previews, those seem to be the biggest problem on my side

@st3iny
Copy link
Member

st3iny commented Feb 6, 2024

Interesting piece of code. Although existing previews are overwritten on file changes.

I'll try to give it a test and might add it to the readme.

@VilterPD
Copy link
Author

VilterPD commented Feb 6, 2024

Yeah, that's a problem. I have a lot of storage, so I rsynced the whole previews folder beforehand for safety. Had no trouble though

I have by now simplified it, because it was a little bulky. It also asks for the path to the previews:

compress_previews.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants