ImageMagick crop a huge image

I'm trying to create tiles from a huge image, say 40000x40000

I found a script on line for imagemagick, it cuts tiles. It works great on small images, e.g. 10000x5000

as soon as I get more, it ends up using a lot of memory and the computer dies.

I added restriction options, but they don't seem to affect

I have a monitor, but it does not help, because the script just slows down and locks the machine

it seems like it just gobbles like a 50gig swap disk and then kills the machine

I think the problem is that since it cuts off every plate, it stores them in memory. It seems to me that I need him to write each fragment to disk, since he creates it without storing them in memory.

here is the script

#!/bin/bash file=$1 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 
+4
bash image image-processing imagemagick
source share
4 answers

After repeatedly digging and some help from the guys on the ImageMagick forum, I managed to get it to work.

The trick for its work is the .mpc format . Since this is the native image format used by ImageMagick, it does not need to convert the original image, it just cuts out the piece that it needs. This refers to the second setup of script I.

Suppose you have an image of 50000x50000 .tif called myLargeImg.tif . First convert it to your own image format using the following command:

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

Then run below bash script to create tiles. Create a file called tiler.sh in the same folder as the mpc image, and put the script below:

  #!/bin/bash src=$1 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 command below and watch how the tiles appear one at a time in your folder.

  sh ./tiler.sh myLargeImg.mpc 
+7
source share

libvips now has an operator that can do exactly what you want very quickly. It can also do this in relatively small memory: I regularly process 200,000 x 200,000 pixel slides using less than 1 GB of memory.

See this answer .

 $ 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 1 --tile-size 512 --overlap 0 --suffix .tif real 0m1.643s user 0m1.668s sys 0m1.000s 
+6
source share

You can try using the gdal_translate utility from GDAL . Do not be alarmed by the "geospatial" in the name of the project. GDAL is an advanced library for accessing and processing raster data from various formats. It is intended for geospatial users, but it can also be used to process conventional images without any problems.

Here is just a script to create 256x256 pixel fragments from a large in.tif file in.tif size of 40000x40000 pixels:

 #!/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 downloadable .

+2
source share

ImageMagick is simply not designed for this kind of task. In situations like yours, I recommend using the VIPS library and the corresponding Nip2 interface

VIPS is specifically designed to handle very large images.

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

+1
source share

All Articles