previewgenerator
previewgenerator copied to clipboard
Optimization Python script
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.
I made it mostly for the square previews, those seem to be the biggest problem on my side
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.
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:
I made it mostly for the square previews, those seem to be the biggest problem on my side
Do you mean you recompress existing previews using the quality settings in the script? Is this different to setting jpeg and webp quality in config.php?
Yes, basically they're recompressed, with different settings. The difference is, it's more granular: the smaller the image, the more it gets compressed, because it's usually also viewed on a smaller screen, and it doesn't matter as much. It also compresses to progressive.
I have problems with upload bandwidth at the server location, so ever bit counts. This way, even if it loads slowly, there will be an image available during loading.
I don't think it's worth the hustle otherwise, but it's logic would make sense in Preview pre-Generators
It doesn't work on webP, when I wrote it webP wasn't yet integrated. Is it now? I can't really find something like that.
Thanks. It is a useful tool. It saves bandwidth for mobile users too.
As for WebP, you need to enable the Imaginary support in Nextcloud and also run a Imaginary daemon.
https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/config_sample_php_parameters.html#preview-imaginary-url