Page 1 of 1

Howdo I make +distort affine preserve virtual pixels?

Posted: 2019-09-21T08:08:01-07:00
by lockywolf
Hello, everyone.

I am running the following command:

Code: Select all

convert rogers.png -alpha set -virtual-pixel transparent +distort affine "1,1 50,50   1,50 50,100   50,1 100,50" output.png
I expect the image to be shifted by 50 pixels down and right, and the image to be padded with transparent ones. However, this doesn't happen, and the result of this command is the image untouched.

The virtual pixels are preserved, however, if I only rotate/stretch the image, and if at least one pixel of the original image touches the borders. (The triangles are filled with the transparent pixels as expected.)

How do I make imagemagick not trim the transparent pixels?

Re: Howdo I make +distort affine preserve virtual pixels?

Posted: 2019-09-21T08:37:42-07:00
by snibgo
I'm not sure what you want. Your command doesn't shift by 50 pixels because 1 becomes 50 (a shift of 49) but 50 becomes 100 (a shift of 50).

See the documentation at http://www.imagemagick.org/script/comma ... hp#distort :
... if you use the plus form of the operator (+distort) the operator will attempt (if possible) to show the whole of the distorted image, while retaining a correct 'virtual canvas' offset, for image layering.
You might use the minus form of distort.

Perhaps you want "-background None -layers Flatten" before the output.

Re: Howdo I make +distort affine preserve virtual pixels?

Posted: 2019-09-21T20:06:06-07:00
by lockywolf

Code: Select all

You might use the minus form of distort.
The minus form of distort doesn't enlarge the canvas.

I want to move the picture's top left corner from 1,1 to 50,50, extend the canvas to fit the image (become imsizeX+50,imsizeY+50) (hence the + form of distort) and pad with transparency.

Image

Re: Howdo I make +distort affine preserve virtual pixels?

Posted: 2019-09-21T21:06:48-07:00
by snibgo
I suggest:

Code: Select all

magick in.png -repage +50+50 -background None -layers Mosaic out.png
Or:

Code: Select all

magick in.png -background None -splice 50x50 out.png

Re: Howdo I make +distort affine preserve virtual pixels?

Posted: 2019-09-22T00:01:42-07:00
by lockywolf
Look, seriously, I appreciate your help. But when I use "+distort affine", it's not because I want to abuse it, or because I am too lazy to read the documentation. I do read it, I just don't understand it.

I use "+distort affine", because I actually need an affine distortion -- that is an affine transformation of the image reference frame. That is, a constant shift plus a linear transformation of the axis. The linear transformation of the axis is needed, even though in is unitary in the original post, it won't be unitary at the time of an actual application.

The "splice" option seems to be almost what I need, except that it pads the image with white pixels, whereas I need transparent ones.

After your recommendation I concocted the following command:

Code: Select all

convert rogers.png  -alpha set -virtual-pixel transparent +distort affine "1,1 1,1 1,50 1,50 50,1 50,50" -alpha set -virtual-pixel transparent -splice "50x50" output.png
The result is the following:

Image

This is almost what I need except I still need actual transparency in place of the white bands.

Re: Howdo I make +distort affine preserve virtual pixels?

Posted: 2019-09-22T00:32:31-07:00
by lockywolf
I seem to have nailed it. The actual code needed is the following:

Code: Select all

 convert rogers.png  -alpha set -virtual-pixel transparent +distort affine "1,1 1,1 1,50 1,50 50,1 50,50" -background transparent -splice "50x50" output.png 
I am completely confused why exactly the same thing is called "virtual pixels" for "distort" and "background" for "splice", but as long as it works, I am happy. Hopefully someone can find this thread useful.

Re: Howdo I make +distort affine preserve virtual pixels?

Posted: 2019-09-22T03:26:27-07:00
by lockywolf
No, this solution also doesn't work.

A counterexample would be trivial: just give negative pixel values to "-splice", say "-10x-10", and convert refuses to move the image.

Re: Howdo I make +distort affine preserve virtual pixels?

Posted: 2019-09-22T07:57:42-07:00
by snibgo
How about "+distort affine" which creates canvas dimensions and offsets, and then "-layers mosaic" to use that metadata to make the final image:

Code: Select all

convert in.png -alpha set -virtual-pixel transparent +distort affine "1,1 50,50   1,50 50,100   50,1 100,50" +write info: -background Blue -layers Mosaic +write out.png
I have included "+write info:" so we can see what is happening. They can both be removed.

"-background" is used by operations when an output pixel is made from some constant colour.

"-virtual-pixels" is used by operations when an output pixel is calculated from an input pixel, but the coordinates of the input are outside the image.