question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Would like to know little bit insight why one image size is increased and other one decreased?

See original GitHub issue

Hello there, I have downloaded two .tiff images from cytomine website:

  1. https://cytomine.com/collection/hsm0170/400-human-ileons (it’s a huge image 158 GB )
  2. https://cytomine.com/collection/uliege001/cat-rectum (size 847 Mb) After that, I converted them into ome.tiff using the below code (bcoz bioformat was unable to read these image resolution layers).
def tiles(stack, tileshape):
    for y in range(0, stack.shape[0], tileshape[0]):
        for x in range(0, stack.shape[1], tileshape[1]):
            yield stack[y: y + tileshape[0], x: x + tileshape[1]]


with tifffile.imread(input_path, aszarr=True) as store:
    group = zarr.open(store, mode='a')
    assert isinstance(group, zarr.Group)
    with tifffile.TiffWriter(output_path + f'HSM0170.ome..tiff', bigtiff=True) as tiff:
        start_time = timeit.default_timer()
        for r in group.keys():
            stack = group[r]
            width = stack.shape[0]
            length = stack.shape[1]
            # assert stack.ndim == 3  # YXC
            a = tiles(stack, (stack.chunks[0], stack.chunks[1]))

            options = {'tile': (stack.chunks[0], stack.chunks[1]), 'compression': 'JPEG'}
            tiff.write(tiles(stack, (stack.chunks[0], stack.chunks[1])), shape=stack.shape,
                       dtype=stack.dtype, **options)
    end_time = timeit.default_timer()
    total_time = float(end_time - start_time)
    print(total_time)

It got converted and I receive generated image first image size was increased to 247.2 GB and the second image size was decreased to 713.5 MB. I want to know a little bit of insight detail why the first image size is increased and the second image size is decreased while applying the same code. Which parameter is crucial while converting, is it jpeg quality?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cgohlkecommented, Mar 7, 2021

For pyramidal OME-TIFF you have to reserve subifds for all subresolutions when writing the base/first level.

This should work, although it will probably take several hours to decode and compress several terabytes of image data:

import tifffile
import zarr

input_path = 'HSM0170-20x20.tiff'
output_path = 'HSM0170.ome.tif'


def tiles(stack, tileshape):
    for y in range(0, stack.shape[0], tileshape[0]):
        for x in range(0, stack.shape[1], tileshape[1]):
            yield stack[y : y + tileshape[0], x : x + tileshape[1]]


with tifffile.Timer():

    with tifffile.imread(input_path, aszarr=True) as store:
        group = zarr.open(store, mode='r')
        assert isinstance(group, zarr.Group)

        with tifffile.TiffWriter(output_path, bigtiff=True) as tiff:
            for i, r in enumerate(tifffile.natural_sorted(group.keys())):
                stack = group[r]
                assert stack.ndim == 3  # YXC
                options = {
                    'tile': (stack.chunks[0], stack.chunks[1]),
                    'compression': ('jpeg', 70),
                }
                if i == 0:
                    options['subifds'] = len(group.keys()) - 1
                else:
                    options['subfiletype'] = 1
                tiff.write(
                    tiles(stack, (stack.chunks[0], stack.chunks[1])),
                    shape=stack.shape,
                    dtype=stack.dtype,
                    **options,
                )
0reactions
cgohlkecommented, Mar 7, 2021

There was another issue in the code. You need to iterate over the zarr groups in the correct order for i, r in enumerate(tifffile.natural_sorted(group.keys())):.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Always Up-To-Date Guide to Social Media Image Sizes
Make sure that you are choosing dimensions based on where you want the majority of viewers to see your image. Learn more about...
Read more >
Resizing images for the web - ALL you need to know
Resizing images for the web - In this article, we will look at digital imaging concepts to do with resolution, image size and...
Read more >
Organic Reach is in Decline—Here's What You Can Do About It
Never take a one-size-fits-all approach to social media marketing, especially with organic content. To reach the most people, organic posts need to be...
Read more >
10 Easy Ways to Boost Your Instagram Reach - Buffer
Here're 10 simple tactics to use the Instagram algorithm to your advantage and increase your Instagram reach in the algorithmic-feed world.
Read more >
Everything you need to know to write effective alt text
Learn how to write meaningful alternative text (alt text) for images, charts, and more.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found