public IMagickImage Execute(IMagickImage image, ValueItemCollection values) { Variables = values; if (RemoveBackgroundActive) { image.ColorAlpha(MagickColors.Transparent); image.Alpha(AlphaOption.Set); image.VirtualPixelMethod = VirtualPixelMethod.Transparent; // image.ColorFuzz = new Percentage(RemoveBackgroundThreshold); // image.TransparentChroma(new MagickColor("#FFFFFF"), new MagickColor("#F0F0F0")); // image.Transparent(MagickColor.FromRgb(255, 255, 255)); var clone = image.Clone(); clone.ColorFuzz = new Percentage(RemoveBackgroundThreshold); clone.TransparentChroma(new MagickColor("#FFFFFF"), new MagickColor("#F0F0F0")); clone.Transparent(MagickColor.FromRgb(255, 255, 255)); if (RemoveBackgroundFeather > 0) { clone.Scale(new Percentage(10)); clone.Blur(0, RemoveBackgroundFeather, Channels.Alpha); clone.Resize(image.Width, image.Width); } image.Composite(clone, CompositeOperator.CopyAlpha); image.Alpha(AlphaOption.Background); using (MagickImageCollection images = new MagickImageCollection()) { var back = new MagickImage(MagickColors.White, image.Width, image.Height); images.Add(back); images.Add(image); image = images.Flatten().Clone(); } } // image.Threshold(new Percentage(RemoveBackgroundThreshold)); // image.Transparent(MagickColor.FromRgb(255, 255, 255)); return(image); }
static public void RoundImage(this IMagickImage <byte> image) { using var mask = new MagickImage(MagickColors.Transparent, image.Width, image.Height); new Drawables() .FillColor(MagickColors.White) .Circle(image.Width / 2, image.Height / 2, image.Width / 2, 0) .Draw(mask); image.Alpha(AlphaOption.On); image.Composite(mask, CompositeOperator.Multiply); }
private void CopyOpacity(IMagickImage image) { image.Alpha(AlphaOption.Off); using (var gray = image.Clone()) { gray.ColorSpace = ColorSpace.Gray; gray.Negate(); gray.AdaptiveThreshold(FilterSize, FilterSize, FilterOffset); gray.ContrastStretch((Percentage)0); if (Threshold.HasValue) { gray.Blur((double)Threshold.Value / 100.0, Quantum.Max); gray.Level(Threshold.Value, new Percentage(100)); } image.Composite(gray, CompositeOperator.CopyAlpha); image.Opaque(MagickColors.Transparent, BackgroundColor); image.Alpha(AlphaOption.Off); } }
private void RemoveNoise(IMagickImage image) { using (var second = image.Clone()) { second.ColorSpace = ColorSpace.Gray; second.Negate(); second.AdaptiveThreshold(FilterSize, FilterSize, FilterOffset); second.ContrastStretch((Percentage)0); if (SmoothingThreshold != null) { second.Blur(SmoothingThreshold.Value.ToDouble() / 100, Quantum.Max); second.Level(SmoothingThreshold.Value, new Percentage(100)); } image.Composite(second, CompositeOperator.CopyAlpha); } image.Opaque(MagickColors.Transparent, BackgroundColor); image.Alpha(AlphaOption.Off); }
public void ShouldCopyTheAlphaChannelWithCopyAlpha() { var readSettings = new MagickReadSettings() { BackgroundColor = MagickColors.None, FillColor = MagickColors.White, FontPointsize = 100, }; using (IMagickImage image = new MagickImage("label:Test", readSettings)) { using (IMagickImage alpha = image.Clone()) { alpha.Alpha(AlphaOption.Extract); alpha.Shade(130, 30); alpha.Composite(image, CompositeOperator.CopyAlpha); ColorAssert.AreEqual(new MagickColor("#7fff7fff7fff0000"), alpha, 0, 0); ColorAssert.AreEqual(new MagickColor("#7fff7fff7fffffff"), alpha, 30, 30); } } }
private IMagickImage DistortOverlay(IMagickImage grayShirt, IMagickImage overlay, PointD[] overlayCoordinates, PointD[] tshirtCoordinates) { using (var images = new MagickImageCollection()) { grayShirt.Alpha(AlphaOption.Transparent); grayShirt.BackgroundColor = MagickColors.Transparent; images.Add(grayShirt); var croppedOverlay = CropOverlay(overlay, overlayCoordinates); croppedOverlay.VirtualPixelMethod = VirtualPixelMethod.Transparent; var arguments = CreateArguments(overlayCoordinates, tshirtCoordinates); var distortSettings = new DistortSettings() { Bestfit = true }; croppedOverlay.Distort(DistortMethod.Perspective, distortSettings, arguments); ApplySharpen(croppedOverlay); images.Add(croppedOverlay); return(images.Merge()); } }
/// <summary> /// Write image data from a pdf file to a bitmap file /// </summary> /// <param name="imgData">Image Data</param> private static void ConvertPdfToBmp(ImageData imgData) { MagickReadSettings settings = new MagickReadSettings(); // Settings the density to 600 dpi will create an image with a better quality settings.Density = new Density(600); using (MagickImageCollection images = new MagickImageCollection()) { // Add all the pages of the pdf file to the collection images.Read(imgData.PdfFilePath, settings); // Create new image that appends all the pages horizontally using (IMagickImage image = images.AppendVertically()) { // Remove the transparency layers and color the background white image.Alpha(AlphaOption.Remove); image.Settings.BackgroundColor.A = 65535; image.Settings.BackgroundColor.R = 65535; image.Settings.BackgroundColor.G = 65535; image.Settings.BackgroundColor.B = 65535; // Convert the image to a bitmap image.Format = MagickFormat.Bmp; // Delete any old file if (File.Exists(imgData.BmpFilePath)) { File.Delete(imgData.BmpFilePath); } // Save result as a bmp image.Write(imgData.BmpFilePath); } } }