使用JPEG压缩(python)保存多通道(n>3)图像

2024-06-10 21:46:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理一些超过3个通道的生物医学图像。 这些图像相当大,所以我更喜欢将它们存储为带有JPEG压缩的金字塔形TIFF文件。与其他压缩方案(如LZW、Deflate)相比,这提供了一个数量级的压缩比

我目前正在使用pyvipstiffsave函数来保存这些图像。使用其他压缩方案,我可以使用任意数量的通道保存图像。 然而,使用JPEG压缩,我意识到保存的图像只能有1或3个通道

如果JPEG的算法可以压缩一个通道,那么肯定有办法将多个JPEG压缩的单通道图像打包到一个文件中

有人知道如何存储n>;使用以下条件将图像三通道传输到单个文件中

  • JPEG压缩(或性能相当的算法)
  • 使用GUI图像查看软件(如ImageJ)轻松查看
  • 我真的不想将每个频道存储为单独的文件
  • 以金字塔格式存储的图像
  • Python 3

Tags: 文件函数图像算法数量方案jpegdeflate
1条回答
网友
1楼 · 发布于 2024-06-10 21:46:19

libvips 8.10 is out now并支持生成OME-TIFF金字塔

这些TIFF将每个频道存储在文件的不同页面中,每个页面都有自己的金字塔保存在TIFF子IFD中。我希望ImageJ可以阅读这些,尽管您可能需要一个插件

您可以在PyVIP中生成它们,如下所示:

#!/usr/bin/python3

import sys
import pyvips

def load(filename):
    return pyvips.Image.new_from_file(filename)

# load a couple of eg. three-band images to make a 6-band image
image = load(sys.argv[2]).bandjoin(load(sys.argv[3]))

# to convert to OME, we need a tall, thin mono image with page-height set to
# indicate where the joins are
ome = pyvips.Image.arrayjoin(image.bandsplit(), across=1)

# you must make a private copy before modifying image metadata
ome = ome.copy()
ome.set_type(pyvips.GValue.gint_type, 'page-height', image.height)

# now we can write as a pyramid
# libvips 8.10+ will put the pyramid layers into SUBIFDs
ome.tiffsave(sys.argv[1], pyramid=True, compression="jpeg", Q=90)

像这样跑:

$ ./gen_ome.py x.tif ~/pics/1.tiff ~/pics/2.tiff 
$ tiffinfo x.tif 
TIFF Directory at offset 0x173f02 (1523458)
  Subfile Type: multi-page document (2 = 0x2)
  Image Width: 6048 Image Length: 4032
  Tile Width: 128 Tile Length: 128
  Resolution: 300, 300 pixels/inch
  Bits/Sample: 8
  Sample Format: unsigned integer
  Compression Scheme: JPEG
  Photometric Interpretation: min-is-black
  Orientation: row 0 top, col 0 lhs
  Samples/Pixel: 1
  Planar Configuration: single image plane
  Page Number: 0-6
  SubIFD Offsets: 2153288 2339670 2396406 2415400 2422262 2424960
  JPEG Tables: (73 bytes)
TIFF Directory at offset 0x3c3b2c (3947308)
  Subfile Type: multi-page document (2 = 0x2)
  Image Width: 6048 Image Length: 4032
  Tile Width: 128 Tile Length: 128
...

因此,您可以看到六个通道有六个页面,每个页面都有一组包含金字塔层的子IFD

相关问题 更多 >