将图像元数据与tif一起保存的最佳方法是什么? [英] What is the best way to save image metadata alongside a tif?

查看:394
本文介绍了将图像元数据与tif一起保存的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我作为研究生的工作中,我捕获了显微镜图像,并使用python将其保存为原始tif图像.我想添加元数据,例如我正在使用的显微镜​​的名称,放大倍数和成像激光波长.这些细节对于我后处理图像都很重要.

In my work as a grad student, I capture microscope images and use python to save them as raw tif's. I would like to add metadata such as the name of the microscope I am using, the magnification level, and the imaging laser wavelength. These details are all important for how I post-process the images.

我应该可以用tif做到这一点,对吗?既然有标头了?

I should be able to do this with a tif, right? Since it has a header?

我能够在PIL图片中添加信息:

I was able to add to the info in a PIL image:

im.info['microscope'] = 'george'

但是当我保存并加载该图像时,添加的信息就消失了.

but when I save and load that image, the info I added is gone.

我愿意接受所有建议.如果可以的话,我将只保存一个单独的带有元数据的.txt文件,但是将其嵌入图像中真的很好.

I'm open to all suggestions. If I have too, I'll just save a separate .txt file with the metadata, but it would be really nice to have it embedded in the image.

推荐答案

供内部使用,请尝试在TIFF ImageDescription标签中将元数据另存为JSON,例如

For internal use, try saving the metadata as JSON in the TIFF ImageDescription tag, e.g.

from __future__ import print_function, unicode_literals

import json
import numpy
import tifffile  # http://www.lfd.uci.edu/~gohlke/code/tifffile.py.html

data = numpy.arange(256).reshape((16, 16)).astype('u1')
metadata = dict(microscope='george', shape=data.shape, dtype=data.dtype.str)
print(data.shape, data.dtype, metadata['microscope'])

metadata = json.dumps(metadata)
tifffile.imsave('microscope.tif', data, description=metadata)

with tifffile.TiffFile('microscope.tif') as tif:
    data = tif.asarray()
    metadata = tif[0].image_description
metadata = json.loads(metadata.decode('utf-8'))
print(data.shape, data.dtype, metadata['microscope'])

请注意,JSON使用unicode字符串.

Note that JSON uses unicode strings.

要与其他显微镜软件兼容,请考虑保存 OME-TIFF 文件,这些文件将定义的元数据以XML格式存储在ImageDescription标记中.

To be compatible with other microscopy software, consider saving OME-TIFF files, which store defined metadata as XML in the ImageDescription tag.

这篇关于将图像元数据与tif一起保存的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆