ImageMagick create circle count on top of image

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

ImageMagick create circle count on top of image

Post by stealthrt »

Hey all I have the following Magick.net code:

Code: Select all

    List<string> lFiles = new List<string>();
    bool isBlankImage = false;
    
    lFiles.Add(@"C:\Users\David\Pictures\1.jpg");
    lFiles.Add(@"C:\Users\David\Pictures\blank.png");
    lFiles.Add(@"C:\Users\David\Pictures\blank.png");
    lFiles.Add(@"C:\Users\David\Pictures\blank.png");
    lFiles.Add(@"C:\Users\David\Pictures\blank.png");

    IMagickImage roundImg = new MagickImage();
    IMagickImage mask = new MagickImage();
    IMagickImage shadow = new MagickImage();
    IMagickImage result = new MagickImage();
    
    foreach (string tempFBProfileImg in lFiles)
    {
         roundImg = new MagickImage(tempFBProfileImg);
    
         if (Regex.IsMatch(tempFBProfileImg.ToLower(), @"\bblank.png\b"))
         {
             result.Extent(360, 100, Gravity.West);
             images.Add(result);
             isBlankImage = true;
             break;
         }
         else
         {
             mask = new MagickImage("xc:black", 100, 100);
             mask.Settings.FillColor = MagickColors.White;
             mask.Draw(new DrawableCircle(50, 50, 50, 90));
             mask.HasAlpha = false;
    
             roundImg.Resize(100, 100);
             roundImg.Composite(mask, CompositeOperator.CopyAlpha);
             roundImg.Draw(new DrawableStrokeColor(MagickColors.Black), 
                           new DrawableStrokeWidth(1),
                           new DrawableFillColor(MagickColors.None),
                           new DrawableCircle(50, 50, 50, 90));
    
             shadow = new MagickImage("xc:none", 100, 100);
             shadow.Settings.FillColor = MagickColors.Black;
             shadow.Draw(new DrawableCircle(50, 50, 50, 90));
             shadow.Blur(0, 5);
             
             roundImg.Composite(shadow, CompositeOperator.DstOver);
    
             images.Add(roundImg);
             images.First().BackgroundColor = MagickColors.None;
    
             result = images.SmushHorizontal(-35);
             result.Resize(100, 100);
             result.Write(@"C:\Users\David\Pictures\final2.png");
         }                                    
    }
     
    var imgText = new MagickImage(MagickColors.Blue, 0, 0);
        
    imgText.Settings.FontPointsize = 24;
    imgText.BackgroundColor = MagickColors.White;
    imgText.Settings.FillColor = MagickColors.Black;
    imgText.Settings.TextAntiAlias = true;
    imgText.Settings.FontFamily = "Arial";
    imgText.Read("label: 10+", 45, 25);
    string caption = "label:10+";
    //imgText = new MagickImage(caption, settings);
    //imgText.Draw(new DrawableText(100, 20, "10+"));
    imgText.Page = new MagickGeometry(300, 100, 0, 0);
    
    result.Composite(imgText, CompositeOperator.Over);
    result.Write(@"C:\Users\David\Pictures\final2.png");
And this comes out looking like this:
Image

But what I am looking to want it to look like is this:
Image

I'm having issues with just making a red circle with a black outline and shadow with the text XX+ inside it. Also, I've looked high and low to find out how i can position it where I need it but I am unable to find it. I thought that MagickGeometry would be it but it turns out that its not.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: ImageMagick create circle count on top of image

Post by snibgo »

You already have code that makes a circular area opaque, with a black outline. Use the same approach for the text.

Instead of a white background for your text, you want red. You have:

Code: Select all

imgText.BackgroundColor = MagickColors.White;
Can you see what needs changing?
snibgo's IM pages: im.snibgo.com
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Re: ImageMagick create circle count on top of image

Post by stealthrt »

Changing some code I now get this:
Image

Changed code:

Code: Select all

    roundImg.Composite(mask, CompositeOperator.CopyAlpha);
    roundImg.Draw(new DrawableStrokeColor(MagickColors.Black),
                  new DrawableStrokeWidth(1),
                  new DrawableFillColor(MagickColors.None),
                  new DrawableCircle(50, 50, 50, 90));
    imgText.Composite(roundImg, CompositeOperator.Over);
    imgText.Settings.FontPointsize = 24;
    imgText.BackgroundColor = MagickColors.Red;
    imgText.Settings.FillColor = MagickColors.White;
    imgText.Settings.TextAntiAlias = true;
    imgText.Settings.FontFamily = "Arial";
    imgText.Read("label: 10+", 45, 25);
    //string caption = "label:This is a very long caption line";
    //imgText = new MagickImage(caption, settings);
    //imgText.Draw(new DrawableText(100, 20, "10+"));
    imgText.Page = new MagickGeometry(360, 100, 0, 0);
    result.Composite(imgText, CompositeOperator.Over);
    result.Write(@"C:\Users\David\Pictures\final2.png"); 
    
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Re: ImageMagick create circle count on top of image

Post by stealthrt »

Ok I have the code correct for the round circle with number in it:

Code: Select all

    MagickImage circleText = new MagickImage(MagickColors.Transparent, 29, 29);
    MagickImage circle = new MagickImage(MagickColors.Transparent, 29, 29);

    circleText.Settings.BorderColor = MagickColors.Black;              
    circleText.Settings.FontPointsize = 16;
    circleText.Settings.FontWeight = ImageMagick.FontWeight.Bold;
    circleText.BackgroundColor = MagickColors.Transparent;
    circleText.Settings.FillColor = MagickColors.White;
    circleText.Settings.TextGravity = Gravity.Center;
    circleText.Settings.TextAntiAlias = true;
    circleText.Settings.FontFamily = "Arial";
    circleText.Border(2);
    circleText.Read("label: 10+", 29, 29);
    circle.Draw(new DrawableStrokeColor(MagickColors.Black),
                new DrawableStrokeWidth(1),
                new DrawableFillColor(MagickColors.Red),
                new DrawableCircle(14, 14, 10, 1));
    circleText.Composite(circle, CompositeOperator.DstOver);
                
    result.Composite(circleText, CompositeOperator.Over);
    result.Write(@"C:\Users\David\Pictures\final2.png");
Which produces the image:
Image

But now I can not seem to get the border for the text to show up and I'm still stuck on how to move the circle to its intended spot?
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Re: ImageMagick create circle count on top of image

Post by stealthrt »

Still looking for some guidance and help with this please.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: ImageMagick create circle count on top of image

Post by fmw42 »

It looks fine. What is wrong with the result you are getting?
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Re: ImageMagick create circle count on top of image

Post by stealthrt »

fmw42 wrote: 2018-03-28T09:27:49-07:00 It looks fine. What is wrong with the result you are getting?
But now I can not seem to get the border for the text to show up and I'm still stuck on how to move the circle to its intended spot?
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Re: ImageMagick create circle count on top of image

Post by stealthrt »

Did that make since?
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Re: ImageMagick create circle count on top of image

Post by stealthrt »

Just cant seem to figure out how to move the text+circle over 50px and down 10px....
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Re: ImageMagick create circle count on top of image

Post by stealthrt »

I finally got it to move over where I need it to be using this code:

Code: Select all

    MagickImage circleText = new MagickImage(MagickColors.Transparent, 29, 29);
    MagickImage circle = new MagickImage(MagickColors.Transparent, 29, 29);
    MagickImage _shadow = new MagickImage();

    circleText.Settings.BorderColor = MagickColors.Black;
    circleText.Settings.FontPointsize = 16;
    circleText.Settings.FontWeight = ImageMagick.FontWeight.Bold;
    circleText.BackgroundColor = MagickColors.Transparent;
    circleText.Settings.FillColor = MagickColors.White;
    circleText.Settings.TextGravity = Gravity.Center;
    circleText.Settings.TextAntiAlias = true;
    circleText.Settings.FontFamily = "Arial";
    circleText.Border(2);
    circleText.Read("label: 10+", 29, 29);
    circle.Draw(new DrawableStrokeColor(MagickColors.Black),
                        new DrawableStrokeWidth(1),
                        new DrawableFillColor(MagickColors.Red),
                        new DrawableCircle(14, 14, 10, 1));

    _shadow = new MagickImage("xc:none", 29, 29);
    _shadow.Settings.FillColor = MagickColors.Black;
    _shadow.Draw(new DrawableCircle(14, 14, 10, 1));
    _shadow.Blur(0, 15);

    circle.Composite(_shadow, CompositeOperator.DstOver);

    circleText.Composite(circle, CompositeOperator.DstOver);
                
    result.Composite(circleText, 68, 9, CompositeOperator.Over);
    result.Write(@"C:\Users\David\Pictures\final2.png");
BUT when I try to add the shadow to the circle it comes out looking like this:
Image

Not sure why the shadow is coming out being a square when its using the exact circle x,y,x,y as the original red circle...
stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Re: ImageMagick create circle count on top of image

Post by stealthrt »

fmw42 wrote: 2018-03-28T09:27:49-07:00 It looks fine. What is wrong with the result you are getting?
Can you let me know how to fix the square shadow and make it into a round shadow even if you don’t know the c# version?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: ImageMagick create circle count on top of image

Post by fmw42 »

Your problem is that the size of the transparent image holding your red circle is too small. Thus the blur gets cut off by the ends of the image before tapering off. You need to increase the size of the transparent image keeping the circle centered and the same size and decrease the amount of blur.

convert -size 51x51 xc:none -fill red -draw "circle 25,25 20,40" \
-fill none -stroke black -draw "circle 25,25 20,39" \
+stroke -fill white -pointsize 16 -font Arial -gravity center -draw "text 0,0 '10+'" \
\( +clone -alpha extract -negate -blur 0x7 -write mpr:blur \) \
+swap -compose over -composite \
\( mpr:blur -negate -white-threshold 15% \) \
-alpha off -compose copy_opacity -composite \
red_circle_blur.png


Image
Post Reply