0

One of our customers need to convert PDF shipping labels to PNG images. The PDF images need to be 300 DPI and have a bit depth of 1 (pure black and white without grayscale).

I have got this working but with some issues that i can not find any solution for.

My Code

MagickNET.SetGhostscriptDirectory(_ghostscriptPath);
            var settings = new MagickReadSettings();
            settings.Density = new Density(_dpi);
            //settings.ColorType = ColorType.Bilevel;                 // Not working
            //settings.Depth = 1;                                     // Not working
            //settings.SetDefine(MagickFormat.Png, "Bit-depth", "1"); // Not working

            using (var images = new MagickImageCollection())
            {
                images.Read(sourceFilePath, settings);
                using (var horizontal = images.AppendHorizontally())
                {
                    //horizontal.Density = new Density(_dpi);         // Not working
                    horizontal.BitDepth(1);                           // Testing (Sometimes commented out)
                    horizontal.Write(targetPath_working);
                }
            }

Result

When this code is run with the following setup (BitDepth(1) + 300 DPI) the PNG image is 1169x2303 and (4Bit depth).

When this code is run with the following setup (Removed BitDepth(1) + 300 DPI) the PNG image is 1169x2303 and (32Bit depth).

This gives me 2 primary issues, when BitDepth is set to 1 why is the PNG image still 4bit? Second the quality of that 4bit image is horrible and unreadable by a barcode scanner. It feels like the image is somehow being resized during the writing process. I would need to have the "sharpness" of the 32 bit image but as 1 bit.

Could someone point me in the right direction here, feels like i am lacking the knowhow of image conversion.

Thank you!

PS: I'm using Magick.NET-Q8-AnyCPU (7.23.3)

Test suggestion 1

Results in image with black lines as borders for everything, none of the text is filled with black however the image is now 1bit as expected.

MagickNET.SetGhostscriptDirectory(_ghostscriptPath);
            var settings = new MagickReadSettings();
            settings.Density = new Density(_dpi);
            settings.SetDefine(MagickFormat.Png, "Bit-depth", "1");

            using (var images = new MagickImageCollection())
            {
                images.Read(sourceFilePath, settings);
                using (var horizontal = images.AppendHorizontally())
                {
                    horizontal.AutoLevel();
                    horizontal.Threshold(new Percentage(50));
                    horizontal.Write(targetPath_working);
                }
            }
Carl
  • 89
  • 1
  • 8
  • Try using `SetDefine(MagickFormat.Png, "Bit-depth", "1")` and including thresholding to 50% to ensure the image is bi-level because the PNG writer will not honour your `defines` if it believes that would cause a loss of quality. – Mark Setchell Jun 24 '21 at 08:42
  • Edited my original post, with that code i get a picture that looks like black border lines on text and rest is white. There is no black inside the text. – Carl Jun 24 '21 at 09:13
  • Ok, try using `auto-level` first in order to move your dark greys to solid black and your light greys to pure white before doing the thresholding. Explanation: If your blacks have brightness 3 and your greys have brightness 15, then thresholding them at 50% (i.e. 127 out of a possible 255) will make everything black - so make your whites white and your blacks black, then threshold. – Mark Setchell Jun 24 '21 at 10:29
  • Adding `horizontal.AutoLevel();` before `horizontal.Threshold(new Percentage(50));` had no effect. I still get the white with black borders on everything sadly. – Carl Jun 24 '21 at 20:14

1 Answers1

0

Found solution for my issue see code below. Had to use a number of different settings in combination for a good end result.

MagickNET.SetGhostscriptDirectory(_ghostscriptPath);
            var settings = new MagickReadSettings();
            settings.Density = new Density(_dpi);
            settings.SetDefine(MagickFormat.Png, "Bit-depth", "1");

            using (var images = new MagickImageCollection())
            {
                images.Read(sourceFilePath, settings);
                using (var horizontal = images.AppendHorizontally())
                {
                    horizontal.AutoLevel();
                    horizontal.Threshold(new Percentage(50));
                    horizontal.ColorType = ColorType.Bilevel;
                    horizontal.Format = MagickFormat.Png8;
                    horizontal.Write(targetPath_working);
                }
            }
Carl
  • 89
  • 1
  • 8