Combining multiple images using -distort

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?".
blxxy
Posts: 6
Joined: 2013-10-05T03:48:51-07:00
Authentication code: 6789

Combining multiple images using -distort

Post by blxxy »

hi,

I'm trying to render multiple images into one output image and simultaneously apply a -distort method to each individual source image.

my current attempt:

Code: Select all

convert -extent 560x316 -define png:color-type=6 -mattecolor transparent -background transparent -matte -virtual-pixel transparent ( input1.png -distort BilinearForward 0,0,547.997,138.263,0,200,543.684,189.479,200,0,601.669,138.875,200,200,598.019,192.506 -virtual-pixel transparent ) ( input2.png -distort BilinearForward 0,0,551.986,100.492,0,200,549.579,135.608,200,0,620.311,101.066,200,200,617.908,137.361 -virtual-pixel transparent ) -flatten output.png
In my understanding, this should work, but it doesn't. It shows only one distorted source image in the outputted image.

Any ideas on how I can get this to work?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Combining multiple images using -distort

Post by snibgo »

Commands like this are easier to read when split into lines. For windows, use "^" at the end of lines. For Unix etc use "\".

Code: Select all

convert ^
-extent 560x316 ^
-define png:color-type=6 ^
-mattecolor transparent ^
-background transparent ^
-matte ^
-virtual-pixel transparent ^
( input1.png ^
  -distort BilinearForward 0,0,547.997,138.263,0,200,543.684,189.479,200,0,601.669,138.875,200,200,598.019,192.506 ^
  -virtual-pixel transparent ^
  -write tryA.png ^
) ^
( input2.png ^
  -distort BilinearForward 0,0,551.986,100.492,0,200,549.579,135.608,200,0,620.311,101.066,200,200,617.908,137.361 ^
  -virtual-pixel transparent ^
  -write tryB.png ^
) ^
-flatten ^
output.png
I have inserted two debugging commands. Do tryA.png and tryB.png look as they should?

The documentation (http://www.imagemagick.org/script/comma ... mattecolor) says "-mattecolor" specifies a colour for "-frame", but you have no frame. "-matte" is not a documented command. What do you think it does?
snibgo's IM pages: im.snibgo.com
blxxy
Posts: 6
Joined: 2013-10-05T03:48:51-07:00
Authentication code: 6789

Re: Combining multiple images using -distort

Post by blxxy »

hi snibgo,

I actually read about the linesplit characters just after posting my question. I'm still pretty new to IM and will remember to add those in the future.

The "-mattecolor transparent" was something used here; http://stackoverflow.com/questions/1138 ... tual-pixel, to avoid non-transparent parts in the output image.

tryA.png looks good. tryB.png is empty and is 357x476 instead of 560x316.
The output image still only displays one (the first) image.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Combining multiple images using -distort

Post by fmw42 »

Perhaps you should post a link to your two input images.
blxxy
Posts: 6
Joined: 2013-10-05T03:48:51-07:00
Authentication code: 6789

Re: Combining multiple images using -distort

Post by blxxy »

hi fmw42,

my input images are plain png images at 357x476 saved for the web from Photoshop, nothing special about them.

I can understand your concern but I'm actually running the command for multiple frames.
I've only renamed them in my query to imageA and imageB to make the problem clear.

I ran both sequences individually and they both come out as they should, so the input images are ok, as well as the distortion coordinates.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Combining multiple images using -distort

Post by snibgo »

When I try this:

Code: Select all

convert -size 357x475 gradient: input1.png

convert -size 357x475 gradient:red-blue input2.png

convert ^
-extent 560x316 ^
-define png:color-type=6 ^
-mattecolor transparent ^
-background transparent ^
-matte ^
-virtual-pixel transparent ^
( input1.png ^
  -distort BilinearForward 0,0,547.997,138.263,0,200,543.684,189.479,200,0,601.669,138.875,200,200,598.019,192.506 ^
  -virtual-pixel transparent ^
  -write tryA.png ^
) ^
( input2.png ^
  -distort BilinearForward 0,0,551.986,100.492,0,200,549.579,135.608,200,0,620.311,101.066,200,200,617.908,137.361 ^
  -virtual-pixel transparent ^
  -write tryB.png ^
) ^
-flatten ^
output.png
... I can see both inputs within the output. tryB.png isn't empty.

"-extent" modifies an image. I don't know what it does when there is no image. Perhaps you want it at the end. Or perhaps you want to extend each subimage.

Can you supply input images, and the expected output?
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Combining multiple images using -distort

Post by fmw42 »

Not sure what you mean by multiply frames. But PNG does not support multiple layers.

-extent must follow an input image and would need -background color set if you don't want the default (white?)

Sorry snibgo, I see you are on top of this, so I will leave it to you to work out with the OP.
blxxy
Posts: 6
Joined: 2013-10-05T03:48:51-07:00
Authentication code: 6789

Re: Combining multiple images using -distort

Post by blxxy »

thanks for your efforts, really appreciate it.

I ran your commands (using the generated gradient input images) and got the same results I had before.
The first input (black and white gradient) is shown, the second input is not. tryB.png was still empty in my case.

considering you are seeing both inputs on the output image I might be running an older version of IM.

I'm running;
Version: ImageMagick 6.6.9-7 2012-08-17 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
Features: OpenMP

Could it be I'm running an old version?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Combining multiple images using -distort

Post by fmw42 »

6.6.9-7 is about 173 versions old compared to the current version at 6.8.7.0

snibgo's code converted to unix syntax runs for me on IM 6.8.7.0 Q16 Mac OSX. Note I moved the -extent after the -background, but am not sure what it is doing when specified before an image.


convert \
-define png:color-type=6 \
-mattecolor transparent \
-background transparent \
-extent 560x316 \
-matte \
-virtual-pixel transparent \
\( input1.png \
-distort BilinearForward 0,0,547.997,138.263,0,200,543.684,189.479,200,0,601.669,138.875,200,200,598.019,192.506 \
-virtual-pixel transparent \
-write tryA.png \
\) \
\( input2.png \
-distort BilinearForward 0,0,551.986,100.492,0,200,549.579,135.608,200,0,620.311,101.066,200,200,617.908,137.361 \
-virtual-pixel transparent \
-write tryB.png \
\) \
-flatten \
output.png

tryA.png is mostly transparent with just a little of the gradient on the right. tryB.png is a red-blue gradient but different from the input. The output has parts of tryA on the left and tryB on the right.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Combining multiple images using -distort

Post by snibgo »

On 6.8.7-0, my results are consistent with fmw42's description.

On an old version, v6.6.2-4, my results are consistent with blxxy's. I suggest you upgrade. For binary upgrades, see http://www.imagemagick.org/script/binary-releases.php . On Windows, this takes me about 5 minutes (most of which is downloading).
snibgo's IM pages: im.snibgo.com
blxxy
Posts: 6
Joined: 2013-10-05T03:48:51-07:00
Authentication code: 6789

Re: Combining multiple images using -distort

Post by blxxy »

Hi again,

I've updated my ImageMagick version to ImageMagick 6.8.7-0 2013-10-05 Q16.

Although I do get two images in the output image now, the distort method isn't properly applied for the second source image.
To make things easier to explain I've changed the coordinates a bit and added some relevant images.

My source images (red and blue) (100x100)
Image
Image

Distortion only using the first input image.

Code: Select all

convert \
-define png:color-type=6 \
-mattecolor transparent \
-background transparent \
-extent 560x316 \
-virtual-pixel transparent \
\( assetA.png -distort BilinearForward 0,0,20,25,0,100,25,150,100,0,550,25,100,100,540,110 \) \
-flatten \
outputA.png
output:
Image

The same goes for the second input image with slightly different coordinates.

Code: Select all

convert \
-define png:color-type=6 \
-mattecolor transparent \
-background transparent \
-extent 560x316 \
-virtual-pixel transparent \
\( assetB.png -distort BilinearForward 0,0,20,200,0,100,25,305,100,0,550,210,100,100,540,300 \) \
-flatten \
outputB.png
output:
Image

Now when I try to combine the two in ways discussed above, something odd happens. The distortion on the second source image isn't applied.

Code: Select all

convert \
-define png:color-type=6 \
-mattecolor transparent \
-background transparent \
-extent 560x316 \
-virtual-pixel transparent \
\( assetA.png -distort BilinearForward 0,0,20,25,0,100,25,150,100,0,550,25,100,100,540,110 \) \
\( assetB.png -distort BilinearForward 0,0,20,200,0,100,25,305,100,0,550,210,100,100,540,300 \) \
-flatten \
outputAB.png
output:
Image

The following is what I'd expect to see:
Image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Combining multiple images using -distort

Post by snibgo »

This is a strange one. I've simplified the command to this Windows script:

Code: Select all

%IM%convert ^
assetA.png ^
-mattecolor blue ^
-background pink ^
-virtual-pixel black ^
-compose Over ^
-gravity NorthWest ^
( assetB.png ^
  -extent 560x316 ^
  -write dbb.png ^
  -distort BilinearForward "0,0,20,200,0,100,25,305,100,0,550,210,100,100,540,300" ^
  -write db.png ^
) ^
-flatten ^
-define png:color-type=6 ^
outputABx.png
Looking at db.png (NOT the final output file), the result is mostly blue, with pink at the bottom, and at half of the right. Thus, the BilinearForward hasn't worked properly.

If we remove assetA.png from the command, db.png is as it was but with a large black area. BilinearForward now works properly.

Weird.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Combining multiple images using -distort

Post by fmw42 »

As snibgo and I suggested before, you should really read the input images before applying any settings. This works fine for me:


convert a.png b.png \
-define png:color-type=6 \
-mattecolor transparent \
-background transparent \
-extent 560x316 \
-virtual-pixel transparent \
\( -clone 0 -distort BilinearForward 0,0,20,25,0,100,25,150,100,0,550,25,100,100,540,110 \) \
\( -clone 1 -distort BilinearForward 0,0,20,200,0,100,25,305,100,0,550,210,100,100,540,300 \) \
-delete 0,1 -flatten \
ab.png
blxxy
Posts: 6
Joined: 2013-10-05T03:48:51-07:00
Authentication code: 6789

Re: Combining multiple images using -distort

Post by blxxy »

8)

it works!

I did come accross the clone function before but couldn't get it to work myself.

Thank you both so much for your kind help. I'm working on a rendering service that stitches images into videoclips based on trackingdata.
When it's done, I'll see if I can post a link for you to (hopefully) enjoy.

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

Re: Combining multiple images using -distort

Post by fmw42 »

Post Reply