gpt4 book ai didi

python - 无法获得 tiff 图像分辨率

转载 作者:太空宇宙 更新时间:2023-11-03 20:34:27 25 4
gpt4 key购买 nike

我正在尝试读取 16 位 .tif 显微镜图像 https://data.broadinstitute.org/bbbc/BBBC006/并使用分析它们 https://github.com/sakoho81/pyimagequalityranking/tree/master/pyimq但是我在加载 tif 图像的代码部分出现错误。它使用 PIL tiffimageplugin: https://pillow.readthedocs.io/en/3.0.0/_modules/PIL/TiffImagePlugin.html当它尝试获取分辨率标签时,它给了我一个关键错误有什么想法吗?建议?修复?

谢谢!

import os
import numpy
import scipy.ndimage.interpolation as itp
import argparse
from PIL import Image
from PIL.TiffImagePlugin import X_RESOLUTION, Y_RESOLUTION
from matplotlib import pyplot as plt
from math import log10, ceil, floor



def get_image_from_imagej_tiff(cls, path):
"""
A class method for opening a ImageJ tiff file. Using this method
will enable the use of correct pixel size during analysis.
:param path: Path to an image
:return: An object of the MyImage class
"""
assert os.path.isfile(path)
assert path.endswith(('.tif', '.tiff'))
print(path) #my own little debug thingamajig
image = Image.open(path)

xresolution = image.tag.tags[X_RESOLUTION][0][0] #line that errors out
yresolution = image.tag.tags[Y_RESOLUTION][0][0]

#data = utils.rescale_to_min_max(numpy.array(image), 0, 255)

if data.shape[0] == 1:
data = data[0]

return cls(images=data, spacing=[1.0/xresolution, 1.0/yresolution])

终端输入:

pyimq.main --mode=directory --mode=analyze --mode=plot --working-directory=/home/myufa/predxion/BBBC/a_1_s1 --normalize-power --result=fstd --imagej

输出:

Mode option is ['directory', 'analyze', 'plot']
/home/myufa/predxion/BBBC/a_1_s1/z0_a_1_s1_w1.tif
Traceback (most recent call last):
File "/home/myufa/.local/bin/pyimq.main", line 11, in <module>
load_entry_point('PyImageQualityRanking==0.1', 'console_scripts', 'pyimq.main')()
File "/home/myufa/anaconda3/lib/python3.7/site-packages/PyImageQualityRanking-0.1-py3.7.egg/pyimq/bin/main.py", line 148, in main
File "/home/myufa/anaconda3/lib/python3.7/site-packages/PyImageQualityRanking-0.1-py3.7.egg/pyimq/myimage.py", line 81, in get_image_from_imagej_tiff
KeyError: 282

Edit: Here's what I got when I tried some suggestions/indexed the tag, which makes even less sense

最佳答案

我猜有问题的 tiff 没有遵循正常的图像约定。 [XY]分辨率标签(编号 282 和 283)在一大堆规范中是强制性的或必需的,但可能并非在所有应用程序中都存在。我有一些 TIFF(DNG 格式)根本无法用 PIL(Pillow)加载;这促使我编写一个脚本来转储主要标签结构:

# TIFF structure program
import struct
import PIL.TiffTags

class DE:
def __init__(self, tiff):
self.tiff = tiff
(self.tag, self.type, self.count, self.valueoroffset) = struct.unpack(
tiff.byteorder+b'HHI4s', self.tiff.file.read(12))
# TODO: support reading the value
def getstring(self):
offset = struct.unpack(self.tiff.byteorder+b'I', self.valueoroffset)[0]
self.tiff.file.seek(offset)
return self.tiff.file.read(self.count)

class IFD:
def __init__(self, tiff):
self.tiff = tiff
self.offset = tiff.file.tell()
(self.len,) = struct.unpack(self.tiff.byteorder+b'H', self.tiff.file.read(2))
def __len__(self):
return self.len
def __getitem__(self, index):
if index>=self.len or index<0:
raise IndexError()
self.tiff.file.seek(self.offset+2+12*index)
return DE(self.tiff)
def nextoffset(self):
self.tiff.file.seek(self.offset+2+12*self.len)
(offset,) = struct.unpack(self.tiff.byteorder+b'I', self.tiff.file.read(4))
return (offset if offset!=0 else None)

class TIFF:
def __init__(self, file):
self.file = file
header = self.file.read(8)
self.byteorder = {b'II': b'<', b'MM': b'>'}[header[:2]]
(magic, self.ifdoffset) = struct.unpack(self.byteorder+b'HI', header[2:])
assert magic == 42
def __iter__(self):
offset = self.ifdoffset
while offset:
self.file.seek(offset)
ifd = IFD(self)
yield ifd
offset = ifd.nextoffset()

def main():
tifffile = open('c:/users/yann/pictures/img.tiff', 'rb')
tiff = TIFF(tifffile)
for ifd in tiff:
print(f'IFD at {ifd.offset}, {ifd.len} entries')
for entry in ifd:
print(f' tag={entry.tag} {PIL.TiffTags.lookup(entry.tag).name}')

if __name__=='__main__':
main()

由于您至少拥有图像对象,因此更快的方法可能是:

import pprint, PIL.TiffTags
pprint.pprint(list(map(PIL.TiffTags.lookup, img.tag)))

其中一个可能会让您了解 TIFF 的实际内容是什么。由于 PIL 可以加载它,因此它可能有像素数,但没有物理分辨率。

关于python - 无法获得 tiff 图像分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57261709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com