<commit_before>#!/usr/bin/python

import sys
import os
from PIL import Image

# you need to install this library yourself
# recent versions handle bigtiff too...
import tifffile

"""
Extract a pyramidal TIFF with JPEG tiled storage into a tree of
separate JPEG files as expected by Zoomify.

usage: extract.py pyramid-file dest-dir

The pyramid-file must be a multi-page TIFF with each page having an
image scaled by 1/2 from the previous page.  All pages must be tiled
with the same tile size, and tiles must be stored using the new-style
JPEG compression format, i.e. TIFF compression == 7.

If the lowest resolution page must have 4 or fewer tiles.  If it has
more than 1, this script will leave space for the user to manually
generate the final lowest zoom tile 0-0-0.jpg that is 1/2 scaled
version of the image represented by that last page.

"""

try:
    fname = sys.argv[1]
    outdir = sys.argv[2]

    infile = open(fname, 'rb')
    if not os.path.exists(outdir):
        os.makedirs(outdir)
except:
    sys.stderr.write('\nusage: extract.py pyramid-file dest-dir\n\n')
    raise

dir_template = '%(outdir)s/TileGroup%(groupno)d'
tile_template = dir_template + '/%(zoomno)d-%(tcolno)d-%(trowno)d.jpg'

tiff = tifffile.TiffFile(fname)
pages = list(tiff)

# we need to go from lowest to highest zoom level
pages.reverse()

tile_group = 0
tiles_per_group = 256

# skip pages that aren't tiled... thumbnails?!
outpages = [ page for page in pages if hasattr(page.tags, 'tile_offsets') ]
if type(outpages[0].tags.tile_offsets.value) is int:
    outpages[0].tags.tile_offsets.value=[outpages[0].tags.tile_offsets.value]

if hasattr(outpages[0].tags, 'tile_offsets') and len(outpages[0].tags.tile_offsets.value) > 1:
    # first input zoom level is multi-tile
    assert len(outpages[0].tags.tile_offsets.value) <= 4

    # so leave space for tile 0-0-0
    zoomno = 1
    group_file_count = 1
    total_tiles = 1
    need_to_build_0 = True
<commit_msg>Correction for the case outpages[0].tags.tile_byte_counts.value is int.<commit_after>#!/usr/bin/python

import sys
import os
from PIL import Image

# you need to install this library yourself
# recent versions handle bigtiff too...
import tifffile

"""
Extract a pyramidal TIFF with JPEG tiled storage into a tree of
separate JPEG files as expected by Zoomify.

usage: extract.py pyramid-file dest-dir

The pyramid-file must be a multi-page TIFF with each page having an
image scaled by 1/2 from the previous page.  All pages must be tiled
with the same tile size, and tiles must be stored using the new-style
JPEG compression format, i.e. TIFF compression == 7.

If the lowest resolution page must have 4 or fewer tiles.  If it has
more than 1, this script will leave space for the user to manually
generate the final lowest zoom tile 0-0-0.jpg that is 1/2 scaled
version of the image represented by that last page.

"""

try:
    fname = sys.argv[1]
    outdir = sys.argv[2]

    infile = open(fname, 'rb')
    if not os.path.exists(outdir):
        os.makedirs(outdir)
except:
    sys.stderr.write('\nusage: extract.py pyramid-file dest-dir\n\n')
    raise

dir_template = '%(outdir)s/TileGroup%(groupno)d'
tile_template = dir_template + '/%(zoomno)d-%(tcolno)d-%(trowno)d.jpg'

tiff = tifffile.TiffFile(fname)
pages = list(tiff)

# we need to go from lowest to highest zoom level
pages.reverse()

tile_group = 0
tiles_per_group = 256

# skip pages that aren't tiled... thumbnails?!
outpages = [ page for page in pages if hasattr(page.tags, 'tile_offsets') ]
if type(outpages[0].tags.tile_offsets.value) is int:
    outpages[0].tags.tile_offsets.value=[outpages[0].tags.tile_offsets.value]
    outpages[0].tags.tile_byte_counts.value=[outpages[0].tags.tile_byte_counts.value]

if hasattr(outpages[0].tags, 'tile_offsets') and len(outpages[0].tags.tile_offsets.value) > 1:
    # first input zoom level is multi-tile
    assert len(outpages[0].tags.tile_offsets.value) <= 4

    # so leave space for tile 0-0-0
    zoomno = 1
    group_file_count = 1
    total_tiles = 1
    need_to_build_0 = True

{'fraction_non_alphanumeric': 0.09940799210656143, 'fraction_numerical': 0.01603354711396152, 'mean_word_length': 3.8621103117505995, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3992710', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1367, 'n_words': 465}