65 lines
1.5 KiB
C#
65 lines
1.5 KiB
C#
using ImageMagick;
|
|
using Ocr.Tesseract.Extensions;
|
|
using Ocr.Tesseract.Screenshots.Configuration;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace Ocr.Tesseract.Screenshots;
|
|
|
|
/// <inheritdoc />
|
|
public class ScreenshotProcessor : ImageProcessor
|
|
{
|
|
/// <inheritdoc />
|
|
public ScreenshotProcessor(ScreenshotProcessorConfiguration configuration)
|
|
: base(configuration)
|
|
{
|
|
}
|
|
|
|
/// <inheritdoc cref="System.Diagnostics.Process"/>
|
|
public override IEnumerable<MagickImage> Process(MagickImage image)
|
|
{
|
|
var tImage = image.CloneImage();
|
|
|
|
if (Configuration.EnableResizing)
|
|
{
|
|
tImage = tImage
|
|
.ResizeImage(
|
|
2f,
|
|
FilterType.Lanczos2Sharp,
|
|
PixelInterpolateMethod.Mesh
|
|
)
|
|
.Resample(300, DensityUnit.PixelsPerInch);
|
|
|
|
yield return tImage.CloneImage();
|
|
}
|
|
|
|
if (Configuration.EnableThresholding)
|
|
{
|
|
tImage = tImage
|
|
.NormalizeImage()
|
|
.RemoveAlpha(MagickColors.White)
|
|
.ToGrayscale()
|
|
.ThresholdAdaptive(Configuration.ThresholdWidth, Configuration.ThresholdHeight)
|
|
.ToBinary();
|
|
}
|
|
|
|
if (Configuration.Border > 0)
|
|
{
|
|
tImage = tImage.AddBorder(Configuration.Border, MagickColors.White);
|
|
}
|
|
|
|
yield return tImage;
|
|
yield return tImage.CloneImage().NegateColors();
|
|
}
|
|
|
|
#region Overrides of Processor<MagickImage,IMagickImageValueProcessorSettings>
|
|
|
|
/// <inheritdoc />
|
|
public override IEnumerable<MagickImage> Process(IEnumerable<MagickImage> inputs)
|
|
{
|
|
return inputs.SelectMany(Process);
|
|
}
|
|
|
|
#endregion
|
|
}
|