ImageMagick crop huge image ImageMagick crop huge image bash bash

ImageMagick crop huge image


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

The trick to getting it working is the .mpc format. 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.

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

 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.sh in the same folder as the mpc image and put the below script:

 #!/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 below command and watch the tiles appear one at at time into your folder.

 sh ./tiler.sh myLargeImg.mpc


libvips has 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.

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.

See this answer, but briefly:

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


You may try to use gdal_translate utility from GDAL project. 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.

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

#!/bin/bashwidth=40000height=40000y=0while [ $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+256done

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