how to correct the dpi in a scanned file

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?".
cts

how to correct the dpi in a scanned file

Post by cts »

identify(1) says the PNM files created by my epson scanner have a resolution of 72x72. On the other hand, scanimage(1) claims it creates the files with 300x300 dpi. When I view them, only about 20% fits on my screen. I've googled and searched and experimented for many hours, but I can't get anything to turn my files into (e.g.) A4 pngs. The Ubuntu scan program manages it without a problem, but I don't like that GUI-based pgm. But it must be trivial (I tried -resize (or was it -resample?) and after a half-hour, I killed it).

Can anyone help me with the correct options to convert(1)?
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: how to correct the dpi in a scanned file

Post by Drarakel »

If you want to convert your A4 scans to PNG and all that's missing is the density info, then you can use that:
convert input.pnm -set units PixelsPerInch -set density 300 output.png
cts

Re: how to correct the dpi in a scanned file

Post by cts »

Thanks for the suggestion, but it didn't help - I still get the full picture, of which my screen is only about 20%. The identy(1) output is:

Image: pix.pnm

Format: PNM (Portable anymap)
Class: PseudoClass
Geometry: 2528x3509+0+0
Type: Bilevel
Endianess: Undefined
Colorspace: Gray
Depth: 1-bit
Channel depth:
Gray: 1-bit
Channel statistics:
Gray:
Min: 0 (0)
Max: 1 (1)
Mean: 0.97531 (0.97531)
Standard deviation: 0.155178 (0.155178)
Histogram:
8651735: (255,255,255) #FFFFFF white
219017: ( 0, 0, 0) #000000 black
Colormap: 2
0: ( 0, 0, 0) #000000 black
1: (255,255,255) #FFFFFF white
Rendering intent: Undefined
Resolution: 72x72
Units: Undefined
Filesize: 1.05751mb
Interlace: None
Background color: white
Border color: rgb(223,223,223)
Matte color: grey74
Transparent color: black
Page geometry: 2528x3509+0+0
Dispose: Undefined
Iterations: 0
Compression: Undefined
Orientation: Undefined
Comment: SANE data follows

Signature: 6037ffcb05fa6940e22803079d7fc9fe819d07f0322402a94194d937d35323ab
Tainted: False
Version: ImageMagick 6.3.5 09/25/07 Q16 http://www.imagemagick.org
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to correct the dpi in a scanned file

Post by fmw42 »

IM 6.3.5 is nearly 300 versions old. Perhaps it is time for an upgrade.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: how to correct the dpi in a scanned file

Post by Drarakel »

Yeah - my commandline from above was also only for a current ImageMagick version.
And what exactly do you want to achieve? If you want to reduce the dimensions, then you should use "-resize" (or "-resample", but for that you would have to update the density info first).
cts

Re: how to correct the dpi in a scanned file

Post by cts »

I'm just trying to use my scanner as a copy machine.

I'm using Suse 10.3. This is not out of the dark ages. But I tested

convert test.pnm -set units PixelsPerInch -set density 300 test.png

on my wife's ubuntu machine with convert at

Version: ImageMagick 6.3.7 06/04/09 Q16 http://www.imagemagick.org

and got the same results. The identify of the output file is:

Image: test.png

Format: PNG (Portable Network Graphics)
Class: PseudoClass
Geometry: 2528x3509+0+0
Type: Bilevel
Endianess: Undefined
Colorspace: Gray
Depth: 1-bit
Channel depth:
Gray: 1-bit
Channel statistics:
Gray:
Min: 0 (0)
Max: 1 (1)
Mean: 0.97531 (0.97531)
Standard deviation: 0.155178 (0.155178)
Histogram:
8651735: (255,255,255) #FFFFFF white
219017: ( 0, 0, 0) #000000 black
Colormap: 2
0: ( 0, 0, 0) #000000 black
1: (255,255,255) #FFFFFF white
Rendering intent: Undefined
Resolution: 72x72
Units: Undefined
Filesize: 41.1934kb
Interlace: None
Background color: black
Border color: rgb(223,223,223)
Matte color: grey74
Transparent color: black
Page geometry: 2528x3509+0+0
Dispose: Undefined
Iterations: 0
Compression: Zip
Orientation: Undefined
Comment: SANE data follows

Density: 300
Signature: 66499efaa0bccb012c796c7491851d53b5e545bb32846cc0d3625796070e2c71
Units: PixelsPerInch
Tainted: False
User time: 0.450u
Elapsed time: 0:02
Pixels per second: 8.45981mb
Version: ImageMagick 6.3.7 06/04/09 Q16 http://www.imagemagick.org


The explicit suggestion of using -resize reminded me of the result of that particular test: using -resize A4 naturally didn't work. Using -resize 210 (where 210 is the width of A4 paper) presumably gives me an image that's 210 pixels wide...

Okay, thank you, you've helped me clarify my question. It was said,

(or "-resample", but for that you would have to update the density info first).

What's not clear to me is what the -density refers to - the real density of the input file or the real density of the output file?

I mean, my scanner scans at 300 dpi, my printer prints at 300 dpi, the scanimage(1) output file says 72 dpi, but when I just print the output file that I created with:

convert test.pnm -set density 300 test.png

I get a huge imagine over may pages of paper.

This must be a problem that a lot of people have - maybe they just don't know how to ask.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: how to correct the dpi in a scanned file

Post by Drarakel »

cts wrote:I'm using Suse 10.3. This is not out of the dark ages. But I tested

convert test.pnm -set units PixelsPerInch -set density 300 test.png

on my wife's ubuntu machine with convert at

Version: ImageMagick 6.3.7 06/04/09 Q16 http://www.imagemagick.org
That ImageMagick version is old, too. :) The former commandline with "-set" works only in a current IM version (version 6.6.3-3 or newer).
If you don't want to/can't update ImageMagick, then use this commandline:
convert -units PixelsPerInch test.pnm -density 300 test.png
(Actually, it's stored as pixels per meter in PNG, but you don't have to worry about that.)
cts

Re: how to correct the dpi in a scanned file

Post by cts »

That ImageMagick version is old, too. :) The former commandline with "-set" works only in a current IM version (version 6.6.3-3 or newer).
If you don't want to/can't update ImageMagick, then use this commandline:
convert -units PixelsPerInch test.pnm -density 300 test.png
(Actually, it's stored as pixels per meter in PNG, but you don't have to worry about that.)
Please note that I ran the proposed command (with the -set option) on both SuSE 10.3 and on a recently updated Ubuntu machine without error. This is not the issue.

To reiterate, if my scanner scans at 300 dpi and my printer prints at 300 dpi, and the scanned image says it's at 72 dpi, what convert command do I need to set things straight.

Alternatively, I could pursue getting scanimage(1) to report the correct value, but I thought this would be the easier approach, but getting convert(1) to set the correct density is starting to look like a hard problem.
cts

Re: how to correct the dpi in a scanned file

Post by cts »

Maybe the problem is elsewhere, e.g. in lpr(1)? I guess it's not so surprising that an A4 image scanned at 300 dpi should be multiple times my 1200x1600 screen. But why wouldn't it fit on an A4 300 dpi printer?

On the other hand, xsane on my wife's Ubuntu box gets it right (but xsane is horrible for a lot of other reasons).
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: how to correct the dpi in a scanned file

Post by Drarakel »

It's not difficult - just use that command line for your file: :!:
Drarakel wrote:convert -units PixelsPerInch test.pnm -density 300 test.png
(Command lines with a different order of the arguments are possible - but I generally recommend the above order.)

Again: My first command line (that uses e.g. "-set units" instead of just "-units") is NOT suitable for your ImageMagick installations, as - again - you have versions that are too old for that. (Unless you have updated to some 6.6.3 or 6.6.4 version in the meantime.)
cts

Re: how to correct the dpi in a scanned file

Post by cts »

As stated above, I did try that command (even before posting to this forum), and it didn't achieve the purpose of reducing the huge image created by scanimage(1) to the correct size on the printer.

I appreciate your patient help but I don't think you exactly understand the problem. Perhaps somehow else will understand how to use a 300 dpi scanner and a 300 dpi printer as a copy machine, with the help of scanimage(1), convert(1), and lpr(1).
cts

Re: how to correct the dpi in a scanned file

Post by cts »

Incidently, I have diffed the identity(1) outputs before and after I ran the suggested command:

1,2c1,2
< Image: test.png
< Format: PNG (Portable Network Graphics)
---
> Image: test.pnm
> Format: PNM (Portable anymap)
24,26c24,26
< Resolution: 299.999x299.999
< Units: PixelsPerInch
< Filesize: 41.1406kb
---
> Resolution: 72x72
> Units: Undefined
> Filesize: 1.05751mb
28c28
< Background color: black
---
> Background color: white
35c35
< Compression: Colorize
---
> Compression: Undefined
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: how to correct the dpi in a scanned file

Post by Drarakel »

cts wrote:As stated above, I did try that command
May I remind you what you really had stated:
cts wrote:Please note that I ran the proposed command (with the -set option)
And the command with the "-set" option is wrong for your old ImageMagick versions! Is that so difficult to understand?

NOW, with your last post, you apparently succeeded in writing your PNG image with a correct density value:
< Resolution: 299.999x299.999
< Units: PixelsPerInch
So, with that output, you now should have a correct 300dpi A4 PNG. That's what you wanted - right?
If you still have the same problems now with printing, then it's a problem of the printer itself or of your printing software (lpr).

Some additional small notes:
To get that output from above, you must have updated your ImageMagick version - or you really have used the command without "-set" now. One can't tell with your posts... And.. well, normally, ImageMagick displays such an info in a PNG file as "118.11 PixelsPerCentimeter". So, your output from identify with approx. 300 PixelsPerInch is still a bit strange. But I will assume now that you used the right commands. (And perhaps something like "identify -verbose -units PixelsPerInch" was used with the PNG file.)
Besides, your image is actually a little bit broader than DIN A4. (With 300dpi, the width should be about 2480 pixels, not 2528 pixels.) But that shouldn't be a big problem.
cts

Re: how to correct the dpi in a scanned file

Post by cts »

Drarakel wrote:
cts wrote:As stated above, I did try that command
May I remind you what you really had stated:
cts wrote:Please note that I ran the proposed command (with the -set option)
And the command with the "-set" option is wrong for your old ImageMagick versions! Is that so difficult to understand?

I've tried every combination of everything that you and anybody else has recommended, with the same results. I have not updated the software since this conversation has begun.
NOW, with your last post, you apparently succeeded in writing your PNG image with a correct density value:
< Resolution: 299.999x299.999
< Units: PixelsPerInch
So, with that output, you now should have a correct 300dpi A4 PNG. That's what you wanted - right?
If you still have the same problems now with printing, then it's a problem of the printer itself or of your printing software (lpr).
I've forgotten how I may have originally stated my problem, but in the end, I want to be able to
* scan a piece of paper on my scanner
* do any required density/size conversion
* lpr it out
and I can't do that. All variations give me a page that is roughly 5 times larger than a sheet of paper, and roughly 5 times larger than my screen. Including the file with the 299.99 density.

This is using stock SuSE 10.3. I'm pretty sure that I get the same results with a recently updated (within 2 months) of Ubuntu.

It's true that identity(1) says my output file has density of 300, but apparently, the starting assumptions of the conversion are not correct. Apparently, the -density argument specifies the output size - but if it thinks it's converting from 72 dpi ... it's not going to get it right.

What I really need is the command to force the file to have the real density - it's already 300. Using convert to *change* it to 300 is clearly leading to trouble.


Some additional small notes:
To get that output from above, you must have updated your ImageMagick version - or you really have used the command without "-set" now. One can't tell with your posts... And.. well, normally, ImageMagick displays such an info in a PNG file as "118.11 PixelsPerCentimeter". So, your output from identify with approx. 300 PixelsPerInch is still a bit strange. But I will assume now that you used the right commands. (And perhaps something like "identify -verbose -units PixelsPerInch" was used with the PNG file.)
Right. I did that to make sure we all understood we were talking apples and apples.

Besides, your image is actually a little bit broader than DIN A4. (With 300dpi, the width should be about 2480 pixels, not 2528 pixels.) But that shouldn't be a big problem.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: how to correct the dpi in a scanned file

Post by Drarakel »

I can only try to help you if you have a problem with an image that you want to solve with ImageMagick.
I can't really help you if you have problems in the handling of your scanning and printing software.
Well, maybe you should try to save your scanned images (in 'scanimage') in a 'better' format right at the start. (A format that can store the correct density, e.g. TIFF, PNG, BMP etc. - not PNM.) That would minimise your problem. And maybe you have to specify a manual density for printing with 'lpr'. But I don't use that software - I'm usually on Windows, not on Linux. Maybe someone else that uses these tools on Unix/Linux wants to try and give some hints...

Anyway - regarding your problem with the correct density value in the PNG file: That should be solved.
cts wrote:What I really need is the command to force the file to have the real density
You did that.

But to be 100% sure, you could post a link to one of your corrected PNG files (e.g. your 'test.png' that shows 299.999x299.999 PixelsPerInch), so that we can take a closer look.
Post Reply