Howdo I make +distort affine preserve virtual pixels?

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
lockywolf
Posts: 5
Joined: 2019-09-21T08:00:51-07:00
Authentication code: 1152

Howdo I make +distort affine preserve virtual pixels?

Post 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?

snibgo
Posts: 12558
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com

lockywolf
Posts: 5
Joined: 2019-09-21T08:00:51-07:00
Authentication code: 1152

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

Post 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

snibgo
Posts: 12558
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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
snibgo's IM pages: im.snibgo.com

lockywolf
Posts: 5
Joined: 2019-09-21T08:00:51-07:00
Authentication code: 1152

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

Post 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.

lockywolf
Posts: 5
Joined: 2019-09-21T08:00:51-07:00
Authentication code: 1152

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

Post 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.

lockywolf
Posts: 5
Joined: 2019-09-21T08:00:51-07:00
Authentication code: 1152

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

Post 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.

snibgo
Posts: 12558
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com

User avatar
whugemann
Posts: 310
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

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

Post by whugemann »

I have a similar problem, trying to make a montage of several images in a more ore less horizontal row, using two fit points at each vertical image side, provided by a text file, to match every two of them. This is rather easy to perform via distort Affine @pointlist.txt, but I have to make acompanying calculations to get the right canvas size via -splice.

There seems to be no way to stop +distort from resizing the image (?), which is a pity.
Wolfgang Hugemann

Post Reply