Create a round section of a picture

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
robsch
Posts: 9
Joined: 2012-07-09T05:36:13-07:00
Authentication code: 13

Create a round section of a picture

Post by robsch »

Hi,

I'm a totally new to IM. So I'd be glad if anyone can help me a bit with this.

My task is to create round images from rectangular ones. E.g. from a 400x300 pixel image I need the round section with the middle point of the rectangle image is the middlepoint of the disk. I think this can only be made with a transparent area around the round section, right? I'd like to know how I could do this in 2 ways: first with a specified radius and second with a radius that is half of a side length of the original image.

How do I have to do this?

Robert
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Create a round section of a picture

Post by Bonzo »

You will have to do some calculations and they may be best done outside of Imagemagick. How are you running your code?
robsch
Posts: 9
Joined: 2012-07-09T05:36:13-07:00
Authentication code: 13

Re: Create a round section of a picture

Post by robsch »

The processing will be started with PHP, but with a direct command line call not with Imagick-API.

IM itselsf is not able to do some calculations for this? Though, it is no problem to this with PHP.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Create a round section of a picture

Post by Bonzo »

When IM draws a circle you give the X and Y for the center and the X and Y for the start point. These all come from the top left hand corner so you need to calculate Y the start point from the center point Y - the radius
robsch
Posts: 9
Joined: 2012-07-09T05:36:13-07:00
Authentication code: 13

Re: Create a round section of a picture

Post by robsch »

With these commands I have had already sucess:

Code: Select all

convert -size 500x375 xc:black -fill white -draw "circle 250,187 250,1" circle.png
composite -compose Dst_In -gravity center circle.png someimage.jpg -alpha Set result.png
This image was of size 500x375. To let PHP calculate the positioning shouldn't be an big issue.

But here I have to create a circle image first what is not that optimal if I want to process many pictures with different sizes. It's okay if there is no other solution. I'm just wondering if those commands could be combined so that the circle image don't have to be created physically.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Create a round section of a picture

Post by Bonzo »

Untested:

Code: Select all

convert -size 500x375 xc:black -fill white -draw "circle 250,187 250,1" miff:- | composite -compose Dst_In -gravity center - someimage.jpg -alpha Set result.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Create a round section of a picture

Post by fmw42 »

try this. you can do your parameter computation for the center of the image (cx,cy) and the radius in PHP if you want.


infile="logo:"
cx=`convert $infile -format "%[fx:w/2]" info:`
cy=`convert $infile -format "%[fx:h/2]" info:`
radius=`convert xc: -format "%[fx:min($cx,$cy)]" info:`
convert $infile \
\( +clone -fill black -colorize 100% \
-fill white -draw "translate $cx,$cy circle 0,0 $radius,0" \) \
-alpha off -compose copy_opacity -composite \
logo_circle.png


see
http://www.imagemagick.org/Usage/draw/#circles
http://www.imagemagick.org/Usage/compose/#copyopacity
robsch
Posts: 9
Joined: 2012-07-09T05:36:13-07:00
Authentication code: 13

Re: Create a round section of a picture

Post by robsch »

Dear Bonzo and fmw42,

thanks for your help so far.

@Bonzo: This would be a possible solution. However, I cannot try this out. My solution worked locally on Windows fine but on the Linux server I've got a problem with '-alpha Set'. This creates an error as it seems. Version on the server is 6.2.4 Q16. Does this version support the alpha argument?

@fmw42: Yes, this worked as well!

Code: Select all

convert rose.jpg ( +clone -fill black -colorize 100% -fill white -draw "translate 250,187 circle 0,0 100,1" ) -alpha off -compose copy_opacity -composite out.png
Probably an even better solution. Again, however, on my remote Linux system it doesn't produce any file :(, though it doesn't seem to produce an error (as fas as I can see, since I use the 'system' (like exec) command in php). Is it the version? Or is something missing? I don't know much about IM. I don't have a glue how it has to be installed or if anything particular is installed. Even worse is that I don't have a shell to the system. I just know that IM is working. I have done some test and could produce image files. Also my composite command does work if I remove the alpha parameter.

Do you have any further advise?

Thanks again,
Robert
robsch
Posts: 9
Joined: 2012-07-09T05:36:13-07:00
Authentication code: 13

Re: Create a round section of a picture

Post by robsch »

Okay, I have realized now that this version is already 5 years old. I guess, some things won't work with this version. I hope I can get a newer version installed. But I'm wondering if my request isn't possible with this version or if there are any other approaches.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Create a round section of a picture

Post by fmw42 »

If you are working in PHP, you probably need to preface the convert command with the full path to convert, which is usually /usr/local/bin or /usr/bin. However, to be sure run

<?php
echo "<pre>";
system("type -a convert");
echo "</pre>";
?>

or

<?php
echo "<pre>";
system("which -a convert");
echo "</pre>";
?>

Also check your safe mode in PHP. see viewtopic.php?f=1&t=21377#p87452
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Create a round section of a picture

Post by Bonzo »

Try this:

Code: Select all

$array=array(); 
echo "<pre>";
exec(" convert rose.jpg \( +clone -fill black -colorize 100% -fill white -draw \"translate 250,187 circle 0,0 100,1\" \) -alpha off -compose copy_opacity -composite out.png2>&1", $array);  
echo "<br>".print_r($array)."<br>";  
echo "</pre>";
Note on linux you need to change ( ) to \( \)
robsch
Posts: 9
Joined: 2012-07-09T05:36:13-07:00
Authentication code: 13

Re: Create a round section of a picture

Post by robsch »

:D Now it's working! :D

I was told that there is a newer version in /usr/bin/im_6.6. With this version it works.

Thank you both for your help. It would have taken weeks to get this solution if I was trying this on my own. So, thank you!

Cheers,
Robert

PS: One little error is in the exec command: It has to be 'out.png 2>&1', not 'out.png2>&1'.
Post Reply