ImageMagick creating blank transparent square(s) according to width

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 creating blank transparent square(s) according to width

Post by stealthrt » 2018-03-23T23:27:21-07:00

Hey all I am trying to fill in the area with blank circles but its turning out looking like this:

(sized down in order not to take up so much room here. Original size: 360x1200). Also note that I do not really use the blank.png file - its just there so i can check to see if its being used or not. I'm making just a plain color box as the "blank.png".

Image

My code:

Code: Select all

    using (MagickImageCollection images = new MagickImageCollection())
 	{
                List<string> lFiles = new List<string>();

                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();
                bool isBlankImage = false;

                foreach (string tempFBProfileImg in lFiles)
                {
                    roundImg = new MagickImage(tempFBProfileImg);

                    if (Regex.IsMatch(@"C:\Users\David\Pictures\blank.png", @"\bblank.png\b"))
                    {
                        roundImg = new MagickImage(MagickColors.White, 100, 100);
                        roundImg.Resize(100, 100);
                        roundImg.Transparent(MagickColors.White);
                    }
                    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(360, 0);
                    result.Write(@"C:\Users\David\Pictures\final.png");
                }
            }
In the above code, I am creating a white 100x100 square. Then im resizing that to 100x100 and turning the white background transparent for the blank image.

The error I get is:

'width or height exceeds limit `#FFFFFFFFFFFF' @ error/cache.c/OpenPixelCache/3491'


on the result.Write(@"C:\Users\David\Pictures\final.png"); line.

When I have just this code running:

Code: Select all

    MagickImage roundImg = new MagickImage(MagickColors.White, 100, 100);
    roundImg.Resize(100, 100);
    roundImg.Transparent(MagickColors.White);
    roundImg.Write(@"C:\Users\David\Pictures\aloneTest.png");
It seems to work just fine...

Image

How can I make this work as I am needing it too?

Images used:
---------------------------------------------
Image
---------------------------------------------
Blank.png start ----
Image
Blank.png end ----

What I am wanting it to look like:

Image

Which really looks like this since blank.png is transparent:

Image

The width will be different depending on how many blank.png images are needed to be inserted to get that width. The example above has 5 images of which 4 are the blank ones.

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

Re: ImageMagick creating blank transparent square(s) according to width

Post by fmw42 » 2018-03-24T10:48:06-07:00

In Imagemgick command line, I would do the following to make your circle image and 4 other blank images. (unix syntax)

Code: Select all

    1) resize to exactly 100x100 (your image is not square so I forced it with !)
    2) and 3) draw a black circle outline on the image
    4) create a white filled circle on black background
    5) put that image into the alpha channel of the first image 
    6) use -extent to fill out the image to 5x its width (as Bonzo suggested) 
    Note I use -compose over to reset the compose method for -extent
    7) write the output

Code: Select all

    convert maggie.jpg -resize 100x100! \
    -fill none -stroke black -strokewidth 1 \
    -draw "circle 50,50 50,99" -alpha off \
    \( -size 100x100 xc:black -fill white -draw "circle 50,50 50,100" \) \
    -alpha off -compose copy_opacity -composite \
    -compose over -background none -gravity west -extent 500x100 \
    result.png
Sorry, I do not code in your API. But why do you use Magick.Colors in some cases and xc: in other cases?

Are you trying to smush each blank image or just the first? If all images, then that is likely your issue, as you are smushing past your image size.

The documentation says: "appends an image sequence together ignoring transparency."

So smashing with transparent images does not work the way you want.

I tried this for 3 and it runs, but fails for 4.

Code: Select all

   convert -size 100x100 xc:none xc:none xc:none -smush -35+0 x.png

Code: Select all

   convert -size 100x100 xc:none xc:none xc:none xc:none -smush -35+0 x.png
convert: width or height exceeds limit `x.png' @ error/cache.c/OpenPixelCache/3906.
convert: memory allocation failed `x.png' @ error/png.c/WriteOnePNGImage/9067.
convert: Invalid image height in IHDR `x.png' @ warning/png.c/MagickPNGWarningHandler/1665.
convert: Image height exceeds user limit in IHDR `x.png' @ warning/png.c/MagickPNGWarningHandler/1665.
convert: Invalid IHDR data `x.png' @ error/png.c/MagickPNGErrorHandler/1639.

But also note that the second (transparent image) smushes over your image so as to trim it as explained above. For example:

Code: Select all

    convert -size 100x100 xc:red xc:none +smush -35 test.png
Note that the size is 65x100.

stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Re: ImageMagick creating blank transparent square(s) according to width

Post by stealthrt » 2018-03-24T15:19:36-07:00

I'm just needing the part where it creates the blank circle images. I have the code for producing the images that actually are images as in my example, 1.jpg (maggie). In my logic I check to see if the current image should be an image or a blank image.

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

Re: ImageMagick creating blank transparent square(s) according to width

Post by fmw42 » 2018-03-24T15:25:26-07:00

This is the part that makes the first image into a circle:

Code: Select all

convert maggie.jpg -resize 100x100! \
-fill none -stroke black -strokewidth 1 \
-draw "circle 50,50 50,99" -alpha off \
\( -size 100x100 xc:black -fill white -draw "circle 50,50 50,100" \) \
-alpha off -compose copy_opacity -composite \
result.png
If you only want the circle with transparency outside and no black circle, then

Code: Select all

convert maggie.jpg -resize 100x100! \
\( -size 100x100 xc:black -fill white -draw "circle 50,50 50,100" \) \
-alpha off -compose copy_opacity -composite \
result.png
or

Code: Select all

convert maggie.jpg -resize 100x100! \
\( +clone -fill black -colorize 100 -fill white -draw "circle 50,50 50,100" \) \
-alpha off -compose copy_opacity -composite \
result.png
Sorry I do not know the Magick.NET code.

stealthrt
Posts: 32
Joined: 2018-03-08T18:09:21-07:00
Authentication code: 1152

Re: ImageMagick creating blank transparent square(s) according to width

Post by stealthrt » 2018-03-24T20:19:19-07:00

Yeah im not able to reproduce this in C# so i hope dlemstra can help me out again

Post Reply