Page 1 of 4

Uniting different commands in a single one

Posted: 2010-12-09T23:22:46-07:00
by javismiles
dear friends,
i am trying to unite a few commands in a single one, so far unsuccessfully , maybe you can help me,
the commands work all great, but create some large temporary files in between, i know i could delete them later, but it would be all more efficient if i could accomplish it all with a single command

these are the commands, 2 different scenarios

Scenario 1

1) convert IMG1.jpg -resize 3900x5196! IMG2.jpg
2) convert -size 2040x2856 xc:none -draw "image over -1038,-1050 0,0 'IMG2.jpg'" -quality 95 IMG3.jpg
as you can see 2) uses as input the output of 1) and puts it in a specific position in a new created canvas, is there a way to link both things together?


Scenario 2
the other 2 i wanted to combine are

a) convert -background none -fill #ff33cc -font Arial -pointsize 60 label:"text1" image1.png
b) convert -background none -fill #ff55cc -font Arial -pointsize 40 label:"text2" image2.png
c) convert -background none -fill #ff11cc -font Arial -pointsize 20 label:"text3" image3.png (and a few more in the same way)
2) convert -size 2040x2856 xc:none -draw "image over 438,-114 0,0 'image1.png'" -draw "image over 840,342 0,0 'image2.png'" (same with the rest) -quality 95 finalresult.png
(so here i generate a few png texts, and then mix them all together in different positions in a single final png , any way to do all that in a single step or less steps?)

thank you so very much for any advice or help you can give me
Jav

Re: Uniting different commands in a single one

Posted: 2010-12-10T00:05:03-07:00
by GreenKoopa
Since -draw takes a filename as a parameter, I don't know how to combine these. Maybe someone else knows how to do this. Have you considered using -composite and -geometry instead of -draw? If you chose draw for a reason, hopefully someone can answer that question. If -composite and -geometry haven't been considered, maybe I can help alter the commands so that they can be combined.

Scenario 1
Consider using a lossless image format for your temporaries.
In words, what does step 2 do? (In general, providing a scaled down picture helps others understand more quickly and accurately.) Maybe there is a better way to do this.

Also, providing links to example input images allows me to test an idea before I suggest it.

Re: Uniting different commands in a single one

Posted: 2010-12-10T00:16:21-07:00
by javismiles
dear GreenKoopa
thank you very much for your help and reply,

actually i didnt know what was the most efficient way to do this, i think -draw is probably not the best way, you are right that
-geometry and -composite may be much better, i had no special reason to use -draw, i just didnt know a better way, could you suggest me how
to implement those with geometry and composite? thank you so much

about the temporaries, the thing is that my images are very large, and if i use PNG for example the processing time is huge compared to jpegs, so i do have a problem with that also,
my images can be 5000 or 6000 pixels in width, so using a lossless as temporary makes the processing time get real large, any suggestions?

the summary of the two problems is

1) resizing an image plus positioning it in a specific coordinate within a brand new larger canvas, outputting all as a jpeg

and the other scenario

2) creating multiple texts as png images, and then compositing them all in different positions within a brand new canvas, outputting all as a png

could you suggest how to do both with geometry and composite maybe? i think -draw is not the right way to do them

thank you so very much

Jav
:)

Re: Uniting different commands in a single one

Posted: 2010-12-10T00:20:19-07:00
by javismiles
trying to condense the summary of both scenarios even more

1) resize an image (img1.jpg) + position it at x,y coordinates within a brand new much larger image (background.jpg) - output : jpeg

2) render multiple texts as png images + composite them all at different x,y coordinates within a brand new large background canvas image (background.png) , output: png

i agree that -composite and -geometry or other ways much better than using -draw for those two, would be great any help
as to which commands could make those in a single command line or in as few as possible

thank you so very much
jav
:)

Re: Uniting different commands in a single one

Posted: 2010-12-10T00:35:47-07:00
by GreenKoopa
javismiles wrote: dear GreenKoopa
thank you very much for your help and reply,
A warning: I have little experience so I only have ideas, not solutions, to offer.

There is an entire page of examples on how to use -composite:
http://www.imagemagick.org/Usage/compose/
The only need you appear to have is positioning, so the only relevant section may be:
http://www.imagemagick.org/Usage/compose/#geometry
I can't see that -composite is less efficient than -draw, but I don't know.
javismiles wrote: about the temporaries, the thing is that my images are very large, and if i use PNG for example the processing time is huge compared to jpegs, so i do have a problem with that also,
my images can be 5000 or 6000 pixels in width, so using a lossless as temporary makes the processing time get real large, any suggestions?
The quality loss is probably small and only you can say if it is important for your application. For PNG, try using less compression with -quality to save time. Better yet would be to try ImageMagick's miff format.
javismiles wrote: 1) resizing an image plus positioning it in a specific coordinate within a brand new larger canvas, outputting all as a jpeg
I see a canvas smaller than the image, a negative image location, and a transparent background. Am I confused?

Re: Uniting different commands in a single one

Posted: 2010-12-10T00:44:30-07:00
by javismiles
thank you :)
background is transparent in second example yes, transparent png as output,
and yes the canvas is smaller than the image, so the final result becomes kind of a crop of the larger image, thats why positioning can become negative,
its all good, the process works perfect, all i need is to make it in less commands , the actual thing works all great,

i did read composite and geometry and tried already but no success so far

thank u again :)

Re: Uniting different commands in a single one

Posted: 2010-12-10T00:53:47-07:00
by GreenKoopa
javismiles wrote: yes the canvas is smaller than the image, so the final result becomes kind of a crop of the larger image, thats why positioning can become negative
Why not use -crop?
javismiles wrote: its all good, the process works perfect, all i need is to make it in less commands , the actual thing works all great,
Then I'll hold the help until we see if someone can better answer the original question about -draw. I'm curious too.

Re: Uniting different commands in a single one

Posted: 2010-12-10T06:57:40-07:00
by anthony
first of all even though -draw takes a file name you can still have it 'read' the image from in memory.

the trick is to -write the image into a named registery of images, then delete it.

Code: Select all

   convert ...create_image.... -write mpr:MyImage +delete \
               -draw 'image over .. "mpr:MyImage" ... ' ....
See IM Examples, File Handling, MPR special file format
http://www.imagemagick.org/Usage/files/#mpr

I also use this MPR technique to set a 'tiling' fill image, as -tile also only 'reads' an image.

Do not confuse this with a MPC file format. That is purely a direct memory dump of the image to disk, so that when IM next reads in the image (as part of the same script) it wastes no time in parsing the image format. It is already in the right format, and is 'paged' directly into memory from disk.



However you are only using -draw to resize and compose the image. so why not resize and compose the image using -resize and -composite

Hmm... sorry looks like the second solution has already come up.


There are a number of other ways.. such as pipelining, that using temporayr in memory buffers, instead of disk space

EG: command | command

I use this all the image in many scripts. See "enlarge_image" script in IM examples, script area!

Re: Uniting different commands in a single one

Posted: 2010-12-10T11:31:44-07:00
by javismiles
dear Anthony, thank you so much for taking the time to reply to this,
i actually tried myself already to do these 2 cases with pipelining or with composite, but just couldnt manage
to reach the right syntax to make them work, i tried many times but so far i can only make them work with separate commands

i wonder if you could be so very kind to show me what would be the right command syntax to make those 2
with pipelining and/or composite

1) resize an image (img1.jpg) + position it at x,y coordinates within a brand new much larger image (background.jpg) - output : jpeg

2) render multiple texts as png images + composite them all at different x,y coordinates within a brand new large background canvas image (background.png) , output: png

thank you so very very much
Jav
:)

Re: Uniting different commands in a single one

Posted: 2010-12-10T11:46:58-07:00
by Bonzo
You do not need to pipe the image etc. for your examples

Code: Select all

convert -size 2040x2856 xc:none ( IMG1.jpg -resize 3900x5196! ) -gravity center -geometry -10-10 -composite -quality 95 IMG3.jpg

Code: Select all

convert -size 2040x2856 xc:none -gravity center \
-fill #ff33cc -font Arial -pointsize 60 -annotate +100+100 "Text 1" \
-fill #ff55cc -font Arial -pointsize 40 -annotate -100+100 "Text 2" \
-fill #ff11cc -font Arial -pointsize 20 -annotate -100-100 "Text 3" \
-quality 95 finalresult.png

Re: Uniting different commands in a single one

Posted: 2010-12-10T11:50:46-07:00
by javismiles
thank you so much Bonzo! :)
i am going to try those commands now, thank you thank you :)

by the way i doubt i had, the -geometry syntax -+x-+y
if im reading both coordinates from a form from php and they are both positive, as i add them to the command does it work
if i put them with a space in between , because the + sign wouldnt appear , like: -geometry 35 41
if the values arriving to php are negative then would work well: -geometry -41-31 , but what if they arrive both positive, do i have to detect that and introduce a +sign before them in the command?

thank you so very much again
im going to try the commands now :)

Jav :)

Re: Uniting different commands in a single one

Posted: 2010-12-10T12:01:03-07:00
by Bonzo
Using php you need to make some mods and I would write my code like this to make it easier to read:

Code: Select all

$cmd = " -size 2040x2856 xc:none \( IMG1.jpg -resize 3900x5196! \) ".
" -gravity center -geometry -10-10 -composite -quality 95 ";

exec(" convert $cmd IMG3.jpg ");

Code: Select all

$cmd = " -size 2040x2856 xc:none -gravity center ".
" -fill #ff33cc -font Arial -pointsize 60 -annotate +100+100 \"Text 1\" ".
" -fill #ff55cc -font Arial -pointsize 40 -annotate -100+100 \"Text 2\"  ".
" -fill #ff11cc -font Arial -pointsize 20 -annotate -100-100 \"Text 3\"  ".
" -quality 95 ";

exec(" convert $cmd finalresult.png ");

Note: If you are testing this on a windows localhost use ( IMG1.jpg -resize 3900x5196! ) not \( IMG1.jpg -resize 3900x5196! \)

No spaces and you need the signs so -annotate +100+100 will work but -annotate 100 100 will fail
Negative valuse are OK but you need to decide where you are starting from; I used the center of the canvas.
Positive and negative work differently depending which -gravity setting you use.

Re: Uniting different commands in a single one

Posted: 2010-12-10T12:14:09-07:00
by javismiles
fantastic, thank you very much Bonzo,
i already tried the first one and it works perfect! :)

now im going to try the second one, thank you again :)

Jav :)

Re: Uniting different commands in a single one

Posted: 2010-12-10T12:43:36-07:00
by javismiles
the second one almost almost works but only positioning fails, i think it may have to do with the gravity parameters colliding, i explain
example of my full command (splitted in lines for more clarity)

convert -size 2040x2856 xc:none -gravity NorthWest
-fill #99ff00 -font Arial -pointsize 600 -rotate 0 -gravity West -annotate +444+6 "text1"
-fill #ff0671 -font Arial -pointsize 360 -rotate 0 -gravity West -annotate +78+564 "text2"
-fill #33ffcc -font Arial -pointsize 144 -rotate 0 -gravity West -annotate +1536+2040 "text3"
-fill #ffffcc -font Arial -pointsize 60 -rotate 0 -gravity West -annotate +1350+2172 "text4"
-quality 95 finalresultingimage.png

only thing that fails in this is the positioning, all the texts appear in wrong positions compared to the separated commands versions that works well

could the reason be the gravity parameters, because each rendered text needs a gravity parameter to specify if the text is aligned to the left center or right, but then there is another gravity parameter for the general positioning
within the large canvas

so clearly the problem must come from the reference point of the positioning numbers im sure, mmm,

reference point should be the top left corner of large canvas which is why i tried to use gravity northwest at the very top which is default anyway

the other gravity commands in each of the texts should work if they work in the same way than the :label tag i was using in the version with separate commands

to compare, positioning was working all perfect in this separated commands version:
convert -background none -fill #ff33cc -font Arial -pointsize 60 label:"text1" image1.png
convert -background none -fill #ff55cc -font Arial -pointsize 40 label:"text2" image2.png
convert -background none -fill #ff11cc -font Arial -pointsize 20 label:"text3" image3.png
convert -size 2040x2856 xc:none -draw "image over 438,-114 0,0 'image1.png'" -draw "image over 840,342 0,0 'image2.png'" (same with the rest) -quality 95 finalresult.png


so what could be wrong with the gravity or positioning in the new compact version
convert -size 2040x2856 xc:none -gravity NorthWest
-fill #99ff00 -font Arial -pointsize 600 -rotate 0 -gravity West -annotate +444+6 "text1"
-fill #ff0671 -font Arial -pointsize 360 -rotate 0 -gravity West -annotate +78+564 "text2"
-fill #33ffcc -font Arial -pointsize 144 -rotate 0 -gravity West -annotate +1536+2040 "text3"
-fill #ffffcc -font Arial -pointsize 60 -rotate 0 -gravity West -annotate +1350+2172 "text4"
-quality 95 finalresultingimage.png


thank you so much for your help
jav
:)

Re: Uniting different commands in a single one

Posted: 2010-12-10T12:46:06-07:00
by javismiles
to be more specific sorry


positioning of the individual texts works perfect in this splitted version adding the gravity (which i took out previously to simplify explanation)

convert -background none -fill #ff33cc -font Arial -pointsize 60 -rotate 0 -gravity West label:"text1" image1.png
convert -background none -fill #ff55cc -font Arial -pointsize 40 -rotate 0 -gravity West label:"text2" image2.png
convert -background none -fill #ff11cc -font Arial -pointsize 20 -rotate 0 -gravity West label:"text3" image3.png
convert -size 2040x2856 xc:none -draw "image over 438,-114 0,0 'image1.png'" -draw "image over 840,342 0,0 'image2.png'" (same with the rest) -quality 95 finalresult.png

so in this new version, which is much better cause its a single command, the positioning though fails to appear right

convert -size 2040x2856 xc:none -gravity NorthWest
-fill #99ff00 -font Arial -pointsize 600 -rotate 0 -gravity West -annotate +444+6 "text1"
-fill #ff0671 -font Arial -pointsize 360 -rotate 0 -gravity West -annotate +78+564 "text2"
-fill #33ffcc -font Arial -pointsize 144 -rotate 0 -gravity West -annotate +1536+2040 "text3"
-fill #ffffcc -font Arial -pointsize 60 -rotate 0 -gravity West -annotate +1350+2172 "text4"
-quality 95 finalresultingimage.png

can it be a problem of the gravity commands in the new compact version?