1

I am new to imageMagick and am using it to convert a PDF shipping label into a PNG but am losing the resolution on the barcode. How can I increase the readability?

Here is the snippet of the imageMagick commands I am using:

img.Resize(new MagickGeometry(800,0));
img.Threshold((Percentage) 60);
img.Write(outputPng);

And here is the result: enter image description here

Here is a screenshot of the actual PDF I converted from enter image description here

UPDATE Here is the revised snippet that works and gets me a darn near 1-to-1 resolution:

var settings = new MagickReadSettings {Density = new Density(200)};

using (var images = new MagickImageCollection())
{
    images.Read(inputPdf, settings);
    using (var img = images.AppendVertically())
    {
        img.Density = new Density(150);
        img.Trim();
        img.Quality = 72;
        img.Sharpen(0, 1.0);
        img.ColorType = ColorType.Bilevel;
        img.Depth = 1;
        img.Alpha(AlphaOption.Off);
        img.Write(outputPng);
    }
}
xpeldev
  • 1,812
  • 2
  • 12
  • 23

1 Answers1

1

I'm a little rusty, but resizing as the last operation is usually a good idea unless you're sure the dimensions are virtual and only used for output rendering.

In addition, my guess is that this is more related to the PDF loading than the PDF writing. To confirm this, save the screenshot as a bmp/gif/jpg and try the same transformations.

Also, remember that the library first and foremost is a command line tool, so the documentation for the original library is a primary source. Here's one thing I found: https://stackoverflow.com/a/6605085

Which says an important thing for PDFs can be vector objects, where you'll need to enter a density configuration value for rasterization pixel density.

Kache
  • 15,647
  • 12
  • 51
  • 79
  • Thank you so much for the reference. Im able to get matching quality now! – xpeldev Mar 05 '19 at 21:58
  • You were right: The density has to be set initially, BEFORE loading the PDF: var settings = new MagickReadSettings {Density = new Density(300)}; – xpeldev Mar 05 '19 at 22:09