cgohlke / tifffile

Read and write TIFF files
https://pypi.org/project/tifffile
BSD 3-Clause "New" or "Revised" License
545 stars 154 forks source link

generate tiff file #208

Closed chengdonglin closed 1 year ago

chengdonglin commented 1 year ago

I use next code to write a tiff file ,when I use the Openslide to open, the level demisiion is only one ,, is correct ? if error, how can I fix it

import tifffile
import numpy as np
import cv2

def gen_im(size_hw):
    im_i = 0
    while True:
        im = np.zeros([*size_hw, 3], np.uint8)
        im = cv2.putText(im, str(im_i), (size_hw[1]//4, size_hw[0]//2), cv2.FONT_HERSHEY_PLAIN, 3, color=(255, 255, 255), thickness=2)
        im_i += 1
        yield im
tile_hw = (256, 256)

multi_hw = [(10240, 10240), (7680, 7680), (2560, 2560), (1280, 1280), (512, 512)]

with tifffile.TiffWriter('abc.ome.tif', bigtiff=True, ome=True) as tif:
    for i, hw in enumerate(multi_hw):
        if i == 0:
            tif.write(data=gen_im(tile_hw), subifds=len(multi_hw)-1, tile=tile_hw, photometric='rgb', compression='jpeg', shape=(*hw, 3), dtype=np.uint8)
        else:
            tif.write(data=gen_im(tile_hw), subfiletype=1, tile=tile_hw, photometric='rgb', compression='jpeg', shape=(*hw, 3), dtype=np.uint8)
cgohlke commented 1 year ago

OpenSlide does not support OME-TIFF. Try Generic tiled TIFF instead.

chengdonglin commented 1 year ago

OpenSlide does not support OME-TIFF. Try Generic tiled TIFF instead.

have any example write Generic tiled Tiff by tifffile ? thanks!

cgohlke commented 1 year ago

Drop the ome format and subifs, use factor 2 subsampling, and write the resolution tags.