nextcloud / nextcloud/previewgenerator

Optimization Python script

Open
#443 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

performance
Dominant language
PHP
Stars
520
Forks
55
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file, test, or entry point is named. Start by determining whether this standalone Python script belongs in previewgenerator and how its ImageMagick, jpegoptim, and pngquant workflow fits the app; completion criteria are not defined.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.