Percent Escapes with Extent

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
bwroga
Posts: 9
Joined: 2019-04-27T12:45:45-07:00
Authentication code: 1152

Percent Escapes with Extent

Post by bwroga »

Is it possible to use percent escapes with the -extent option?

I would like to swap the width and height of an image, but keep everything else the same.

I tried all of the following:

Code: Select all

convert a.png -extent %hx%w d.png
convert a.png -extent '%hx%w' d.png
convert a.png -extent '%[h]x%[w]' d.png
convert a.png -extent '%[fx:h*1]x%[fx:w*1]' d.png
but I get invalid argument errors for each.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Percent Escapes with Extent

Post by fmw42 »

Only in IM 7.

Should be OK, but system dependent whether quotes are needed and may require double quotes.

Code: Select all

magick a.png -extent %hx%w d.png
magick a.png -extent '%hx%w' d.png
This is not allowed syntax:

Code: Select all

magick a.png -extent '%[h]x%[w]' d.png
No need to multiply by 1 in this syntax:

Code: Select all

magick a.png -extent '%[fx:h*1]x%[fx:w*1]' d.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Percent Escapes with Extent

Post by snibgo »

As Fred says, only with IM v7. Ensure you are using v7, and use "magick", not "convert".
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Percent Escapes with Extent

Post by GeeMack »

bwroga wrote: 2019-05-03T15:49:40-07:00Is it possible to use percent escapes with the -extent option?

I would like to swap the width and height of an image, but keep everything else the same.
As mentioned, the FX expressions are most versatile using IM7, but it can be done with IM6 using "-distort" and setting a viewport. Try this...

Code: Select all

convert a.png -set option:distort:viewport %[h]x%[w] -virtual-pixel none \
   -distort affine "%[fx:w/2],%[fx:h/2] %[fx:h/2],%[fx:w/2]" result.png
There are many options available for that virtual-pixel setting, which defines what IM does with the space not filled with any image after the distort. I used "none" which leaves a transparent background. You could use "black" or "white" or set a background color and use "-virtual-pixel background".
bwroga
Posts: 9
Joined: 2019-04-27T12:45:45-07:00
Authentication code: 1152

Re: Percent Escapes with Extent

Post by bwroga »

I think I'll go ahead and upgrade, I was using 6.9.7-4. Thank you everyone.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Percent Escapes with Extent

Post by fmw42 »

Just to be clear, GeeMack is providing an IM 6 alternative that does the same as -extent (though not as straight-forward), but using -distort Affine. In IM7 6, you can only use % escapes in a few functions including -distort, but not -extent.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Percent Escapes with Extent

Post by fmw42 »

bwroga wrote: 2019-05-03T16:30:23-07:00 I think I'll go ahead and upgrade, I was using 6.9.7-4. Thank you everyone.
For differences, see https://imagemagick.org/script/porting.php#cli
Post Reply