Extract multiple crops from a single TIFF-file

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
sebhoff
Posts: 2
Joined: 2013-09-24T03:54:23-07:00
Authentication code: 6789

Extract multiple crops from a single TIFF-file

Post 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
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Extract multiple crops from a single TIFF-file

Post by dlemstra »

I think you are looking for the Clone method: http://www.imagemagick.org/script/perl- ... cellaneous
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
sebhoff
Posts: 2
Joined: 2013-09-24T03:54:23-07:00
Authentication code: 6789

Re: Extract multiple crops from a single TIFF-file

Post by sebhoff »

Great - thanks! Yes - that is much quicker...
Sebastian
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extract multiple crops from a single TIFF-file

Post 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.)
snibgo's IM pages: im.snibgo.com
Post Reply