Page 1 of 1

Extract multiple crops from a single TIFF-file

Posted: 2013-09-24T04:07:09-07:00
by sebhoff
Hi forum,
This is probably a real newbie question - but I've looked around for a good hour and can't find the answer by myself...

I have TIFF-files (about 25MB) and I'd like to extract many individual small crops (like about 2500 per file) from them based on coordinates that I have. At the moment, I've got this (almost) working via the PerlMagick API by doing this:

Code: Select all

$image = Image::Magick->new;
$x = $image->Read('filename.tif');

#and then a loop that is supposed to identify the area with the necessary geometry information:
$IDextract = $image->Crop(geometry=>"${WIDTH}x${HEIGHT}+${HPOS}+${VPOS}");
$x = $image->Write("small_tiffs/$ID.tif");
# end loop

This works for the first crop - but not the rest, because the image has been modified.
How can I keep the original image in memory so that I don't need to read in the TIFF-file from scratch for every single crop action? I suppose "crop" is not the thing to do - what is a better alternative?

(BTW: I don't mind whether this is via PerlMagick or just the command line...)

Thanks in advance for any pointers!
Sebastian

Re: Extract multiple crops from a single TIFF-file

Posted: 2013-09-24T05:32:10-07:00
by dlemstra
I think you are looking for the Clone method: http://www.imagemagick.org/script/perl- ... cellaneous

Re: Extract multiple crops from a single TIFF-file

Posted: 2013-09-24T05:55:54-07:00
by sebhoff
Great - thanks! Yes - that is much quicker...
Sebastian

Re: Extract multiple crops from a single TIFF-file

Posted: 2013-09-24T10:42:54-07:00
by snibgo
You can do this in a single IM command, if you want. The command is, for example:

Code: Select all

convert @cropr.txt NULL:
This needs a file I have named cropr.txt, which contains, for example:

Code: Select all

rose:
( +clone -crop 1x1+0+0 -write rx_0_0.png +delete )
( +clone -crop 1x1+1+0 -write rx_1_0.png +delete )
( +clone -crop 1x1+2+0 -write rx_2_0.png +delete )
:
:
( +clone -crop 1x1+65+45 -write rx_65_45.png +delete )
( +clone -crop 1x1+66+45 -write rx_66_45.png +delete )
( +clone -crop 1x1+67+45 -write rx_67_45.png +delete )
( +clone -crop 1x1+68+45 -write rx_68_45.png +delete )
( +clone -crop 1x1+69+45 -write rx_69_45.png +delete )
I have omitted about 3200 lines.

(Tested in IM v6.8.6-9 on Windows 7 for all 3220 pixels.)