首先感谢!!!!我自己!!!!!
然后感谢前人分享的经验和编写 tifffile 的大佬!!!
我终于TMD的成功编写了一个SVS文件,并被Openslide, Tiffslide, ASAP, QUPATH 完美识别!!!!!CTMD
相信这能为后来人节省大量的时间!!!!
总结! CTMD
以下为从 0 生成一个全兼容的 SVS 文件的完整代码。图像内容为图块坐标。包含thumbnail, macro, label 。
import time
import numpy as np
import tifffile
import cv2
def gen_im(size_hw):
im_i = 0
while True:
im = np.zeros([*size_hw, 3], np.uint8)+255
im[:60, :60, :2] = 0
im[:60, -60:, 2] = 0
im[-60:, -60:, 1:] = 0
im[-60:, :60, 1] = 0
im = cv2.putText(im, str(im_i), (size_hw[1]//4, size_hw[0]//2), cv2.FONT_HERSHEY_PLAIN, 3, color=(0, 0, 0), thickness=3)
im_i += 1
yield im
'''
svs格式定义,TIFF或BIGTIFF,不能使用subifds
第1张,全分辨率tile图,需设定desc
第2张,缩略图
第3到第N-2张,降分辨率tile图,必须使用从大到小顺序
第N-1张,label图,需设定标志(ReducedImage 1 (0x1)),需设定desc
第N张,marco图,需设定标志 (ReducedImage 1 (0x1), Macro 8 (0x8)),需设定desc
'''
svs_desc = 'Aperio Image Library Fake\nABC |AppMag = {mag}|Filename = {filename}|MPP = {mpp}'
label_desc = 'Aperio Image Library Fake\nlabel {W}x{H}'
macro_desc = 'Aperio Image Library Fake\nmacro {W}x{H}'
def gen_pyramid_tiff(out_file):
thumbnail_im = np.zeros([762, 762, 3], dtype=np.uint8)
thumbnail_im = cv2.putText(thumbnail_im, 'thumbnail', (thumbnail_im.shape[1]//4, thumbnail_im.shape[0]//2), cv2.FONT_HERSHEY_PLAIN, 6, color=(255, 0, 0), thickness=3)
label_im = np.zeros([762, 762, 3], dtype=np.uint8)
label_im = cv2.putText(label_im, 'label', (label_im.shape[1]//4, label_im.shape[0]//2), cv2.FONT_HERSHEY_PLAIN, 6, color=(0, 255, 0), thickness=3)
macro_im = np.zeros([762, 762, 3], dtype=np.uint8)
macro_im = cv2.putText(macro_im, 'macro', (macro_im.shape[1]//4, macro_im.shape[0]//2), cv2.FONT_HERSHEY_PLAIN, 6, color=(0, 0, 255), thickness=3)
tile_hw = np.int64([512, 512])
multi_hw = np.int64([(10240, 10240), (7680, 7680), (2560, 2560), (1024, 1024), (512, 512)])
mpp = 0.25
mag = 40
resolution = [10000 / mpp, 10000 / mpp, 'CENTIMETER']
filename = 'ASD'
with tifffile.TiffWriter(out_file, bigtiff=True) as tif:
thw = tile_hw.tolist()
compression = ['JPEG', 95, dict(outcolorspace='YCbCr')]
kwargs = dict(subifds=0, photometric='rgb', planarconfig='CONTIG', compression=compression, dtype=np.uint8, metadata=None)
for i, hw in enumerate(multi_hw):
gen = gen_im(tile_hw)
hw = hw.tolist()
if i == 0:
desc = svs_desc.format(mag=mag, filename=filename, mpp=mpp)
tif.write(data=gen, shape=(*hw, 3), tile=thw[::-1], resolution=resolution, description=desc, **kwargs)
tif.write(data=thumbnail_im, description='', **kwargs)
else:
tif.write(data=gen, shape=(*hw, 3), tile=thw[::-1], resolution=resolution, description='', **kwargs)
tif.write(data=label_im, subfiletype=1, description=label_desc.format(W=label_im.shape[1], H=label_im.shape[0]), **kwargs)
tif.write(data=macro_im, subfiletype=9, description=macro_desc.format(W=macro_im.shape[1], H=macro_im.shape[0]), **kwargs)
t1 = time.perf_counter()
gen_pyramid_tiff('a9.svs')
print(time.perf_counter() - t1)
参考引用
openslide 手册中 svs 格式 https://openslide.org/formats/aperio/ 转换已有图像到openslide兼容格式 http://www.andrewjanowczyk.com/converting-an-existing-image-into-an-openslide-compatible-format/ 保存一个 8-bit RGB ome-tiff给QUPATH https://forum.image.sc/t/save-ome-tiff-as-8-bit-rgb-for-qupath/61281
|