-convert composite .. having some trouble.

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
vonshavingcream
Posts: 3
Joined: 2011-09-07T20:44:31-07:00
Authentication code: 8675308

-convert composite .. having some trouble.

Post by vonshavingcream »

Hi all .. I am having some issues so I decided to hit the forums. It's a pretty straight forward issue, I just can't seem to get it working.
here's what I am trying to do.

Basically Composite Two Images together, However The twist it, I want the final image to be the size of the image on top. I am trying to tile a background behind another image. any help would be fantastic. I have spend 2 days off and on with this and it doesn't feel like it should be this difficult. Here is what I think I need to do.

1. create image using the "main image"
2. get geometry of that image
3. make another image (we can call this final_image) setting the geometry to that of the first image.
4. tile a "background image" onto final_image
5. composite the main_image onto the final_image
6. write final_image to a file.

I just can't seem to make this work in Imagemagick, I've been used to using the Imagick PHP class, but this project requires command line to be used.

Any help or a point in the right direction would be great. I've had a look at the documentation and I can get most of this to work on it's own, but I can seem to put it all together in one single command.

thanks again for any help.

C
Version: ImageMagick 6.6.2-6 2011-03-16 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2010 ImageMagick Studio LLC
Features: OpenMP
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -convert composite .. having some trouble.

Post by fmw42 »

I am not sure I understand your goal. How can you put an image over a tiled image and have it be the size of the overlaid image without covering the whole tiled image? Does your top image have transparency? Why do you need to make the bottom tiled image bigger than the top image?

This will do what I think you want, assuming you want the tiled image below the top image to have the same size as the top image.

# create a top image that has transparency
convert logo: -resize 50% -transparent white logo2t.png

# get top image size
dim=`convert logo2t.png -format "%wx%h" info:`

# create tile image
convert -size 30x30 pattern:checkerboard checkerboard.png

# fill out the tiled image to the size of the top image and composite
convert \( -size $dim tile:checkerboard.png \) logo2t.png -compose over -composite logo_checks.jpg
or
convert \( -size $dim tile:checkerboard.png \) logo2t.png -flatten logo_checks.jpg


Image

You can try all these commands as all the images are built-in IM images or patterns. If this is what you want, then you can substitute your images. Just get the dimension of the top image and then substitute your top and tile images into the last command line.

If this is not what you want, then please clarify further.

If on windows, then see http://www.imagemagick.org/Usage/windows/ as the above (esp the dim variable) is computed differently in windows and parenthesis are not escaped (with the \)

see
http://www.imagemagick.org/Usage/canvas/#tile
http://www.imagemagick.org/Usage/layers/#convert
http://www.imagemagick.org/Usage/layers/#flatten
vonshavingcream
Posts: 3
Joined: 2011-09-07T20:44:31-07:00
Authentication code: 8675308

Re: -convert composite .. having some trouble.

Post by vonshavingcream »

Your example is exactly what I am trying to do. All of your code makes sense to me, I just don't understand how to run it as a single command line. This is where I am getting stuck

i tried this

Code: Select all

convert logo: -resize 50% -transparent white logo2t.png dim=`convert logo2t.png -format "%wx%h" info:` convert -size 30x30 pattern:checkerboard checkerboard.png convert \( -size $dim tile:checkerboard.png \) logo2t.png -compose over -composite logo_checks.jpg
and get the following error:

Code: Select all

convert: unable to open image `logo2t.png':  @ error/blob.c/OpenBlob/2498.
convert: unable to open file `logo2t.png' @ error/png.c/ReadPNGImage/3072.
which in turn spawns all kinds of other errors since the command is all based on logo2t.png

I also tried it like this

Code: Select all

convert logo: -resize 50% -transparent white logo2t.png \
dim=`convert logo2t.png -format "%wx%h" info:` \ 
convert -size 30x30 pattern:checkerboard checkerboard.png \
convert \( -size $dim tile:checkerboard.png \) logo2t.png -compose over -composite logo_checks.jpg
I get the same errors. The directory I am working in has proper read/write permissions and I also tried both commands as root. Still the same errors.
if I do something simple like

Code: Select all

convert logo: -resize 50% logo2.png
that works just fine.
Version: ImageMagick 6.6.2-6 2011-03-16 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2010 ImageMagick Studio LLC
Features: OpenMP
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: -convert composite .. having some trouble.

Post by Bonzo »

You can not do it all in one line as the dimensions need saving into a variable.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -convert composite .. having some trouble.

Post by fmw42 »

You actually can do it, but it really does not make much sense to do it this way if you are scripting. Furthermore, presumably you already have your tile pattern as an image and your top image with transparency. So all you need to do is substitute into the composite command. However, if you really want one command line, you can do it as follows, though it is a trick to get the dim variable by inserting another convert command. This does not really save you much. Hopefully in IM 7 there will be a way to get the dimension of one image and use it to create another.



convert \( logo: -resize 50% -transparent white -write logo2t.png \) \
\( -size 30x30 pattern:checkerboard \) \
\( -size `convert logo2t.png -format "%wx%h" info:` tile:checkerboard.png \) \
-delete 1 +swap -compose over -composite logo_checks.jpg

but then you have to delete logo2t.png using rm logo2t.png, if you don't want it any longer.


The other way to do it, is to use -fx, but that may be slow for big images. However, it does the job without having to resort to the second convert to get the dimensions.

convert \( logo: -resize 50% -transparent white \) \
\( -clone 0 \( -size 30x30 pattern:checkerboard \) -alpha off -virtual-pixel tile -fx "v.p{i,j}" \) \
+swap -compose over -composite logo_checks.jpg


see
http://www.imagemagick.org/Usage/transform/#fx
http://www.imagemagick.org/Usage/transform/#fx_escapes
http://www.imagemagick.org/script/fx.php
vonshavingcream
Posts: 3
Joined: 2011-09-07T20:44:31-07:00
Authentication code: 8675308

Re: -convert composite .. having some trouble.

Post by vonshavingcream »

I'm still having problems with this here is what I am doing.

Code: Select all

dim=`convert "/home/filecabinet/complete/2384017213479/renders/custom-render.png" -format "%wx%h" info:`;
convert "/home/filecabinet/stock/backgrounds/low-res/space-lr.png" -resize 256x256 "/home/filecabinet/complete/2384017213479/renders/tempbg.png";
convert \( -size $dim tile: "/home/filecabinet/complete/2384017213479/renders/tempbg.png" \) "/home/filecabinet/complete/2384017213479/renders/custom-render.png" -flatten "/home/filecabinet/complete/2384017213479/renders/custom-render2.png";
when I execute the third line my shell screen just sits there and doesn't do anything.
Version: ImageMagick 6.6.2-6 2011-03-16 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2010 ImageMagick Studio LLC
Features: OpenMP
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -convert composite .. having some trouble.

Post by fmw42 »

Close up the space between tile: and your image

convert \( -size $dim tile: "/home/filecabinet/complete/2384017213479/renders/tempbg.png" \)

use

convert \( -size $dim tile:"/home/filecabinet/complete/2384017213479/renders/tempbg.png" \)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: -convert composite .. having some trouble.

Post by anthony »

Bonzo wrote:You can not do it all in one line as the dimensions need saving into a variable.
And yes while it can be done using various 'tricks', it is not straight forward.

This is one of the features that IM v7 will be addressing. Specifically accessing and making use of existing image attributes to define the various settings for future operations. Until then two commands will be needed :-(
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply