public static void DeleteGrayscaleImg(string path, float thresh, bool invert) { MagickImage img = IOUtils.ReadImage(path); if (img == null) { return; } img.ColorSpace = ColorSpace.HSL; float saturation = 100f; using (IMagickImage channel = img.Separate(Channels.Gray).First()) { saturation = channel.FormatExpression("%[fx:mean]").GetFloat(); Logger.Log("Image Saturation: " + saturation.ToString("0.00")); } if (!invert && saturation < thresh) { Logger.Log("Deleting " + Path.GetFileName(path)); File.Delete(path); } if (invert && saturation > thresh) { Logger.Log("Deleting " + Path.GetFileName(path)); File.Delete(path); } }
public static bool CheckGrayscale(this MagickImage i) { using (MagickImage image = i) { // -colorspace HSL image.ColorSpace = ColorSpace.HSL; // -channel g -separate +channel string r; using (IMagickImage channel = image.Separate(Channels.Gray).First()) { // -format "%[fx:mean]" r = channel.FormatExpression("%[fx:mean]"); } return(double.Parse(r, NumberStyles.Any, CultureInfo.InvariantCulture) < 1e-10); } }