place a picture behind a frame.

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
ladiko
Posts: 9
Joined: 2014-01-09T10:00:21-07:00
Authentication code: 6789

place a picture behind a frame.

Post by ladiko »

I usually place a portrait on top of a background image like this:

convert background.jpg -scale 600x400 \( portrait.jpg -scale 250x200^ -crop 250x200+0+0 \) -geometry +300+150 -composite result.jpg

So the resulting portrait is 250x200 pixel big and placed at X=300 and Y=150. Now i want to place the portrait behind a PNG-background with a transparent part at the portraits posititon. So the background is somehow like a frame but at the same position.

Here is an example:
Image
click on the picture for full size

So i if i change the command like this, it gets all messed up and i have no idea why:

convert background.jpg -scale 600x400 \( portrait.jpg -scale 250x200^ -crop 250x200+0+0 \) -geometry +300+150 +swap -composite result.jpg

the resulting image just shows the portrait with it's final size of 250x200. I also tried

Code: Select all

convert background.png -scale 600x395 \(
    portrait.jpg -scale 250x200^ -crop 250x200+0+0  -geometry +300+150
\) +swap -composite result.bmp
and played around with +page -page -flatten -layers brackets -geometry -extent -scale canvas etc. but i just dont get a suitable result.

edit:
This would work, but it seems to me like a strange workaround:

Code: Select all

convert background.png -scale 600x400 \(
    -clone 0 \(
        /home/cosmocard/cc_temp/portraits/4.jpg -scale 250x200^ -crop 250x200+0+0
    \) -geometry +300+150 -composite
\) +swap -composite result.bmp
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: place a picture behind a frame.

Post by fmw42 »

It might be good to provide your input images by posting to dropbox.com and put the URLs here. It is not clear which image is your background and which the foreground and which has transparency and what you are trying to do with the swap. In general, the first image will be the background and the second the foreground. But if you add +swap the order is then reversed and depending upon your transparency, may not show as you want. Also it is a good idea to add +repage after a crop to remove the virtual canvas so that the composite can properly use -geometry or -page etc and put the images in the right location.

Also always a good idea to post your version of IM and platform.

See
http://www.imagemagick.org/Usage/crop/#crop_repage
http://www.imagemagick.org/Usage/layers/#convert
http://www.imagemagick.org/Usage/compose/
ladiko
Posts: 9
Joined: 2014-01-09T10:00:21-07:00
Authentication code: 6789

Re: place a picture behind a frame.

Post by ladiko »

Code: Select all

user@host:~/test$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 14.04.1 LTS
Release:	14.04
Codename:	trusty
user@host:~/test$ uname -a
Linux host 3.13.0-44-generic #73-Ubuntu SMP Tue Dec 16 00:23:46 UTC 2014 i686 i686 i686 GNU/Linux
user@host:~/test$ convert --version
Version: ImageMagick 6.7.7-10 2014-03-06 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP    

user@host:~/test$ convert frame.png -scale 600^x400 \( portrait.jpg -scale 400x280^ -crop 400x280+0+0 \) -geometry +42+41 -composite test.jpg
user@host:~/test$ convert frame.png -scale 600^x400 \( portrait.jpg -scale 400x280^ -crop 400x280+0+0 \) -geometry +42+41 +swap -composite test_swap.jpg
user@host:~/test$ convert frame.png -scale 600^x400 \( -clone 0 \( portrait.jpg -scale 400x280^ -crop 400x280+0+0 \) -geometry +42+41 -composite \) +swap -composite test_swap_clone.jpg
https://www.dropbox.com/sh/r8pjpv8yt0r1 ... oVzJha?lst
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: place a picture behind a frame.

Post by fmw42 »

Since the portrait image must appear behind the frame, it must be the first image in the sequence and the transparent frame must be the second image in the sequence. IM composes the second image over the first image. So you need to list the modified frame after the modified image or use +swap. The geometry must be associated with the second image. But if the frame is larger than the portrait, that causes problems such that you need to use -mosaic, which is canvas expanding and -page which comes before the appropriate image image (for the cleanest method). Also you must put +repage after a crop.

This is correct.

Code: Select all

convert  \( portrait.jpg -scale 400x280^ -crop 400x280+0+0 +repage \) -page -42-41 \( frame.png -scale 600x400^ \) -mosaic  test_swap1.jpg
or using +swap

Code: Select all

convert -page -42-41  \( frame.png -scale 600x400^ \) -page +0+0 \( portrait.jpg -scale 400x280^ -crop 400x280+0+0 +repage \)  +swap -mosaic  test_swap2.jpg
The -page +0+0 is needed in the last case so that the second image does not get the same page. It is optional in the first example since the default is +0+0 for the first image.

The ^ symbol can be put anywhere in the -scale arguments, but I like to put it last. It is not relevant to one or the other argument, but to both.

see
http://www.imagemagick.org/Usage/layers/
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: place a picture behind a frame.

Post by snibgo »

"-compose DstOver" is useful here. The output size is that of the frame, but the portrait goes beneath the frame.

Windows syntax:

Code: Select all

convert frame3.png -resize 600x400^^ ( portrait2.jpg -scale 400x200^^ -crop 400x280+0+0 +repage ) -compose DstOver -composite out.jpg
snibgo's IM pages: im.snibgo.com
ladiko
Posts: 9
Joined: 2014-01-09T10:00:21-07:00
Authentication code: 6789

Re: place a picture behind a frame.

Post by ladiko »

Thank you snibgo, that solved my issue. @Fred: Yorr solution also works, but the second solution is shorter, so i go for that one. Thank you for helping me, too. :-)
Post Reply