【发布时间】:2021-03-14 14:37:20
【问题描述】:
我正在使用 Tifffile 加载 .tiff-image 文件,然后将它们转换为 PIL 图像以进行处理。我使用此解决方法是因为其他方法在某些特定 TIFF 图像上存在问题。在我的本地机器上,以下代码运行良好。但是当我在 SageMaker 环境中运行代码时,它不起作用
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
import scipy.ndimage
import os
import random
import tifffile
from PIL import Image
from PIL.ExifTags import TAGS
import matplotlib
#import imagecodecs
# Causes errors with some pictures
#image = np.expand_dims(scipy.ndimage.imread(image_path), 0)
# Causes errors with some pictures
#image = np.expand_dims(matplotlib.pyplot.imread(image_path), 0)
# This works on my local machine, but not in Amazon SageMaker
# Use tifffile to load the image
img = tifffile.imread(image_path)
# Make into "PIL Image" and carry on as usual
image = Image.fromarray(img)
我收到以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-137-fe721f47a3dc> in <module>
----> 1 batch_image_augmentation(test_images, 20)
<ipython-input-134-b0ca28d40a9c> in batch_image_augmentation(path_list, n)
3 target_dir = "./Test/" + path[:-5] + "_AUG"
4 print(path)
----> 5 augment_image(path, target_dir, n)
<ipython-input-131-cdef2a00cd5f> in augment_image(image_path, target_dir, n)
24 #Some images cause problems, so I try the same workaround as in the PNG-conversion script
25 # Use tifffile to load the image
---> 26 img = tifffile.imread(image_path)
27 # Make into "PIL Image" and carry on as usual
28 image = Image.fromarray(img)
/usr/local/lib/python3.6/dist-packages/tifffile/tifffile.py in imread(files, **kwargs)
719 if isinstance(files, (str, os.PathLike)) or hasattr(files, 'seek'):
720 with TiffFile(files, **kwargs_file) as tif:
--> 721 return tif.asarray(**kwargs)
722
723 with TiffSequence(files, **kwargs_seq) as imseq:
/usr/local/lib/python3.6/dist-packages/tifffile/tifffile.py in asarray(self, key, series, level, out, maxworkers)
2805 typecode, product(series.shape), out=out)
2806 elif len(pages) == 1:
-> 2807 result = pages[0].asarray(out=out, maxworkers=maxworkers)
2808 else:
2809 result = stack_pages(pages, out=out, maxworkers=maxworkers)
/usr/local/lib/python3.6/dist-packages/tifffile/tifffile.py in asarray(self, out, squeeze, lock, reopen, maxworkers)
5646
5647 for _ in self.segments(
-> 5648 func=func, lock=lock, maxworkers=maxworkers, sort=True
5649 ):
5650 pass
/usr/local/lib/python3.6/dist-packages/tifffile/tifffile.py in segments(self, lock, maxworkers, func, sort)
5510 *self._offsetscounts, lock=lock, sort=sort, flat=True
5511 ):
-> 5512 yield decode(segment)
5513 else:
5514 # reduce memory overhead by processing chunks of up to
/usr/local/lib/python3.6/dist-packages/tifffile/tifffile.py in decode(args, decodeargs, keyframe, func)
5499
5500 def decode(args, decodeargs=decodeargs, keyframe=keyframe, func=func):
-> 5501 result = keyframe.decode(*args, **decodeargs)
5502 if func is not None:
5503 return func(result)
/usr/local/lib/python3.6/dist-packages/tifffile/tifffile.py in decode(exc, *args, **kwargs)
5228 except KeyError as exc:
5229 def decode(*args, exc=str(exc)[1:-1], **kwargs):
-> 5230 raise ValueError(f'TiffPage {self.index}: {exc}')
5231 return cache(decode)
5232
ValueError: TiffPage 0: <COMPRESSION.LZW: 5> requires the 'imagecodecs' package
当我尝试安装 imagecodecs 时,pip 告诉我它已经安装:
bash-4.2$ pip install imagecodecs
Requirement already satisfied: imagecodecs in /opt/conda/lib/python3.7/site-packages (2020.5.30)
Requirement already satisfied: numpy>=1.15.1 in /opt/conda/lib/python3.7/site-packages (from imagecodecs) (1.19.4)
bash-4.2$
但无论如何,如果我将 import imagecodecs 添加到导入中,我会收到以下错误:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-138-920d3e93091b> in <module>
8 from PIL.ExifTags import TAGS
9 import matplotlib
---> 10 import imagecodecs
11
12 # https://gis.stackexchange.com/questions/365950/how-can-i-solve-this-error-from-tiff-file
ModuleNotFoundError: No module named 'imagecodecs'
有人知道这个问题的解决方案吗?
【问题讨论】:
-
您正在混用并且无法匹配您的
pip和python版本。查看来自pip install imagecodecs的错误消息,您会看到它是为 Python 3.7 安装的,但其余错误消息表明您正在运行 Python 3.6 -
谢谢,我将整个环境更改为 Pyhon 3.7,并在导入前添加了
!pip install imagecodecs。现在我不再收到导入错误,但我仍然收到与ValueError: TiffPage 0: <COMPRESSION.LZW: 5> requires the 'imagecodecs' package相同的错误消息堆栈
标签: python image-processing python-imaging-library tiff