Skip to content

Commit

Permalink
Implement progress bars for DZI creation
Browse files Browse the repository at this point in the history
They are off by default. To show them, call: `create(..., quiet=False)`

Implements openzoom#6
  • Loading branch information
ali1234 committed Oct 28, 2020
1 parent f2a1fa7 commit 080f2a2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 7 additions & 4 deletions deepzoom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import xml.dom.minidom

import PIL.Image
from tqdm import tqdm, trange

from collections import deque

Expand Down Expand Up @@ -401,14 +402,16 @@ def get_image(self, level):
return self.image.resize((width, height), PIL.Image.ANTIALIAS)
return self.image.resize((width, height), RESIZE_FILTERS[self.resize_filter])

def tiles(self, level):
def tiles(self, level, quiet=True):
"""Iterator for all tiles in the given level. Returns (column, row) of a tile."""
columns, rows = self.descriptor.get_num_tiles(level)
progress = tqdm(desc="Tiles", total=columns*rows, leave=None, disable=quiet)
for column in range(columns):
for row in range(rows):
progress.update()
yield (column, row)

def create(self, source, destination):
def create(self, source, destination, quiet=True):
"""Creates Deep Zoom image from source file and saves it to destination."""
self.image = PIL.Image.open(safe_open(source))
width, height = self.image.size
Expand All @@ -421,10 +424,10 @@ def create(self, source, destination):
)
# Create tiles
image_files = _get_or_create_path(_get_files_path(destination))
for level in range(self.descriptor.num_levels):
for level in trange(self.descriptor.num_levels, desc="Levels", disable=quiet):
level_dir = _get_or_create_path(os.path.join(image_files, str(level)))
level_image = self.get_image(level)
for (column, row) in self.tiles(level):
for (column, row) in self.tiles(level, quiet=quiet):
bounds = self.descriptor.get_tile_bounds(level, column, row)
tile = level_image.crop(bounds)
format = self.descriptor.tile_format
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
keywords="deepzoom seadragon dzi dzc seadragonajax seadragonmobile silverlightdeepzoom microsoft openzoom",
packages=find_packages(),
license="BSD 3-Clause License",
install_requires=["Pillow>=6"],
install_requires=["Pillow>=6", "tqdm"],
url="https://github.com/openzoom/deepzoom.py",
include_package_data=True,
classifiers=[
Expand Down

0 comments on commit 080f2a2

Please sign in to comment.