Force Density Units on Read?

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
danburke
Posts: 1
Joined: 2017-12-13T10:38:21-07:00
Authentication code: 1152

Force Density Units on Read?

Post by danburke »

I am attempting to create a unit test around some dynamically generated images and wanted to ensure that images are being created at 300x300 dpi. In certain formats, such as png, they are being read back in metric, which fails my asserts. Is there a way to force it to dpi on read?

Some reproduce code:

Code: Select all

            string lvFile = @"C:\Temp\test.png";

            using (MagickImage lvImage = new MagickImage(new MagickColor(Color.Black), 2550, 3300))
            {
                lvImage.Settings.Format = MagickFormat.Png;
                lvImage.Settings.Compression = Compression.Group4;
                lvImage.Settings.Density = new Density(300, 300, DensityUnit.PixelsPerInch); 
                lvImage.Settings.Debug = true;
                lvImage.Settings.Verbose = true;
                lvImage.Density = new Density(300, 300, DensityUnit.PixelsPerInch);

                lvImage.Write(lvFile);
            }

            using (MagickImage lvWrittenImage = new MagickImage(lvFile))
            {
                Trace.WriteLine(lvWrittenImage.Density); // Outputs 118.11x118.11 cm
            }
I am running .Net 4.5.1 with nuget package Magick.NET-Q8-AnyCPU version 7.2.0.

Thanks
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Force Density Units on Read?

Post by snibgo »

I believe the PNG standard allows for integer numbers, "units unknown" and "per metre" only. So it doesn't record density as "per inches". See http://www.w3.org/TR/2003/REC-PNG-20031110/#11pHYs

EDIT: I'll add that, at the command line, we can change the units to get what we want after the read, eg:

Code: Select all

convert r.png -units pixelsperinch -verbose info:
snibgo's IM pages: im.snibgo.com
Post Reply