is there a way to draw circles with command?

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?".
manit
Posts: 123
Joined: 2009-01-30T22:31:26-07:00

is there a way to draw circles with command?

Post by manit »

I want to know how can I write command to draw circle with given center,radius & colour in a particular image.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: is there a way to draw circles with command?

Post by el_supremo »

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.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: is there a way to draw circles with command?

Post by Bonzo »

Try running this ( its using php but you do not say how you want to run the code and you can modify it for your usage ):

Code: Select all

<?php
exec("convert -size 200x200 xc:black -stroke black -strokewidth 1 \\
-fill white -draw \"circle 100,100 100,10 \" \\
-fill black -draw \"circle 100,100 100,30 \" \\
-fill blue -draw \"circle 100,100 100,50 \" \\
-fill red -draw \"circle 100,100 100,70 \" \\
-fill yellow -draw \"circle 100,100 100,90 \" \\
-fill none -draw \"circle 100,100 100,20 \" \\
-fill none -draw \"circle 100,100 100,60 \" \\
-fill none -draw \"circle 100,100 100,80 \" \\
-fill none -draw \"circle 100,100 100,95 \" \\
-stroke white -fill none \\
-draw \"circle 100,100 100,40 \" circle.png");
?>
manit
Posts: 123
Joined: 2009-01-30T22:31:26-07:00

Re: is there a way to draw circles with command?

Post by manit »

basically i want to write a .bat file in window containing series of commands to draw series of circles with fixed centre & varying radii(actually radius increasing by 1 pixel successively and constant x,y coordinate of center of circle) in a particular image file.If i can give colour of periphery of circle as #RRGGBB code and radius in pixel then it will be helpful.Can anybody tell me the command?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: is there a way to draw circles with command?

Post by Bonzo »

If you had tried my example you would have seen this:

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

Re: is there a way to draw circles with command?

Post by fmw42 »

IM does not natively draw circles with a given radius. You normally have to specify a center point and a point on the perimeter. However there is a trick. See the third example at http://www.imagemagick.org/Usage/draw/#circles using stroke-linecap round line
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: is there a way to draw circles with command?

Post by el_supremo »

I've made two simple examples of drawing coloured circles on the built-in logo: image.
This first example creates a base image and then uses a FOR loop to call another batch script which draws a circle on the image.

This is the content of draw_circle.bat

Code: Select all

@rem Draw a circle on the specified image
@rem several assumptions are made about the input image to simplify
@rem this script, in particular it is assumed that the image is 640x480
@rem but with more programming the script can be made much more flexible

@rem compute the x coordinate of a point on the circumference of the circle
set /A rad=320+%1

@rem the colour of the circle depends upon its radius
set /A colour=255-%1
imconvert %2 -fill rgb(%colour%,255,127) -draw "circle 320,240 %rad%,240" %2
and this is the content of for_circle.bat:

Code: Select all

@rem create the base image
imconvert logo: logo_base.gif

@rem now draw concentric coloured circles on it
FOR /L %%A IN (120,-10,10) DO call draw_circle %%A logo_base.gif
You would execute use this with the command

Code: Select all

for_circle
This method has the disadvantage that convert is called for each iteration of the loop and so it can take a while to execute.
A faster method is for the batch file to create and then execute a batch file which contains one big convert command.
I'll post that in the next message.

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.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: is there a way to draw circles with command?

Post by el_supremo »

Here's the more efficient method of doing exactly the same thing as in my previous post.

This time, a pair of scripts is used to build another batch script. Each iteration of the for loop appends a -fill and -circle command to a file.
Once the script has been built it is executed.

This is draw_loop.bat

Code: Select all

@rem Write a sub-command to draw a circle 
@rem several assumptions are made about the input image to simplify
@rem this script, in particular it is assumed that the image is 640x480
@rem but with more programming the script can be made much more flexible

@rem compute the x coordinate of a point on the circumference of the circle
set /A rad=320+%1

@rem the colour of the circle depends upon its radius
set /A colour=255-%1

@rem It is CRUCIAL that there is NOT a space after the continuation symbol ^^ so
@rem there is NO space between the continuation and the redirection 
echo -fill rgb(%colour%,255,127) -draw ^"circle 320,240 %rad%,240^" ^^>>logo_loop.bat
This is for_loop.bat

Code: Select all

echo imconvert logo: >logo_loop.bat ^^
FOR /L %%A IN (120,-10,10) DO call draw_loop %%A
echo logo_loop.gif>>logo_loop.bat

rem now execute the script we've just built
logo_loop
Use this command to run the example:

Code: Select all

for_loop
The resulting logo_loop.gif image is identical to the logo_base.gif image produced by the example in my previous post.

NOTE how the echo statement is written in draw_loop.bat. If you put a space between ^^ and >> near the end of the echo statement, the batch script will fail because the continuation character being written into the logo_loop script must NOT have a space after it.


This is what the logo_loop.bat file will contain:

Code: Select all

imconvert logo:  ^
-fill rgb(135,255,127) -draw "circle 320,240 440,240" ^
-fill rgb(145,255,127) -draw "circle 320,240 430,240" ^
-fill rgb(155,255,127) -draw "circle 320,240 420,240" ^
-fill rgb(165,255,127) -draw "circle 320,240 410,240" ^
-fill rgb(175,255,127) -draw "circle 320,240 400,240" ^
-fill rgb(185,255,127) -draw "circle 320,240 390,240" ^
-fill rgb(195,255,127) -draw "circle 320,240 380,240" ^
-fill rgb(205,255,127) -draw "circle 320,240 370,240" ^
-fill rgb(215,255,127) -draw "circle 320,240 360,240" ^
-fill rgb(225,255,127) -draw "circle 320,240 350,240" ^
-fill rgb(235,255,127) -draw "circle 320,240 340,240" ^
-fill rgb(245,255,127) -draw "circle 320,240 330,240" ^
logo_loop.gif
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.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: is there a way to draw circles with command?

Post by Bonzo »

I thought I would have a go at this as I have never knowingly used a batch file before.

Both examples fail with something like 'imconvert' is not recognised as an internal or external command, operable program or batch file.

Changing it to convert seems to work - any reason for this ?

I have tried a simple resize as a batch file which works Ok:

Code: Select all

convert bungalow.jpg -resize 50x50 small_bungalow.png
Does anyone know of any tutorials showing how to build more complicated batch files with imagemagick?
One thing I was looking at was reading some of the image EXIF data and writing it on the image.

P.S. Could I run my resize.bat file something like "resize bungalow.jpg" and it would resize the image and save it as say "resized_bungalow.png" the call it again with "resize house.jpg" and I would get a new image called "resized_house.png". :D
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: is there a way to draw circles with command?

Post by Bonzo »

Just had a look around and found an answer to my P.S. - place holders

Code: Select all

convert %1 -resize 50x50 resized_%1
Run as resize2 input.png

Code: Select all

convert %1 -resize 50x50 resized_%2
Run as resize2 input.png output.png
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: is there a way to draw circles with command?

Post by el_supremo »

Both examples fail with something like 'imconvert' is not recognised
On my system, "convert" clashes with the Windows convert command so I made a copy of the ImageMagick convert.exe and renamed it to imconvert.exe so that I don't have any confusion over which one I'm using. And I forgot to change it back to convert when I posted it :-)
Could I run my resize.bat file something like "resize bungalow.jpg"
This script will resize its input argument and write it as a png with "resized_" prepended to the name.

resize.bat:

Code: Select all

convert %1 -resize 50x50 "%~p1resized_%~n1.png"
%~p1 is just the pathname part of the input %1 argument.
%~n1 is just the filename part
You must put quotes around the output filename because if it contains spaces (e.g. C:\Program Files\test.jpg) Dos will parse it as two (or more) separate arguments.

I don't know of any tutorial sites which specifically cover using ImageMagick commands in DOS batch files but this site has a useful summary of DOS commands (the %~p1 sort of syntax is covered under the FOR command)
http://www.pc-ask.com/xp-dos/xp-dos-cmd.html

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.
manit
Posts: 123
Joined: 2009-01-30T22:31:26-07:00

ASKING EVERYBODY

Post by manit »

I want to know the meaning of '#00ff' that is how to determine what will be colour?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: is there a way to draw circles with command?

Post by anthony »

it is fully opaque blue.
#00ff expands to #00000000FFFFFFFF or 0 for red 0 for green FFFF for blue and FFFF for alpha
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: is there a way to draw circles with command?

Post by Bonzo »

Modifying an example of Anthonys I have come up with this to add the date taken to an image:

Code: Select all

@ECHO OFF
:: Set some variables
SET INPUTFILE=%1

FOR /F %%x IN ('identify -format "%%[EXIF:DateTime]" %INPUTFILE%') DO SET DateTime=%%x

:: Add the date photo taken to the image
  convert %INPUTFILE% -pointsize 18 -fill black -gravity northwest ^
  -annotate +0+0 "Date: %DateTime%" watermarked_%INPUTFILE%
Some more experimentation rquired - the image behind the text was a bit distorted for some reason.
Image

I spent about an hour getting nowhere until I realised I had called one of my test files identify.bat. The batch file was using the identify.bat file not the ImageMagick identify program :?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: is there a way to draw circles with command?

Post by anthony »

That is an even better, and more practical example than the previous one. I will use in in IM examples, Windows DOS scripting.
http://www.imagemagick.org/Usage/api/#windows

That distortion is JPG lossy effects. You read and re-saved the JPEG image too many times. Use some other file format for intermediate results, or start with the original image and do everything in one go.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply