Reputation: 10552
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".
My code:
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:
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...
How can I make this work as I am needing it too?
Images used:
Blank.png start ----
Blank.png end ----
What I am wanting it to look like is this:
which really looks like this, since blank.png is transparent:
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.
Using Bonzos example in C#:
roundImg.Resize(new MagickGeometry(100, 100));
roundImg.BackgroundColor = MagickColors.Transparent;
roundImg.Extent(360, 100, Gravity.West);
result = roundImg;
Produces just a transparent 360x100 image.
Tried fmw42:
mask = new MagickImage("xc:black", 100, 100);
mask.Settings.FillColor = MagickColors.White;
mask.Draw(new DrawableCircle(50, 50, 50, 100));
mask.HasAlpha = false;
mask.Resize(100, 100);
roundImg.Composite(mask, CompositeOperator.CopyAlpha);
Possible solution
if (Regex.IsMatch(tempFBProfileImg.ToLower(), @"\bblank.png\b"))
{
result.Extent(360, 100, Gravity.West);
images.Add(result);
break;
}
which results in:
Upvotes: 1
Views: 31681
Reputation: 53174
I'm just needing the part where it creates the blank circle images.
This is the part that makes the first image into a circle:
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
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
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. But it is just creating a black and white circle image the same size as the resized maggie image and putting it into the alpha channel of the maggie image using the equivalent of -compose copy_opacity -composite.
Upvotes: 0
Reputation: 53174
In Imagemgick command line, I would do the following to make your circle image and 4 other blank images. (unix syntax)
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
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.
convert -size 100x100 xc:none xc:none xc:none -smush -35+0 x.png
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:
convert -size 100x100 xc:red xc:none +smush -35 test.png
Note that the size is 65x100.
Upvotes: 1
Reputation: 5299
Why do you not use extent with a transparent background to increase the canvas size?
I do not know c# but in command line it would be in this format:
convert ZME5U.jpg -background transparent -gravity west -extent 800x284 result.png
Upvotes: 2