我尝试在Python中使用bioformat来读取显微镜图像(.lsm、.czi、.lif,随便你怎么说),打印出元数据,然后显示图像。ome = bf.OMEXML(md)
给了我一个错误(如下)。我想这是在讨论存储在md
中的信息。它不喜欢md
中的信息不全是ASCII。但是我如何克服这个问题呢?
我写的是:
import Tkinter as Tk, tkFileDialog
import os
import javabridge as jv
import bioformats as bf
import matplotlib.pyplot as plt
import numpy as np
jv.start_vm(class_path=bf.JARS, max_heap_size='12G')
用户选择要使用的文件
^{pr2}$将图像放入numpy数组
raw_data = []
for z in range(iome.Pixels.get_SizeZ()):
raw_image = reader.read(z=z, series=0, rescale=False)
raw_data.append(raw_image)
raw_data = np.array(raw_data)
显示需要的元数据
iome = ome.image(0) # e.g. first image
print(iome.get_Name())
print(iome.Pixels.get_SizeX())
print(iome.Pixels.get_SizeY())
我得到的错误是:
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-22-a22c1dbbdd1e> in <module>()
11 reader = bf.ImageReader(file_full_path)
12 md = bf.get_omexml_metadata(file_full_path)
---> 13 ome = bf.OMEXML(md)
/anaconda/envs/env2_bioformats/lib/python2.7/site-packages/bioformats/omexml.pyc in __init__(self, xml)
318 if isinstance(xml, str):
319 xml = xml.encode("utf-8")
--> 320 self.dom = ElementTree.ElementTree(ElementTree.fromstring(xml))
321
322 # determine OME namespaces
<string> in XML(text)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb5' in position 1623: ordinal not in range(128)
这是一个具有专利显微术格式的代表test image
感谢您添加示例图像。这帮了大忙!在
让我们首先删除所有不必要的Tkinter代码,直到得到一个Minimal, Complete and Verifiable Example,它允许我们重现您的错误消息。在
我们首先得到一些关于
3i SlideBook SlideBook6Reader library not found
的警告消息,但是我们can apparently ignore那么做。在您的错误消息显示
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb5' in position 1623: ordinal not in range(128)
,所以让我们看看在1623位置可以找到什么。在如果在
^{pr2}$md = bf.get_omexml_metadata(file_full_path)
之后添加print md
,则会打印出包含元数据的整个xml。让我们放大:所以,
µ
字符是罪魁祸首,它不能用'ascii' codec
编码。在回顾回溯:
我们看到在错误发生之前的行中,我们将
xml
编码为utf-8
,这应该可以解决我们的问题。那为什么不发生呢?在如果我们添加}如代码所期望的那样。。所以这是
print type(md)
,则返回<type 'unicode'>
,而不是{omexml.py
中的一个错误!在要解决这个问题,请执行以下操作(您可能需要成为root用户)
/anaconda/envs/env2_bioformats/lib/python2.7/site-packages/bioformats/
omexml.pyc
omexml.py
中,将第318行从isinstance(xml, str):
更改为if isinstance(xml, basestring):
^{} 是}的实例。在
str
和unicode
的超类。它用于测试对象是str
还是{我想为此提交一个bug,但似乎已经有了一个open issue。在
相关问题 更多 >
编程相关推荐