Skip to content

Instantly share code, notes, and snippets.

@elisium-oat
Last active August 18, 2023 21:00
Show Gist options
  • Select an option

  • Save elisium-oat/fdffe012c6261b3001658115c36b5d68 to your computer and use it in GitHub Desktop.

Select an option

Save elisium-oat/fdffe012c6261b3001658115c36b5d68 to your computer and use it in GitHub Desktop.
This script help to combine a very big image from EasyZoom.
# Image tiler for EasyZoom's image
# This script requires Pillow (https://pillow.readthedocs.io/en/stable/)
# You have to download the .szi file of the EasyZoom image.
# You can find the URL of this file in by "inspecting" the page in the browser
# and looking at the "Network" tab. Be sure to remove the "?bytes=xxxxxx-yyyyyyy"
# of the URL params before downloading.
import sys
import re
import io
from functools import cmp_to_key
from PIL import Image
# NOTE: You can use '-' for using stdin/stdout
SRC = sys.argv[1] # Your .szi file
DST = sys.argv[2] # The final image path
def eprint(s):
if DST != '-':
print(s, file=sys.stderr)
def get_img(jpeg):
return Image.open(io.BytesIO(jpeg))
szi = (sys.stdin.buffer if SRC == '-' else open(SRC, 'rb')).read()
ZOOM = max(int(x.group(1))
for x in re.finditer(b'_files/(\d+)/\d+_\d+\.jpgII', szi))
eprint(f'Zoom level: {ZOOM}')
TILES = sorted((
(int(i.group(1)),
int(i.group(2)),
i.group(3))
for i in re.finditer(b'_files/%i/(\d+)_(\d+)\.jpgII.*?(\xFF\xD8.*?\xFF\xD9)' % ZOOM, szi, re.DOTALL)),
key=cmp_to_key(lambda t1, t2: t1[0] > t2[0] and t1[1] > t2[1]))
if len(TILES) == 0:
eprint(f'Zoom level {ZOOM} not found.')
exit(1)
TILE_W, TILE_H = get_img(TILES[0][2]).size
W, H, LAST_JPEG = TILES[-1] # Take the last tile's X and Y
LAST_W, LAST_H = get_img(LAST_JPEG).size
img = Image.new('RGB', (TILE_W*W+LAST_W, TILE_H*H+LAST_H))
for x, y, jpeg in TILES:
eprint(f'{x}x{y}')
tile_img = get_img(jpeg)
img.paste(tile_img, (x * TILE_W, y * TILE_H))
tile_img.close()
if DST == '-':
DST = sys.stdout
eprint(f'Saving {DST}...')
img.save(DST, "JPEG", optimize=True, quality=100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment