dlemstra / ImageMagick.FredsScripts.NET

Fred's ImageMagick Scripts for .NET
http://www.fmwconcepts.com/imagemagick/
Other
46 stars 11 forks source link

TextCleanerScript returning blank page #35

Closed starnutoditopo closed 1 year ago

starnutoditopo commented 1 year ago

I'm trying to clean some page, but the TextCleanerScript outputs a blank page.

Debugging the source code, I noticed that the problem is in the RemoveNoise method, when calling Negate.

It seems that the problem is caused by the Alpha channel, so I added a line to fix it:

        private void RemoveNoise(IMagickImage<TQuantumType> image)
        {
            using (var second = image.Clone())
            {
                second.ColorSpace = ColorSpace.Gray;
                second.Alpha(AlphaOption.Off); // <- Added this line to fix
                second.Negate();
                second.AdaptiveThreshold(FilterSize, FilterSize, FilterOffset);
                second.ContrastStretch((Percentage)0);

                if (SmoothingThreshold != null)
                {
                    var max = _factory.Quantum.Max.ToDouble(null);
                    second.Blur(SmoothingThreshold.Value.ToDouble() / 100, max);
                    second.Level(SmoothingThreshold.Value, new Percentage(100));
                }

                image.Composite(second, CompositeOperator.CopyAlpha);
            }

            image.Opaque(_factory.Colors.Transparent, BackgroundColor);
            image.Alpha(AlphaOption.Off);
        }

Maybe the

            image.Opaque(_factory.Colors.Transparent, BackgroundColor);
            image.Alpha(AlphaOption.Off);

should be performed at the beginning of the method, BEFORE cloning the image?

dlemstra commented 1 year ago

Maybe it should be image.Negate(Channels.RGB); instead? And do you have a test image that I can use to reproduce this so I can add a unit test for this?

p.s. I hope you are aware that you need a commercial license when you use this in a commercial application.

starnutoditopo commented 1 year ago

Thank you. A sample image is in attachment (hope github doesn't process it once uploaded).

image_4

ps.: At the moment I'm just experimenting with this tool; if it suits the requirements for the project I'm working on, then some people of the commercial team will deal with licencing.

dlemstra commented 1 year ago

I have updated the script and added a unit test to make sure this stays fixed. Thanks for reporting this.