Cannot set units to pixelsperinch?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
sdaau
Posts: 10
Joined: 2011-05-23T03:49:56-07:00
Authentication code: 8675308

Cannot set units to pixelsperinch?

Post by sdaau »

I looked a bit at this post, ImageMagick • View topic - -size -density -units - and I tried to come up with a command line, which could set "PixelsPerInch" as units it output image (as read by `identify`). But no matter what order of arguments I use (or which case is used for "pixelsperinch" ), it seems I only get "Units: Undefined" or "Units: PixelsPerCentimeter" - but I never get "PixelsPerInch"??

My platform:

Code: Select all

$ convert --version
Version: ImageMagick 6.6.2-6 2012-08-17 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2010 ImageMagick Studio LLC
Features: OpenMP 

$ echo $(cat /etc/issue) $(uname -a)
Ubuntu 11.04 \n \l Linux mypc 2.6.38-16-generic #67-Ubuntu SMP Thu Sep 6 18:00:43 UTC 2012 i686 i686 i386 GNU/Linux
For example, this is the kind of results I get:

Code: Select all

convert -size 100x100 xc:red test.png
  Geometry: 100x100+0+0
  Resolution: 72x72
  Print size: 1.38889x1.38889
  Units: Undefined
convert -size 100x100 xc:red -units PixelsPerInch test.png
  Geometry: 100x100+0+0
  Resolution: 28.34x28.34
  Print size: 3.52858x3.52858
  Units: PixelsPerCentimeter
convert -size 100x100 -units PixelsPerInch xc:red test.png
  Geometry: 100x100+0+0
  Resolution: 28.34x28.34
  Print size: 3.52858x3.52858
  Units: PixelsPerCentimeter
convert -size 100x100 xc:red -units PixelsPerInch -density 72 test.png
  Geometry: 100x100+0+0
  Resolution: 28.34x28.34
  Print size: 3.52858x3.52858
  Units: PixelsPerCentimeter
convert -size 100x100 xc:red -density 72 -units PixelsPerInch test.png
  Geometry: 100x100+0+0
  Resolution: 28.34x28.34
  Print size: 3.52858x3.52858
  Units: PixelsPerCentimeter
convert -size 100x100 xc:red -set units PixelsPerInch test.png
  Geometry: 100x100+0+0
  Resolution: 72x72
  Print size: 1.38889x1.38889
  Units: Undefined
convert -size 100x100 -set units PixelsPerInch xc:red test.png
  Geometry: 100x100+0+0
  Resolution: 72x72
  Print size: 1.38889x1.38889
  Units: Undefined
convert -size 100x100 -units PixelsPerInch xc:red -set units pixelsperinch test.png
  Geometry: 100x100+0+0
  Resolution: 28.34x28.34
  Print size: 3.52858x3.52858
  Units: PixelsPerCentimeter
convert -size 100x100 -set units PixelsPerInch xc:red -units pixelsperinch test.png
  Geometry: 100x100+0+0
  Resolution: 28.34x28.34
  Print size: 3.52858x3.52858
  Units: PixelsPerCentimeter
... which were obtained with the following `bash` script:

Code: Select all

INimg="xc:red"
OUTIMG="test.png"
# ppi="pixelsperinch"
ppi="PixelsPerInch"

for icmd in \
    "convert -size 100x100 $INimg $OUTIMG" \
    "convert -size 100x100 $INimg -units $ppi $OUTIMG" \
    "convert -size 100x100 -units $ppi $INimg $OUTIMG" \
    "convert -size 100x100 $INimg -units $ppi -density 72 $OUTIMG" \
    "convert -size 100x100 $INimg -density 72 -units $ppi $OUTIMG" \
    "convert -size 100x100 $INimg -set units $ppi $OUTIMG" \
    "convert -size 100x100 -set units $ppi $INimg $OUTIMG" \
    "convert -size 100x100 -units $ppi $INimg -set units pixelsperinch $OUTIMG" \
    "convert -size 100x100 -set units $ppi $INimg -units pixelsperinch $OUTIMG" \
  ;
  do
    echo $icmd ;
    $icmd ;
    identify -verbose $OUTIMG | grep 'Geometry:\|Resolution:\|Print size:\|Units:' ;
  done
What am I doing wrong? Or is this possibly a bug?

Many thanks in advance for any answers,
Cheers!
sdaau
Posts: 10
Joined: 2011-05-23T03:49:56-07:00
Authentication code: 8675308

Re: Cannot set units to pixelsperinch?

Post by sdaau »

Hmm... not sure if this explains it - but maybe someone can confirm:

I noticed if I set the "Print Size" resolution from Gimp in pixels/inch, `identify -verbose` will nonetheless report "Units: PixelsPerCentimeter"!

Then I discovered, that if I supply `-units pixelsperinch` to `identify`, then it will report in "Units: PixelsPerInch" ?! E.g.:

Code: Select all

$ convert -size 100x100 xc:red test.png \ 
  && identify -verbose test.png | grep 'Geometry:\|Resolution:\|Print size:\|Units:' 
  && echo \ 
  && identify -units pixelsperinch -verbose test.png | grep 'Geometry:\|Resolution:\|Print size:\|Units:'

  Geometry: 100x100+0+0
  Resolution: 72x72
  Print size: 1.38889x1.38889
  Units: Undefined

  Geometry: 100x100+0+0
  Resolution: 72x72
  Print size: 1.38889x1.38889
  Units: PixelsPerInch
Is this how it is supposed to work? Then again, I could swear I had seen some images before, that report "Units: PixelsPerInch" just with `identify -verbose` - what would be different for them?

EDIT: This seems related: ImageMagick • View topic - Determining DPI through ImageMagick ; ImageMagick • View topic - Setting the density in PSD and PNG ; ImageMagick • View topic - how to correct the dpi in a scanned file (`-set units` vs `units`)
Last edited by sdaau on 2014-02-13T08:11:32-07:00, edited 4 times in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Cannot set units to pixelsperinch?

Post by snibgo »

I'm not sure what you are asking.

In "convert", the "-units" option defines the units for the density you are supplying. The data may or may not be stored in the file with those units.

In "identify", the "-units" option defines the units that will be used for reporting the density. If necessary, "identify" will do the arithmetic to convert from the units in the file to the reported units.

Does that answer the question?
snibgo's IM pages: im.snibgo.com
sdaau
Posts: 10
Joined: 2011-05-23T03:49:56-07:00
Authentication code: 8675308

Re: Cannot set units to pixelsperinch?

Post by sdaau »

Hi snibgo,
snibgo wrote: In "identify", the "-units" option defines the units that will be used for reporting the density. If necessary, "identify" will do the arithmetic to convert from the units in the file to the reported units.

Does that answer the question?
That does it exactly - thanks!

My mistake was - I always thought that "Units" are somehow hardcoded in the image (e.g. 0 for Undefined, 1 for PixelsPerCentimeter, 2 for PixelsPerInch), and I thought that - when using the `-units` option with `convert`, it would set this field; and then `identify` would read out that field "as is".

That is why I got confused - I was trying to "set" this `-units` to PixelsPerInch in the "convert" step; and was quite surprised that `identify` would read it out by default as PixelsPerCentimeter.

Apparently, this means that different resolution units are _not_ encoded in the image files (maybe an image file format works only with a single, predefined by specs, resolution unit); and `-units` simply means what units should ImageMagick internally work with (e.g. so as to interpret the `-density` numbers on the command line, etc).

Many thanks again,
Cheers!
Last edited by sdaau on 2014-02-13T08:10:14-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Cannot set units to pixelsperinch?

Post by snibgo »

File formats vary. I think png always stores an integer number of pixels per metre, so "convert" will do the multiplication, or "unknown".

If you want more control about the metadata stored in the file, exiftool is useful.
snibgo's IM pages: im.snibgo.com
Post Reply