在Python中调整大图片的大小(大于可用的RAM) [英] Resize huge images in Python (bigger than available RAM)

查看:128
本文介绍了在Python中调整大图片的大小(大于可用的RAM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将巨大的(1 TB)图像调整为256x256像素的图块(Zoomify/OSM/Google Maps/XYZ模式).图片将采用BigTIFF或PSB(大文档格式或Photoshop Big)格式.

I would like to resize and split huge (1 TB) images to 256x256 pixel tiles (Zoomify / OSM / Google Maps / XYZ schema). Images would be in BigTIFF or PSB (Large Document Format or Photoshop Big) format.

有哪些可用的库可以做到这一点?我正在查看GDAL,但它会产生非常模糊的输出,因此我无法将其设置为更好的插值方式.理想情况下,我会考虑使用Lanczos插值器执行此任务.

What are the available libraries which can do this? I was having a look at GDAL but it was producing quite blurry output and I couldn't set it to interpolate better. Ideally I'd be looking at a Lanczos interpolator for such task.

是否有任何本机Python库或基于C的库的包装程序可以做到这一点? imagemagick的Python包装器可以做这种事情吗?

Are there any native Python libraries, or wrappers for C based libraries which can do this? Can the Python wrapper for imagemagick do such thing?

如果没有可用的Python库,我还将开放基于命令行的工具,这些工具可以使用Python自动化.

If no Python library is available, I'm also open for command line based tools, which I can automate using Python.

推荐答案

libvips 可以处理大量(大于RAM)的图像.它是一个流式图像处理库,因此(在这种情况下)它可以同时解压缩,调整大小,平铺和写入所有内容,而无需将整个图像存储在内存中或需要任何临时文件.

libvips can process huge (larger than RAM) images efficiently. It's a streaming image processing library, so it can (in this case) decompress, resize, tile, and write all at the same time, and without having the whole image in memory or needing any temporary files.

dzsave运算符将编写DeepZoom/Zoomify/Google Maps金字塔.您可以从命令行像这样运行它:

The dzsave operator will write a DeepZoom / Zoomify / Google Maps pyramid. You can run it from the command-line like this:

$ vipsheader y.tif
y.tif: 104341x105144 uchar, 3 bands, srgb, tiffload
$ ls -l y.tif
-rw-r--r-- 1 john john 32912503796 Jun 13 13:31 y.tif
$ time vips dzsave y.tif x
real    3m4.944s
user    9m21.372s
sys 7m20.232s
peak RES: 640mb
$ ls -R x_files/ | wc
 227190  227172 2784853

因此,在我的桌面上,它在大约3分钟内将32GB的图像转换为230,000个图块.这是带有机械硬盘的,而使用固态硬盘则可能更快.在介绍dzsave的文档中,有一个章节 .

So on my desktop it converted a 32GB image to 230,000 tiles in about 3 minutes. That's with a mechanical HDD, it might be quicker with a SSD. There's a chapter in the docs introducing dzsave.

它具有 Python绑定,因此您还可以编写:

It has a Python binding, so you could also write:

import pyvips

image = pyvips.Image.new_from_file("y.tif", access="sequential")
image.dzsave("x")

access选项告诉libvips它应该流式传输图像.它可以读取BigTIFF和PSB.您会发现BigTIFF更快,并且需要更少的内存.

The access option tells libvips that it should stream the image. It can read both BigTIFF and PSB. You'll find BigTIFF is a lot quicker and needs much less memory.

这篇关于在Python中调整大图片的大小(大于可用的RAM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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