The morass of compose and alpha

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
bugbear
Posts: 33
Joined: 2010-10-21T03:06:35-07:00
Authentication code: 8675308

The morass of compose and alpha

Post by bugbear »

I wish to do something pretty simple;

I have an RGB image (let's call it a character, say "snoopy");

I wish to make an animation of small snoopy moving (sliding, really) across a large RGB background image, so a programmed sequence
of blits to successive (x,y) coordinates on the background will suit. ffmpeg can then make my frames into a video.

I would like snoopy not to be rectangular, so I have a gray scale mask (snoopy_shape).

I think all I need to do is first add the "snoopy_shape" to "snoopy" to make
an alpha'd snoopy ("snoopy_with_a_shape"), and then composite
snoopy_with_a_shape onto background at an x,y offset.

But I have just spent an hour reading manuals and (principally)
Anthony Thyssen's "Examples of ImageMagick Usage", and while
I now know quite a lot about the history of ImageMagick's handling
of transparency, various workrounds, old command line options, new
command line options, I'm no nearer knowing
the 2 commands I indeed to perform a masked blit operation.

Can anyone help?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: The morass of compose and alpha

Post by snibgo »

It could be something like this.

Suppose snoopy.png is an image with the character and snoopy_mask.png is the same size as snoopy, white where the character is and black elsewhere. It is probably anti-aliased (ie a thin graduation between black and white. background.png is a larger image of the required background.

Code: Select all

convert ^
  background.png ^
  ( snoopy.png snoopy_mask.png -compose CopyOpacity -composite ) ^
  -gravity South ^
  -geometry +0+100 ^
  -compose Over -composite
  out.png
Change "100" to "101" to move snoopy right, etc.
snibgo's IM pages: im.snibgo.com
bugbear
Posts: 33
Joined: 2010-10-21T03:06:35-07:00
Authentication code: 8675308

Re: The morass of compose and alpha

Post by bugbear »

That's a very good basis for a solution to my problem.

Thank you.

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

Re: The morass of compose and alpha

Post by snibgo »

If snoopy.png and snoopy_mask.png are constant then so is everything within the brackets, so you could do this once:

Code: Select all

convert ^
  snoopy.png snoopy_mask.png -compose CopyOpacity -composite ^
  snoopy_shaped.png
Then each frame is:

Code: Select all

convert ^
  background.png ^
  snoopy_shaped.png ^
  -gravity South ^
  -geometry +0+100 ^
  -compose Over -composite
  out.png
If you have a fairly small number of frames, an efficient command could be:

Code: Select all

convert ^
  background.png ^
  snoopy_shaped.png ^
  -gravity South ^
  -compose Over ^
  ( -clone 0-1 -geometry +0+100 -composite -write f100.png +delete ) ^
  ( -clone 0-1 -geometry +0+101 -composite -write f101.png +delete ) ^
  ( -clone 0-1 -geometry +0+102 -composite -write f102.png +delete ) ^
  NULL:
snibgo's IM pages: im.snibgo.com
Post Reply