ImageMagick crop huge image

后端 未结 4 690
南笙
南笙 2020-12-29 17:48

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

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

相关标签:
4条回答
  • 2020-12-29 18:14

    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/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.

    0 讨论(0)
  • 2020-12-29 18:28

    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

    VIPS has been designed specifically to deal with very large images.

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

    0 讨论(0)
  • 2020-12-29 18:37

    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
    
    0 讨论(0)
  • 2020-12-29 18:38

    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.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
    
    0 讨论(0)
提交回复
热议问题