Reduce the number of commands

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?".
Albireo
Posts: 68
Joined: 2010-01-12T03:32:23-07:00
Authentication code: 8675309

Reduce the number of commands

Post by Albireo »

Hello!

I want to:
1. Resize the images (as an example max 300x300)
2. Remove imageinformation
3. Give the images rounded corners
4. Add the image with rounded corners on a transparent square image (300x300)

Programming sequence below performs this but with 3 pcs of commands
Is it possible to simplify in some way?

Code for Windows / DOS

Code: Select all

convert -define jpeg:size=300x300 c:\temp\pic\hatching_orig.jpg -thumbnail "300x300>" c:\temp\pic\pad_extent.png
convert c:\temp\pic\pad_extent.png ( +clone  -threshold -1 -draw "fill black polygon 0,0 0,25 25,0 fill white circle 25,25 25,0" ( +clone -flip ) -compose Multiply -composite ( +clone -flop ) -compose Multiply -composite ) +matte -compose CopyOpacity -composite  c:\temp\pic\rounded_corners.png
convert c:\temp\pic\rounded_corners.png  -background transparent -gravity center -extent 300x300 c:\temp\pic\Image1.gif
//Jan
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reduce the number of commands

Post by fmw42 »

unix commands with black border added

infile="hatching_orig.jpg"
drawcmd=`convert $infile -define jpeg:size=300x300 -thumbnail "300x300>" \
-format "roundrectangle 1,1 %[fx:w+4],%[fx:h+4] 25,25" info:`
convert \( $infile -define jpeg:size=300x300 -thumbnail "300x300>" \
-matte -bordercolor none -border 3 \) \
\( +clone -alpha transparent -background none \
-fill white -stroke none -strokewidth 0 -draw "$drawcmd" \) \
-compose DstIn -composite \
\( +clone -alpha transparent -background none \
-fill none -stroke black -strokewidth 3 -draw "$drawcmd" \) \
-compose Over -composite thumbnail_rounded_border_in_one.png
Albireo
Posts: 68
Joined: 2010-01-12T03:32:23-07:00
Authentication code: 8675309

Re: Reduce the number of commands

Post by Albireo »

Thank you!

But when I converted the command to DOS / Windows is something wrong

So now I try this:

Code: Select all

convert -define jpeg:size=300x300 c:\temp\pic\hatching_orig.jpg -thumbnail "300x300>" -write c:\temp\pic\pad_extent2.png ^
 ( +clone  -threshold -1 -draw "fill black polygon 0,0 0,25 25,0 fill white circle 25,25 25,0" ^
 ( +clone -flip ) -compose Multiply -composite ( +clone -flop ) ^
 -compose Multiply -composite ) +matte -compose CopyOpacity -composite -write c:\temp\pic\rounded_corners2.png ^
 -background transparent -gravity center -extent 300x300 c:\temp\pic\Image2.gif
I can remove both -write commans in the command.

The images from the "intermediate stages" seems be OK (pad_extent2.png & rounded_corners2.png)

With the last picture is something wrong
The Image with round corners is completely black
and the surface to be transparent is completely white

I can't see what is differ or what' s wrong. :?

//Jan
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reduce the number of commands

Post by fmw42 »

You did not use my commands correctly.

Note you must convert this to Windows correctly and use it as I have.

drawcmd=`convert $infile -define jpeg:size=300x300 -thumbnail "300x300>" \
-format "roundrectangle 1,1 %[fx:w+4],%[fx:h+4] 25,25" info:`


Note 1,1 %[fx:w+4],%[fx:h+4] 25,25 is not the same as your code.

I am computing the actual image size after reducing it and adding 4. This must be done separately as you don't know the exact size of the resulting thumbnail as it won't necessarily be 300x300.

Then you are using circles and polygons and I was using roundrectangle to cut the corners.

My code was a variant of one Anthony made at http://www.imagemagick.org/Usage/thumbn ... ded_border

I just converted his MVG file to a string, drawcmd.

Windows does not recognize the backquotes type computations. So you will need to do that differently, but easy enough to get the image size after reducing it and then set up a string as I did. Or see http://www.imagemagick.org/Usage/windows/ for more information about Windows procssing on IM


Hope that helps

Fred
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reduce the number of commands

Post by fmw42 »

see el_supremo's comments at viewtopic.php?f=3&t=15419

add -compose over before -extent
Albireo
Posts: 68
Joined: 2010-01-12T03:32:23-07:00
Authentication code: 8675309

Re: Reduce the number of commands

Post by Albireo »

fmw42 wrote:unix commands with black border added

infile="hatching_orig.jpg"
drawcmd=`convert $infile -define jpeg:size=300x300 -thumbnail "300x300>" \
-format "roundrectangle 1,1 %[fx:w+4],%[fx:h+4] 25,25" info:`
convert \( $infile -define jpeg:size=300x300 -thumbnail "300x300>" \
-matte -bordercolor none -border 3 \) \
\( +clone -alpha transparent -background none \
-fill white -stroke none -strokewidth 0 -draw "$drawcmd" \) \
-compose DstIn -composite \
\( +clone -alpha transparent -background none \
-fill none -stroke black -strokewidth 3 -draw "$drawcmd" \) \
-compose Over -composite thumbnail_rounded_border_in_one.png
Tried to convert this to Windows but something was going wrong.
I'm not sure where the right place for single and the double quotes is.

Code: Select all

@set infile=c:\temp\pic\hatching_orig.jpg
@set drawcmd=convert %infile% -define jpeg:size="300x300" -thumbnail "300x300^>" -format "roundrectangle 1,1 %%[fx:w+4],%%[fx:h+4] 25,25" info:

convert ( %infile% -define jpeg:size="300x300" -thumbnail "300x300^>" -matte -bordercolor none -border 3 ) ^
( +clone -alpha transparent -background none ^
-fill white -stroke none -strokewidth 0 -draw %drawcmd% ) ^
-compose DstIn -composite ^
( +clone -alpha transparent -background none ^
-fill none -stroke black -strokewidth 3 -draw %drawcmd% ) ^
-compose Over -composite c:\temp\pic\thumbnail_rounded_border_in_one.png
The error message is:
convert: Non-confirming drawing primitive definition 'convert' @ draw.c/DrawImage/3141.

I get a picture - (a narrow strip below and to the right).
The file size is 7kB (expecting around 100kB).

______________________________________________________________

The command below works for me. (DOS / Windows)
(I don't know if it is possible to see any difference
between the results from the various commands)

Code: Select all

@Set PicSize=400x400
@Set FileIn=c:\temp\pic\hatching_orig.jpg
@Set FileOut=c:\temp\pic\Image2.png
@Set RC=20

convert -define jpeg:size=%PicSize% %FileIn% -thumbnail "%PicSize%>" ^
 ( +clone  -threshold -1 -draw "fill black polygon 0,0 0,%RC% %RC%,0 ^
  fill white circle %RC%,%RC% %RC%,0" ^
 ( +clone -flip ) -compose Multiply -composite ( +clone -flop ) ^
 -compose Multiply -composite ) +matte -compose CopyOpacity -composite ^
 -background transparent -gravity center -compose Over -extent %PicSize% %FileOut%
//Jan
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Reduce the number of commands

Post by el_supremo »

I'm not sure where the right place for single and the double quotes is.
There are a couple of problems when trying to convert a UNIX shell script which contains backquoted commands. DOS doesn't allow that kind of syntax directly in any DOS batch command but there is a way to do it with the FOR command. But, from what I've been able to figure out while playing with FOR and backquotes, DOS does not allow a normal string (within double-quotes) in the FOR statement if it contains spaces. So, translating Fred's original suggestion to use the DOS FOR command, you would expect (hope?) that this would work:

Code: Select all

for /f "usebackq" %i IN (`convert %infile% -define jpeg:size="300x300" -thumbnail "300x300^>" -format "roundrectangle 1,1 %[fx:w+4],%[fx:h+4] 25,25" info:`) DO set drawcmd=%i
But all this does is set drawcmd to "roundrectangle". The spaces within the quoted string blow the statement up. To get around it, you can move the fixed parts of the string into the set statement like this:

Code: Select all

for /f "usebackq" %i IN (`convert %infile% -define jpeg:size="300x300" -thumbnail "300x300^>" -format "%[fx:w+4],%[fx:h+4]" info:`) DO set drawcmd=roundrectangle 1,1 %i 25,25
Note that the string in the set statement does not have quotes around it. Also note that this command was pasted directly into a DOS window and is one long command line. If you want to put it in a batch file you have to double the percent signs in "%%[fx:w+4],%%[fx:h+4]" and you might have to double the %%i too.
So, in theory you should be able to replace your "@set drawcmd=...." statement with this command and it should work.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
Albireo
Posts: 68
Joined: 2010-01-12T03:32:23-07:00
Authentication code: 8675309

Re: Reduce the number of commands

Post by Albireo »

Thank you Pete for your time!

I understand that it's not easy :)
and I must say that I don't really can follow all lines of thought.
I can't add as much :(

I always write an ".BAT" file, because the command is long
(and can't paste the command directly to a "DOS" window)

I tried to make an "image convert" with this command:

Code: Select all

@set infile=c:\temp\pic\hatching_orig.jpg
@set drawcmd=for /f "usebackq" %%i IN (`convert %infile% -define jpeg:size="300x300" -thumbnail "300x300^>" -format "roundrectangle 1,1 %%[fx:w+4],%%[fx:h+4] 25,25" info:`) DO set drawcmd=%%i

convert ( %infile% -define jpeg:size="300x300" -thumbnail "300x300^>" -matte -bordercolor none -border 3 ) ^
( +clone -alpha transparent -background none ^
-fill white -stroke none -strokewidth 0 -draw %drawcmd% ) ^
-compose DstIn -composite ^
( +clone -alpha transparent -background none ^
-fill none -stroke black -strokewidth 3 -draw %drawcmd% ) ^
-compose Over -composite c:\temp\pic\thumbnail_rounded_border_in_one.png
This command generated a lot of errors (18 pcs) :?
No1: convert: Non-conforming drawing primitive definition 'for' @ draw.c/DrawImage/3141.
No2: convert: unable to open image ''/f': No such file or directory @ blob.c/OpenBlob/2481.
No3: ..........

//Jan
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Reduce the number of commands

Post by el_supremo »

The first two lines of your batch script should be:

Code: Select all

@set infile=c:\temp\pic\hatching_orig.jpg
@for /f "usebackq" %%i IN (`imconvert %%infile%% -define "jpeg:size=300x300" -thumbnail "300x300^>" -format "%%[fx:w+4],%%[fx:h+4]" info:`) DO set drawcmd=roundrectangle 1,1 %%i 25,25
Note that I put double quotes around "jpeg:size=300x300" instead of using jpeg:size="300x300". The colon means something special to DOS and the only way I could get this to work was to hide the colon within the double-quoted string.
I checked this in a batch file and you do have to double all the percent signed including the one on %%i.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
Albireo
Posts: 68
Joined: 2010-01-12T03:32:23-07:00
Authentication code: 8675309

Re: Reduce the number of commands

Post by Albireo »

Thank's Now I think the command works, but....

solution first:
I did a few small things.
convert ( %%infile%% doesn't work but convert ( "%infile%" did
the same problem at "%drawcmd%"

The "ultimate" result :D :

Code: Select all

set infile=c:\temp\pic\hatching_orig.jpg
for /f "usebackq" %%i IN (`convert %%infile%% -define "jpeg:size=300x300" -thumbnail "300x300^>" -format "%%[fx:w+4],%%[fx:h+4]" info:`) DO set drawcmd=roundrectangle 1,1 %%i 25,25

convert ( "%infile%" -define "jpeg:size=300x300" -thumbnail "300x300^>" -matte -bordercolor none -border 3 ) ^
( +clone -alpha transparent -background none ^
-fill white -stroke none -strokewidth 0 -draw "%drawcmd%" ) ^
-compose DstIn -composite ^
( +clone -alpha transparent -background none ^
-fill none -stroke black -strokewidth 3 -draw "%drawcmd%" ) ^
-compose Over -composite c:\temp\pic\thumbnail_rounded_border_in_one.png
But the result isn't the same as I desire...... :?

- the final image size is not 300x300 as I desire (it is 406x306)
(I have not checked, but if the image has been rotated 90degrees
would the image probably received the dimensions 306x402)

- I get a black border around the image with the rounded corner.


Back to my wishes.
1. Resize the images (as an example max 300x300)
I want to get all the images in size 300x300 (for example) but
the picture may have a size of 300x244 or 244x300 - the rest is filled up by a transparent surface)
My idea was to make a transparent surface with the size 300x300.
In the middle of this space, add an image with rounded corners

2. Remove image information
I don't want some image information - (if I can't save my own information)

//Jan
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reduce the number of commands

Post by fmw42 »

1. Resize the images (as an example max 300x300)
I want to get all the images in size 300x300 (for example) but
the picture may have a size of 300x244 or 244x300 - the rest is filled up by a transparent surface)
My idea was to make a transparent surface with the size 300x300.
In the middle of this space, add an image with rounded corners
You cannot resize to 300x300 and get that exactly in most cases. To get 300x244 or the like from the resize you need to do something like

convert image -resize 300x300 -background none -gravity center -extent 300x300 result

That will resize to 300x244 or the like then pad with transparency to fill back to 300x300. However, your round corners needs to be based upon 300x244. That was why I needed to get the image dimensions after the resize to know how big the image was (300x244 or the like) rather than 300x300.

If you can show us a picture of what you want with round corners then we can probably help. I am not sure of what exact shape you wanted. My earlier example did the round corners and then added a black border around it.

Perhaps this is what you want (unix code)

infile="hatching_orig.jpg"
drawcmd=`convert $infile -define jpeg:size=300x300 -thumbnail "300x300>" \
-format "roundrectangle 1,1 %[fx:w],%[fx:h] 25,25" info:`
convert \( $infile -define jpeg:size=300x300 -thumbnail "300x300>" \) \
\( +clone -alpha transparent -background none \
-fill white -stroke none -strokewidth 0 -draw "$drawcmd" \) \
-compose DstIn -composite -compose over -background none -gravity center -extent 300x300 \
thumbnail_rounded_in_one.png

This is padded to 300x300 with transparency
Image
Albireo
Posts: 68
Joined: 2010-01-12T03:32:23-07:00
Authentication code: 8675309

Re: Reduce the number of commands

Post by Albireo »

fmw42 wrote:
........Perhaps this is what you want (unix code)

Code: Select all

infile="hatching_orig.jpg"
drawcmd=`convert $infile -define jpeg:size=300x300 -thumbnail "300x300>" \
-format "roundrectangle 1,1 %[fx:w],%[fx:h] 25,25" info:`
convert \( $infile -define jpeg:size=300x300 -thumbnail "300x300>" \) \
\( +clone -alpha transparent -background none \
-fill white -stroke none -strokewidth 0 -draw "$drawcmd" \) \
-compose DstIn -composite -compose over -background none -gravity center -extent 300x300 \
thumbnail_rounded_in_one.png
This is padded to 300x300 with transparency
Image
Jepp! the result was what I wanted
(But I'm still not sure where the right place for single and the double quotes shall be.)
I have tried this code,

Code: Select all

Set infile=c:\temp\pic\hatching_orig.jpg
Set Size=300x300
Set drawcmd=convert "%infile%" -define "jpeg:size=300x300" -thumbnail "300x300^>" -format "roundrectangle 1,1 %%[fx:w],%%[fx:h] 25,25" info:

convert ( "%infile%" -define "jpeg:size=300x300" -thumbnail "300x300^>" ) ^
( +clone -alpha transparent -background none -fill white -stroke none -strokewidth 0 -draw "%drawcmd%" ) ^
-compose DstIn -composite -compose over -background none -gravity center -extent 300x300 thumbnail_rounded_in_one.png
but it get one error.
convert: Non-conforming drawing primitive definition 'convert' @ draw.c/DrawImage/3141.
When is the [fx:w] & [fx:h] get the value?

//Jan
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reduce the number of commands

Post by fmw42 »

I am not a windows user so see the comments from el_supremo. But I believe that your definition of drawcmd is still not proper DOS syntax. You also define size=300x300, but never use it? Hopefully el_supremo or another Windows expert can help you further. or study http://www.imagemagick.org/Usage/windows/
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Reduce the number of commands

Post by Bonzo »

Looks to me like you have a problem here: -draw "%drawcmd% as this expands out to;

Code: Select all

-draw "convert "%infile%" -define "jpeg:size=300x300" -thumbnail "300x300^>" -format "roundrectangle 1,1 %%[fx:w],%%[fx:h] 25,25" info:"
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Reduce the number of commands

Post by el_supremo »

I've done this a different way, avoiding the FOR statement, because the syntax is a bit easier which makes it easier to convert from *nix shell.

The first convert writes the info required for the rectangle into a text file which is then referenced by the -draw in the next convert command.
I changed the coordinates a bit and I also changed the way it creates the final image. This clones the thumbnail and turns it completely black, then draws a white roundrectangle on that and the result is composited as the alpha channel of the original thumbnail.
This is for use in a batch file.

Code: Select all

set infile=c:\temp\pic\hatching_orig.jpg
convert %infile% -define "jpeg:size=300x300" -thumbnail "300x300>" ^
   -format "roundrectangle 0,0 %%[fx:w-1],%%[fx:h-1] 25,25" info: >draw_cmd.txt

convert "%infile%" -thumbnail "300x300>"  ^
   ( +clone -threshold "100%%" -fill white -draw "@draw_cmd.txt" ) ^
   -alpha off -compose CopyOpacity -composite ^
   -background none -compose Over -gravity center -extent 300x300 ^
   thumbnail_rounded_in_one.png
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
Post Reply