convert -density does not work for this image...

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
jannilrau
Posts: 2
Joined: 2011-05-26T06:31:04-07:00
Authentication code: 8675308

convert -density does not work for this image...

Post by jannilrau »

I have an image and I need to change the meta data from 182 DPI to 300 DPI in order to allow easy printing of 80 x 60 cm poster. I do this automatically for huge amounts of images without problems, but this image does not go thru...

The image name is src.jpg. By applying the cmdline tool convert I create the image target1.jpg, but the result has wrong meta data of 762 DPI. I use:
convert src.jpg -density 300x300 target1.jpg
Also applying the unit parameter -units has no effect...
convert src.jpg -units PixelsPerInch -density 300x300 target2.jpg
I have absolutely no errors, but the meta data is wrong. What's happening?

The files are available here: The difference between target1.jpg and target2.jpg is just 5 bytes.

prodsrv: # cmp -l target1.jpg target2.jpg
14 2 1
15 1 2
16 54 372
17 1 2
18 54 372
prodsrv: #

Who can help?
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: convert -density does not work for this image...

Post by whugemann »

I think your problems result from the fact that the resolution of src.jpg is 72 Pixels per centimeter (and not inch, as usual). IM seems to multply the density by 2.54.

As far as I can see, this is a slight bug in IM, as you cannot influence this behaviour.

The workaround would be to calculate the desired resolution having the above factor in mind.

I do however think that there are easier ways to just set the resolution of a JPEG, without re-encoding it lossy. Under Windows, the resolution can be set with IrfanView lossless jpeg transformation plugin and on other systems jpegtran would be the tool of choice.
Wolfgang Hugemann
jannilrau
Posts: 2
Joined: 2011-05-26T06:31:04-07:00
Authentication code: 8675308

Re: convert -density does not work for this image...

Post by jannilrau »

Thanks a lot, I wasn't aware that it multiplies by 2.54. That explains the DPI settings...

The problem is that I have full automated image processing here, so the usage of IrfanView's edit density plugin is not acceptable...

In any case I also modify some other image parameters, so the lossless option is not my focus. I just uploaded this very simplyfied example in order to reproduce the error... Is there no way to fix the glitch within this IM convert call?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: convert -density does not work for this image...

Post by anthony »

The problem is exactly what should happen when each of the two options are called.

Using -denisty should set both the resultion number and the units at the same time.

The real problem however is that the option is both a setting (just stored for later use, such as when an image is read in) and a operator (changing image meta-data changing of existing images).

The distinction is subtile, and is the cause of a lot of grief in the current implementation of the Command Line Interface, not just for this but in may other image meta-data settings. It is this distintion that many options are already split into two types... for example you use -label before reading images and -set label after reading images. Also the reason their is a difference between the options -page and -repage.

See http://www.imagemagick.org/Usage/basics/#attributes

The -denisty image meta-data attribute is one that has not 'split' in this way.

This distinction is something I want to fix in IM v7 (currently being forked) but it is not clear what should be done.

(suggestions?)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert -density does not work for this image...

Post by fmw42 »

The -denisty image meta-data attribute is one that has not 'split' in this way.

This distinction is something I want to fix in IM v7 (currently being forked) but it is not clear what should be done.

(suggestions?)
You could consider a -set density and -convert-density (or -change-density or the like)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: convert -density does not work for this image...

Post by anthony »

If we follow the current convention then
-density sets the density for reading images
-set density sets the density within existing images.

NOTE: that -density for read also sets the density attribute of images as they are read in (like -label does).

As such really there are currently three uses of the -density option:
  • reading density for vector images;
  • set (override) the density attribute on images being read in;
  • and set density attribute on images in memory.
And this is before any addition of 'units' which probably should be included as a integral part of the density setting itself!

What I like to know just how these 3 distinctions of density (and other) settings should be handled by the future IMv7 CLI interface? The most important aspect would be clarity to users.

Do not answer this here, I am moving this specific question to a new topic in development.
viewtopic.php?f=2&t=18837
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply