-crop to -page

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
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

-crop to -page

Post by GreenKoopa »

I would like to save a crop geometry for a future crop. This is the only solution I can think of. Any others? Page offsets may alternatively be positive.

Code: Select all

: Save an image with an area of interest specified.
convert wizard: -repage 70x70-200-104 a.png
: Verify that the page information has been saved.
identify a.png
: Crop away everything outside the area of interest.
convert a.png -background none -flatten b.png
Why? I have two ideas, neither of which may make any sense. One idea is to save a photo with information for later cropping. But jpeg doesn't save page information, so my solution is limited. The other idea is that by postponing the crop, blurs, sharpens, an other area effects have better "virtual pixels" to work with. Now that I say it, maybe this isn't often better?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: -crop to -page

Post by snibgo »

Sadly, there is no user-defined metadata available in most image file formats for storing the instructions for our favourite crops, colour shifts, etc.

Personally, I store this data in the form of a Windows script that does all the processing from a a raw camera file to the final product(s).
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -crop to -page

Post by fmw42 »

If you have cropped an image and do not specify +repage, then for many image formats (but not JPG for example), the page offsets and image size are stored with the image. Not all image formats store that, though. PNG and GIF definitely do as well as IM TIFF and MPC. I am not sure about TIFF. For the ones that do, you can then access them using string formats, %O, %P, %w, %h. See http://www.imagemagick.org/script/escape.php
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: -crop to -page

Post by anthony »

PNG images saves all meta data that is a normal image "-set" You don't have to store it as 'virtual canvas and offset' in the image which can confuse other programs. For example some old web browsers go crazy on images with negative offsets.

Code: Select all

convert wizard: -set my:crop 70x70+200+104 a.png
identify -format '%[my:crop]' a.png
70x70-200-104
Unfortunately IMv6 does not understand escape sequences in all arguments, so you need to extract the information from the image as a separate shell/DOS command. Here I use the above 'identify' as a UNIX shell command substitution.

Code: Select all

convert a.png     -crop `identify -format '%[my:crop]' a.png`    show:
But you can cheat by using a no-op distort viewport to do that crop in one command.

Code: Select all

convert a.png   -set option:distort:viewport '%[my:crop]'   -distort SRT 0    show:
IMv7 "magick" command does understand percent escapes in almost all options (Not in FX Escapes yet ;( )

Code: Select all

magick a.png -crop '%[my:crop]' show:

Now while PNG (and MIFF) can save all 'set' metadata, JPEG and GIF can not. Thye do however have comments.

For examples see IM Examples, Montage, Using Saved Image MetaData
http://www.imagemagick.org/Usage/montage/#metadata
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply