Search code examples
bashimagemagickcropimagemagick-convert

Imagemagick crop loop not working well


I need to cut 16 images out from one.
All images in .tif format. Coordinates of top left points of each images contains in text file.
It's like

100,200
300,400
...

I used this bash code

IFS=','
while read x y; do 
convert image.tif -crop 262x262+$x+$y image_%02d.tif; 
done < coordinates

And it's give me 395 images with wrong coordinates.
I use Ubuntu 14.04, Imagemagick 6.7.7
Please any help.


Solution

  • Try this - you don't have any variable to represent the %02d in your output file specifier.

    #!/bin/bash
    i=1
    IFS=','
    while read x y; do 
    name=$(printf "image%02d.tif" $i)
    convert image.tif -crop 262x262+$x+$y "$name"
    ((i++))
    done < coordinates
    

    If you don't really need the images to be called image01.tif and image02.tif, and image1.tif, image2.tif is ok, you can simply use this

    #!/bin/bash
    i=1
    IFS=','
    while read x y; do 
    convert image.tif -crop 262x262+$x+$y image$i.tif
    ((i++))
    done < coordinates