public override void SaveDiffIamgeTo(string diffImagePath)
        {
            if (base.NoFileExists)
            {
                return;
            }

            var img1 = ChangeImage(image1);
            var img2 = ChangeImage(image2);

            MagickImage diffImage = new MagickImage();

            img1.SetHighlightColor(new MagickColor(parms.HighLightColor));

            img1.Compare(img2, ErrorMetric.Absolute, diffImage);

            diffImage.Despeckle();

            MakeSemiTranspernt(image2);

            if (parms.ShowImages)
            {
                image1.Composite(image2, CompositeOperator.Atop);
                if (parms.HighLightColor != "None")
                {
                    image1.Composite(diffImage, CompositeOperator.Atop);
                }
                image1.Write(diffImagePath);
            }
            else
            {
                diffImage.Write(diffImagePath);
            }
        }
        public void CreateNewImageFrom(string source, string target)
        {
            using (MagickImage image = new MagickImage(source))
            {
                image.Deskew(DeskewPercentage);
                image.Despeckle();
                image.Grayscale(PixelIntensityMethod.Rec709Luminance);

                image.Write(target);
            }
        }
Example #3
0
        private static void RemoveLines(MagickImage original, MagickImage image, string geometryValue)
        {
            image.Scale(new MagickGeometry(geometryValue));
            MagickGeometry geometry = new MagickGeometry(original.Width, original.Height);

            geometry.IgnoreAspectRatio = true;
            image.Scale(geometry);

            image.AutoLevel();
            //image.Shave(20, 20);
            image.Despeckle();
            image.ReduceNoise();

            image.Morphology(MorphologyMethod.Erode, Kernel.Diamond, 2);
            using (MagickImage clone = (MagickImage)image.Clone())
            {
                image.Negate();
                image.Composite(original, Gravity.Center);
            }
        }
Example #4
0
        private Bitmap processImgForScanning(Bitmap imgInput)
        {
            using (MemoryStream memstream = new MemoryStream())
            {
                imgInput.Save(memstream, ImageFormat.Tiff);
                MagickImage img = new MagickImage(memstream.ToArray());


                if (sharpen)
                {
                    img.Sharpen((int)sharpenIntX.Value, (int)sharpenIntY.Value, Channels.All);
                }

                if (autoGamma)
                {
                    img.AutoGamma();
                }

                if (enhance)
                {
                    img.Enhance();
                }

                if (contrast)
                {
                    img.Contrast();
                }

                if (autoLevel)
                {
                    img.AutoLevel();
                }

                if (autoOrient)
                {
                    img.AutoOrient();
                }

                if (despeckle)
                {
                    img.Despeckle();
                }


                if (medianFilter)
                {
                    img.MedianFilter((int)medianInt.Value);
                }

                if (unsharpmask)
                {
                    img.Unsharpmask(6.8, 4, 4, 0);
                }

                if (wtThreshold)
                {
                    img.LinearStretch((float)0.9, 0.1);
                    //img.WhiteThreshold((int)wtThresInt.Value);
                    //img.ReduceNoise();
                    //img.Grayscale(PixelIntensityMethod.Brightness);
                }

                if (invert)
                {
                    img.Negate();
                }

                return(img.ToBitmap());
            }
        }
        public async Task <AnimationExtractionResult> ExtractFrames(string animationPath, string outputFolderPath, Func <bool> shouldTerminateDelegate = null)
        {
            string animationName      = Path.GetFileNameWithoutExtension(animationPath);
            string outputFramesFormat = Path.Combine(outputFolderPath, animationName) + "_%04d.png";

            double framerate = 0;

            using (MagickImageCollection gifFrames = new MagickImageCollection(animationPath))
            {
                foreach (var frame in gifFrames)
                {
                    if (shouldTerminateDelegate())
                    {
                        return(null);
                    }

                    framerate += frame.AnimationDelay / 100.0 / gifFrames.Count;
                }
            }

            framerate = 1.0 / framerate;

            var ffmpegInstance = new FfmpegInstance(FfmpegPath);

            ffmpegInstance.Options = new FfmpegRawOptions
            {
                RawParams = $"-i \"{animationPath}\" -vf fps={framerate} \"{outputFramesFormat}\""
            };

            var ffmpegResult = await ffmpegInstance.Start(null, null, shouldTerminateDelegate);

            if (shouldTerminateDelegate())
            {
                return(null);
            }

            if (ffmpegResult.ExitCode != 0)
            {
                Logger.Error("Failed to extract GIF frames for {InputAnimationPath} with ffmpeg, ffmpeg output was {@FfmpegOutput}", animationPath, ffmpegResult.OutputStreamData);
                return(null);
            }

            var animationFiles = Directory.EnumerateFiles(outputFolderPath).Where(f => Path.GetFileName(f).StartsWith(animationName + "_")).ToList();

            List <string> outputFiles = new List <string>();

            for (int i = 1; i <= animationFiles.Count; i++)
            {
                string originalIdxString = i.ToString();
                originalIdxString = new string('0', 4 - originalIdxString.Length) + originalIdxString;

                string correctedIdxString = (i - 1).ToString();
                correctedIdxString = new string('0', 4 - correctedIdxString.Length) + correctedIdxString;

                string originalFile  = $"{outputFolderPath}\\{animationName}_{originalIdxString}.png";
                string correctedFile = $"{outputFolderPath}\\{animationName}_{correctedIdxString}.png";
                File.Move(originalFile, correctedFile);


                outputFiles.Add(correctedFile);
            }

            if (DenoiseAmount > 0)
            {
                Logger.Verbose("Frame extraction complete, denoising by {DenoiseAmount}x", DenoiseAmount);

                foreach (var frame in outputFiles)
                {
                    if (shouldTerminateDelegate())
                    {
                        return(null);
                    }

                    using (MagickImage img = new MagickImage(frame))
                    {
                        for (int i = 0; i < DenoiseAmount; i++)
                        {
                            img.Despeckle();
                        }

                        img.Write(frame);
                    }
                }
            }

            return(new AnimationExtractionResult
            {
                Fps = framerate,
                ExtractedFiles = outputFiles
            });
        }