Page 1 of 1

Percent Escapes with Extent

Posted: 2019-05-03T15:49:40-07:00
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.

Re: Percent Escapes with Extent

Posted: 2019-05-03T15:55:24-07:00
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

Re: Percent Escapes with Extent

Posted: 2019-05-03T16:05:01-07:00
by snibgo
As Fred says, only with IM v7. Ensure you are using v7, and use "magick", not "convert".

Re: Percent Escapes with Extent

Posted: 2019-05-03T16:16:58-07:00
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".

Re: Percent Escapes with Extent

Posted: 2019-05-03T16:30:23-07:00
by bwroga
I think I'll go ahead and upgrade, I was using 6.9.7-4. Thank you everyone.

Re: Percent Escapes with Extent

Posted: 2019-05-03T16:31:47-07:00
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.

Re: Percent Escapes with Extent

Posted: 2019-05-03T16:47:12-07:00
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