extent vs define distort:viewport

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
tom_dl
Posts: 43
Joined: 2015-02-26T08:25:44-07:00
Authentication code: 6789

extent vs define distort:viewport

Post by tom_dl »

I'm running Version: ImageMagick 7.0.7-8 Q16 x64 2017-10-14 in Powershell 5.1.15063.674 on Windows 10 64-bit

I've noticed strange behaviour when using -extent which I have worked-around using -define distort:viewport. This is the line I use which produces an unwanted result:

Code: Select all

convert input.jpg `( -clone 0 -gamma 0 -fill white -stroke white -draw "polygon 2347,1536 3648,3789 3648,0 2347,0" `) -alpha off -compose CopyOpacity -composite -virtual-pixel transparent -extent 3648x5472+523-1200 +repage .\output.png
And this is the line which produces the wanted result:

Code: Select all

convert input.JPG `( -clone 0 -gamma 0 -fill white -stroke white -draw "polygon 2347,1536 3648,3789 3648,0 2347,0" `) -alpha off -compose CopyOpacity -composite -virtual-pixel transparent -define distort:viewport=3648x5472+523-1200 -filter point -distort srt 0 +repage .\output.png
Weirdly, if I only run the first line up to the end of "-composite" and save to disk, then load that image and run the rest of the line (i.e. convert output.png -virtual-pixel transparent -extent 3648x5472+523-1200 +repage output.png) then it works just like the second line.

My two questions are, why does the distort viewport option work, but the extent option not; and why does the extent option not work "in-line" but does does work when saved to disk and reloaded?

This example works with a jpeg made like this:

Code: Select all

convert -size 3648x5472 xc:red input.jpg
Thanks in advance for any suggestions/answers. Let me know if I should have posted this in bugs!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: extent vs define distort:viewport

Post by fmw42 »

Post your input.jpg so we can test. But what is the point of -gamma 0. I do not think that is a good idea (since it also changes the gamma value associated with the image). If you want to make the image black (and keep it gamma=0.4545 non-linear), use

Code: Select all

-clone 0 -fill black -colorize 100 
Also if you want to extent an image with transparency, use background none -extent

Also you have to reset the -compose method from copyopacity to over or it will affect the extent.

So try (Unix syntax)

Code: Select all

convert logo: \
\( -clone 0 -fill black -colorize 100 -fill white -stroke white -draw "polygon 200,200 200,400 400,400 400,200" \) \
-alpha off -compose CopyOpacity -composite \
-compose over -background none -extent 1000x1000+50+50 +repage output.png
Or with your image:

Code: Select all

convert input.jpg \
\( -clone 0 -fill black -colorize 100 -fill white -stroke white -draw "polygon 2347,1536 3648,3789 3648,0 2347,0" \) \
-alpha off -compose CopyOpacity -composite \
-compose over -background none -extent 3648x5472+523-1200 +repage output.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: extent vs define distort:viewport

Post by fmw42 »

P.S. If you are only going to use a rectangle, you could also just crop the image and extent it or insert it into a transparent background of the desired size. If the polygon is not a simple rectangle, then your method should still work.
tom_dl
Posts: 43
Joined: 2015-02-26T08:25:44-07:00
Authentication code: 6789

Re: extent vs define distort:viewport

Post by tom_dl »

Thanks for your reply! nb the last part of my post:
This example works with a jpeg made like this:
convert -size 3648x5472 xc:red input.jpg
Thanks for your advice on making the image black. I got the idea from the ImageMagick Usage page: http://www.imagemagick.org/Usage/canvas/#other

The advice about the -compose method affecting the extent was what fixed this for me.

I had no idea -compose affected -extent, although having looked at the command line options page (http://www.imagemagick.org/script/comma ... php#extent) it does say that!

Thanks again!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: extent vs define distort:viewport

Post by fmw42 »

Thanks for your advice on making the image black. I got the idea from the ImageMagick Usage page: http://www.imagemagick.org/Usage/canvas/#other
Yes, it does show that. But I try not to use -gamma since at least at one time, it changed the gamma value stored with the image as well as applying the gamma. I just checked with

Code: Select all

convert logo: -gamma 1 logo.png
And now it keeps the gamma value as 0.4545, rather than 1. So it is not doing it any more.

As you can see from that post, there are many ways to turn an image black, including

-evaluate set 0
Post Reply