问题描述
我正在使用带有以下代码的 deepzoom python API-
import os
import deepzoom
# Specify your source image
SOURCE = "https://drive.google.com/uc?export=view&id=1W5UKlO_wZJLNvqsyC0E3zJ5CVlSDp_PG"
DEST = "D:\\mesh.dzi"
# Create Deep Zoom Image creator with weird parameters
creator = deepzoom.ImageCreator(
tile_size=128,tile_overlap=2,tile_format="png",image_quality=0.8,resize_filter="bicubic",)
# Create Deep Zoom image pyramid from source
creator.create(SOURCE,DEST)
请注意,我使用的是 Windows 10 操作系统和 Jupyter Notebook 来运行代码。但显示以下错误-
---------------------------------------------------------------------------
UnidentifiedImageError Traceback (most recent call last)
<ipython-input-14-6b5c22482c62> in <module>
1 # Create Deep Zoom image pyramid from source
----> 2 creator.create(SOURCE,DEST)
~\anaconda3\lib\site-packages\deepzoomtools-2.0.0a2-py3.8.egg\deepzoom\__init__.py in
create(self,source,destination)
411 def create(self,destination):
412 """Creates Deep Zoom image from source file and saves it to destination."""
--> 413 self.image = PIL.Image.open(safe_open(source))
414 width,height = self.image.size
415 self.descriptor = DeepZoomImageDescriptor(
~\anaconda3\lib\site-packages\PIL\Image.py in open(fp,mode,formats)
2941 for message in accept_warnings:
2942 warnings.warn(message)
-> 2943 raise UnidentifiedImageError(
2944 "cannot identify image file %r" % (filename if filename else fp)
2945 )
UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000001FE552ED9F0>
解决方法
改为尝试 libvips
。 libvips
处理大图像没有问题。
您可以使用 pyvips 从 URL 中读取图像,如下所示:
#!/usr/bin/python3
import urllib.request
import pyvips
URL = 'http://www.rollthepotato.net/~john/IMG_2420.JPG'
input_file = urllib.request.urlopen(URL)
# pyvips will use this to fetch bytes from the URL
def read_handler(size):
return input_file.read(size)
# 'sequential' means stream the image during processing
source = pyvips.SourceCustom()
source.on_read(read_handler)
image = pyvips.Image.new_from_source(source,'',access='sequential')
print('writing mesh.dzi ...')
image.dzsave('mesh')
这将从服务器流式传输图像并生成 DZI,而无需先下载。
不过,您需要弄清楚如何从 gdrive 获取正确的 URL。