bash ImageMagick 裁剪巨幅图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10542246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:15:05  来源:igfitidea点击:

ImageMagick crop huge image

imagebashimage-processingimagemagick

提问by abe

I am trying to create tiles from a huge image say 40000x40000

我正在尝试从一个巨大的图像创建瓷砖,比如 40000x40000

i found a script on line for imagemagick he crops the tiles. it works fine on small images like say 10000x5000

我在网上找到了一个用于 imagemagick 的脚本,他裁剪了瓷砖。它适用于像 10000x5000 这样的小图像

once i get any bigger it ends up using to much memory and the computer dies.

一旦我变得更大,它最终会使用太多内存并且计算机死机。

I have added the limit options but they dont seem to take affect

我添加了限制选项,但它们似乎没有生效

i have the monitor in there but it does not help as the script just slows down and locksup the machine

我在那里有显示器,但它没有帮助,因为脚本只会减慢速度并锁定机器

it seems to just goble up like 50gig of swap disk then kill the machine

它似乎只是吞下 50gig 的交换磁盘然后杀死机器

i think the problem is that as it crops each tile it keeps them in memory. What i think i needs is for it to write each tile to disk as it creates it not store them all up in memory.

我认为问题在于,当它裁剪每个瓷砖时,它会将它们保存在内存中。我认为我需要的是将每个磁贴写入磁盘,因为它创建它而不是将它们全部存储在内存中。

here is the script so far

这是到目前为止的脚本

 #!/bin/bash
 file=
 function tile() {
 convert -monitor -limit memory 2GiB -limit map 2GiB -limit area 2GB $file -scale ${s}%x -crop 256x256 \
 -set filename:tile "%[fx:page.x/256]_%[fx:page.y/256]" \
 +repage +adjoin "${file%.*}_${s}_%[filename:tile].png"
 }
 s=100
 tile
 s=50
 tile

回答by abe

After a lot more digging and some help from the guys on the ImageMagick forumI managed to get it working.

经过更多的挖掘和ImageMagick 论坛上的一些人的帮助,我设法让它工作。

The trick to getting it working is the .mpcformat. Since this is the native image format used by ImageMagick it does not need to convert the initial image, it just cuts out the piece that it needs. This is the case with the second script I setup.

让它工作的诀窍是.mpcformat。由于这是 ImageMagick 使用的原生图像格式,因此它不需要转换初始图像,它只是切出它需要的部分。我设置的第二个脚本就是这种情况。

Lets say you have a 50000x50000.tifimage called myLargeImg.tif. First convert it to the native image format using the following command:

假设您有一个50000x50000.tif名为myLargeImg.tif. 首先使用以下命令将其转换为原生图像格式:

 convert -monitor -limit area 2mb myLargeImg.tif myLargeImg.mpc

Then, run the bellow bash script that will create the tiles. Create a file named tiler.shin the same folder as the mpc image and put the below script:

然后,运行将创建磁贴的波纹管 bash 脚本。tiler.sh在与 mpc 图像相同的文件夹中创建一个文件,并放置以下脚本:

 #!/bin/bash
 src=
 width=`identify -format %w $src`
 limit=$[$width / 256]
 echo "count = $limit * $limit = "$((limit * limit))" tiles"
 limit=$((limit-1))
 for x in `seq 0 $limit`; do
   for y in `seq 0 $limit`; do
     tile=tile-$x-$y.png
     echo -n $tile
     w=$((x * 256))
     h=$((y * 256))
     convert -debug cache -monitor $src -crop 256x256+$w+$h $tile
   done
 done

In your console/terminal run the below command and watch the tiles appear one at at time into your folder.

在您的控制台/终端中运行以下命令,并观察磁贴一次出现在您的文件夹中。

 sh ./tiler.sh myLargeImg.mpc

回答by jcupitt

libvipshas an operator that can do exactly what you want very quickly. There's a chapter in the docs introducing dzsave and explaining how it works.

libvips有一个运算符,可以非常快速地完成您想要的操作。文档中有一个章节介绍了 dzsave 并解释了它是如何工作的

It can also do it in relatively little memory: I regularly process 200,000 x 200,000 pixel slide images using less than 1GB of memory.

它还可以在相对较少的内存中完成:我经常使用不到 1GB 的内存处理 200,000 x 200,000 像素的幻灯片图像。

See this answer, but briefly:

请参阅此答案,但简要说明:

$ time convert -crop 512x512 +repage huge.tif x/image_out_%d.tif
real    0m5.623s
user    0m2.060s
sys     0m2.148s
$ time vips dzsave huge.tif x --depth one --tile-size 512 --overlap 0 --suffix .tif
real    0m1.643s
user    0m1.668s
sys     0m1.000s

回答by mloskot

You may try to use gdal_translateutility from GDALproject. Don't get scared off by the "geospatial" in the project name. GDAL is an advanced library for access and processing of raster data from various formats. It is dedicated to geospatial users, but it can be used to process regular images as well, without any problems.

您可以尝试使用GDAL项目中的gdal_translate实用程序。不要被项目名称中的“地理空间”吓到。GDAL 是一个高级库,用于访问和处理各种格式的栅格数据。它专供地理空间用户使用,但也可用于处理常规图像,没有任何问题。

Here is simple script to generate 256x256 pixel tiles from large in.tiffile of dimensions 40000x40000 pixels:

这是从in.tif尺寸为 40000x40000 像素的大文件生成 256x256 像素图块的简单脚本:

#!/bin/bash
width=40000
height=40000
y=0
while [ $y -lt $height ]
do
   x=0
   while [ $x -lt $width ]
   do
      outtif=t_${y}_$x.tif
      gdal_translate -srcwin $x $y 256 256 in.tif $outtif
      let x=$x+256
   done
   let y=$y+256
done

GDAL binaries are available for most Unix-like systems as well as Windows are downloadable.

GDAL 二进制文件可用于大多数类 Unix 系统,Windows 也可下载

回答by datenwolf

ImageMagick is simply not made for this kind of task. In situations like yours I recommend using the VIPS library and the associated frontend Nip2

ImageMagick 根本不适合这种任务。在像您这样的情况下,我建议使用 VIPS 库和相关的前端 Nip2

VIPS has been designed specifically to deal with verylarge images.

VIPS 专为处理非常大的图像而设计。

http://www.vips.ecs.soton.ac.uk/index.php?title=VIPS

http://www.vips.ecs.soton.ac.uk/index.php?title=VIPS