Compose problem: JPG + PNG

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
Weirdo

Compose problem: JPG + PNG

Post by Weirdo »

Hi,

I'm trying to create a composition from two images - one is a *.jpg file (as a background) and second one is a *.png with transparency (as a frame over the background).
What i want to get is a .jpg file with all from frame.png but transparency - where frame.png is transparent I would like to get part from background.jpg.
I've tried a lot of code examples but it hasn't worked for me (neither convert nor composite). Read lot about image layering but it hasn't helped.
I ask you to write me sample code which will work my way.
Thanks in advance!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Compose problem: JPG + PNG

Post by Bonzo »

You do not say what method you are using or the IM version. But try:

Code: Select all

convert background.jpg frame.png -composite output.jpg
OR

Code: Select all

convert frame.png background.jpg -compose SrcIn -composite output.jpg
 
Weirdo

Re: Compose problem: JPG + PNG

Post by Weirdo »

@Bonzo: thank you for your reply. Unfortunatelty it doesn't seem to help in my case.

Please take a look at my code (PHP):

Code: Select all

// (...)

$layer22 = $storagePath . '/t/frame.jpg';
$layer222 = $storagePath . '/frame.png';

// paths are valid - checked also with file_exists()
// rights: dirs have 755, files 644

shell_exec('"/usr/bin/composite" -compose "atop" -geometry "1393x1081+162+105" "'.$layer.'" "'.$backgroundFile.'" "'.$storagePath.'/first.jpg" ');
shell_exec('"/usr/bin/composite" -compose "atop" -geometry "1598x2362+0+0" "'.$layer22.'" "'.$storagePath.'/first.jpg" "'.$storagePath.'/second.jpg" ');

// Both work nice.
// If in second one I will replace $layer22 with $layer222 - it will fail. Only first one will work.
Wish it wil help to find solution.

And IM version info:
Version: ImageMagick 6.4.4 2009-04-15 Q16 http://www.imagemagick.org Copyright: Copyright (C) 1999-2008 ImageMagick Studio LLC
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Compose problem: JPG + PNG

Post by Bonzo »

I suggest you start from basics; forget all the variables etc. and put everything in one directory and get it to work. When its working OK you can then modify the code as you know it is working OK.

There may be a problem with your png image; if you post a link to it here we can try with your actual images.
Have you tried a different png image ?

If you check out my site there are other examples of php IM use and there may be something that works for you there.
Weirdo

Re: Compose problem: JPG + PNG

Post by Weirdo »

Well I've started from basics - and I get correct code (it works, but with JPG file) - in variables there are only full pathnames (with filename and extension).

I've put both files to the same directory.
Then was doing some other research - first with "-debug exception" -> which hadn't showed any error.
After that I've tried with identify - it succeded with JPG, but failed with PNG.

You wrote: "There may be a problem with your png image" - and it seems like you are right! I've not expected that it's possible... but if IM can not obtain image file info how could he use it?

Now my question is what should I do to make this file correct?
How to edit and save it with Photoshop / Gimp ?

Here is my PNG:
http://www1.crazyfoto.pl/gen/userPhotos ... 1_2009.png

I appreciate your help, big thanks!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Compose problem: JPG + PNG

Post by Bonzo »

After some messing about the code below worked for me.

Code: Select all

exec("convert ( -size 1598x2362 xc:white background.jpg -geometry +110+130 -composite ) 11_2009.png  -composite output.png");
1/ Create a canvas the same size as the calander with a white background - can be any colour or none.
2/ Place your image onto the canvas where it will show through the transparent section on your png image.
3/ Place the png image over the first image.

It may not be the correct way to do it but it worked !

You can save as a jpg or png
Weirdo

Re: Compose problem: JPG + PNG

Post by Weirdo »

Hi,

first of all I would like to say sorry for such a long time without responding.

Thank you very much for your help. After long time of unsuccessful trials it really gave me hope that my problem will be solved.

Today I checked your solution... and it still hadn't worked.
But after some time I have already found where the problem laid.
It's a shame that I haven't checked it before...

ImageMagick was installed, but without support of PNG. Used convert logo: logo.png to check it, also checked /usr/include/png.h (filesize = 0).
Now it works in the simpliest way.

Sorry for that I haven't checked in on the begining - I just hadn't know about it.

Thanks one more time!
Post Reply