Skip to content

Instantly share code, notes, and snippets.

@grischard
Created October 6, 2022 19:23
Show Gist options
  • Save grischard/f52bfd88f85b2b69a1f3b00785748d72 to your computer and use it in GitHub Desktop.
Save grischard/f52bfd88f85b2b69a1f3b00785748d72 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Optimise JPEGs in a mapproxy cache with mozjpeg.
Remembers processed files so they are only processed once.
Find -mtime wouldn't work because optimising the files changes the mtime.
"""
import os
from subprocess import call
PATH = "/home/openstreetmap/mapproxy/"
MOZJPEG = "/usr/local/bin/mozjpeg"
PROCESSED_FILES_FILE = os.path.join(PATH, "mozjpeg_optimised_tiles.txt")
PROCESSED_FILES = set(line.strip() for line in open(PROCESSED_FILES_FILE))
CACHE_DATA = os.path.join(PATH, "cache_data")
with open(PROCESSED_FILES_FILE, "a") as pff:
for root, dirs, files in os.walk(CACHE_DATA):
for tilefile in files:
if tilefile.endswith(".jpeg"):
tilepath = os.path.join(root, tilefile)
if tilepath not in PROCESSED_FILES:
call([MOZJPEG, "-copy", "none", "-outfile", tilepath, tilepath])
pff.write("%s\n" % tilepath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment