Sigur
Sigur

Reputation: 354

On dpi option from imsave

Consider the following MWE to generate a random image:

import matplotlib.pyplot as plt
import numpy as np

pts = np.random.random_sample((1024, 1024))

plt.imsave('foo.png',pts, dpi=300)

I'm trying to understand how the dpi option works. According to the matplotlib.pyplot.imsave documentation,

dpi : int

The DPI to store in the metadata of the file. This does not affect the resolution of the output image.

The output of the program above is a 1024x1024 image file.

What I don't understand is the fact that neither identify -verbose foo.png nor exiftool foo.png shows the image resolution.

But, opening it with ImageMagick (display) and checking the image info, I find

enter image description here

So, what is the math behind the resolution and printing size values?

How to obtain a 300dpi resolution image?

Upvotes: 3

Views: 3401

Answers (2)

fmw42
fmw42

Reputation: 53089

In ImageMagick, you can set the output density by

convert image <processing> -density 300 newimage


Then to check the density you can do either

identify -verbose newimage

or

identify -format "%xx%y"


to find the density (resolution)

Upvotes: 1

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339102

Image formats like png do not have a dpi defined. If you save a 1024 x 1024 pixel array via imsave, the image will simply be 1024 x 1024 pixel.

Imagemagick seems to ignore any metadata, so it assumes a resolution of 96 dpi. From the pixel size (1024) and the dpi (96) it then calculates the size in inches to be

1024 dots / 96 dots per inch  = 10.667 inch

That said, the question "How to obtain a 300dpi resolution image?" is not really clear. But most graphics viewers would allow to scale the image prior to printing, so it shouldn't be a problem to get a 300 dpi print on paper.

Upvotes: 1

Related Questions