LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2014, 09:50 AM   #1
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Ghostscript resolution confusion - slow performance


I store all documents electronically. Sometimes I can print the invoice as PDF from a website (e.g. amazon). Sometimes I have to scan the image myself and convert it to pdf.

When I scan the image, the workflow is like this:
Code:
scanimage --resolution=200 > iomega.ppm
convert iomega.ppm iomega-p.jpg
convert iomega-p.jpg iomega-p.pdf
Using Imagemagick's identify these are the resolutions for the .jpg and .pdf:
iomega-p.jpg JPEG 1653x2337 1653x2337+0+0 8-bit DirectClass 563KB 0.010u 0:00.010
iomega-p.pdf PDF 1653x2337 1653x2337+0+0 16-bit Bilevel DirectClass 484KB 0.140u 0:00.030

So far, so good.
Now I have a different application which I use to view, annotate and store the PDFs. In that application I want to view the PDFs on screen. So I use the external gs command to convert the PDF to PNG so I can display it on screen.
Code:
gs -sPAPERSIZE=a4 -sDEVICE=png16m -r200 -o iomega-p.png iomega-p.pdf
Here is where the trouble starts, as gs converts the image to a much higher resolution.
iomega-p.png PNG 4592x6492 4592x6492+0+0 8-bit DirectClass 6.196MB 1.880u 0:01.869
I don't need this, and it makes processing an order of magnitude slower. That is I have to wait longer before the image is being displayed.

When I change the resolution to 72 dpi, the resulting PNG is exactly the correct resolution.

However, I cannot do that in the application. Because the PDF files which I did not scan, but print from the web browser have a much lower resolution:
amazon.pdf PDF 612x792 612x792+0+0 16-bit Bilevel DirectClass 61KB 0.030u 0:00.009
Converting those at 72 dpi produce way too low a quality.
amazon.png PNG 612x792 612x792+0+0 8-bit DirectClass 18.1KB 0.030u 0:00.029

Questions:
- Why is gs converting back at a resolution of 4592x6492? The original was 200 dpi and 1653x2337.
- Why is a printed PDF at a resolution of 612x792 perfectly readable in Okular, while it is apparently only 72 dpi?
- What am I doing wrong here?

jlinkels
 
Old 05-02-2014, 10:06 AM   #2
mdooligan
Member
 
Registered: Feb 2009
Location: Vancouver BC
Distribution: Mandrake10/ArchHackery/Gentoo
Posts: 179

Rep: Reputation: 22
When printing PNGs or whatever, the conversion command I use is:
Code:
convert -page letter "$sfile" "$psfile"
where $sfile is the PNG, and $psfile is FILE.ps. I t does a lovely job, and is pretty quick about it too.
 
Old 05-02-2014, 10:20 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Original Poster
Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
My problem is converting a PDF to PNG.

Converting from JPG to PDF I do with convert like you do.

jlinkels
 
Old 05-02-2014, 12:42 PM   #4
Spect73
Member
 
Registered: Aug 2013
Distribution: Slackware 14.1
Posts: 128

Rep: Reputation: Disabled
Quote:
Originally Posted by jlinkels View Post
I store all documents electronically. Sometimes I can print the invoice as PDF from a website (e.g. amazon). Sometimes I have to scan the image myself and convert it to pdf.

When I scan the image, the workflow is like this:
Code:
scanimage --resolution=200 > iomega.ppm
convert iomega.ppm iomega-p.jpg
convert iomega-p.jpg iomega-p.pdf
Using Imagemagick's identify these are the resolutions for the .jpg and .pdf:
iomega-p.jpg JPEG 1653x2337 1653x2337+0+0 8-bit DirectClass 563KB 0.010u 0:00.010
iomega-p.pdf PDF 1653x2337 1653x2337+0+0 16-bit Bilevel DirectClass 484KB 0.140u 0:00.030

So far, so good.
Now I have a different application which I use to view, annotate and store the PDFs. In that application I want to view the PDFs on screen. So I use the external gs command to convert the PDF to PNG so I can display it on screen.
Code:
gs -sPAPERSIZE=a4 -sDEVICE=png16m -r200 -o iomega-p.png iomega-p.pdf
Here is where the trouble starts, as gs converts the image to a much higher resolution.
iomega-p.png PNG 4592x6492 4592x6492+0+0 8-bit DirectClass 6.196MB 1.880u 0:01.869
I don't need this, and it makes processing an order of magnitude slower. That is I have to wait longer before the image is being displayed.

When I change the resolution to 72 dpi, the resulting PNG is exactly the correct resolution.

However, I cannot do that in the application. Because the PDF files which I did not scan, but print from the web browser have a much lower resolution:
amazon.pdf PDF 612x792 612x792+0+0 16-bit Bilevel DirectClass 61KB 0.030u 0:00.009
Converting those at 72 dpi produce way too low a quality.
amazon.png PNG 612x792 612x792+0+0 8-bit DirectClass 18.1KB 0.030u 0:00.029

Questions:
- Why is gs converting back at a resolution of 4592x6492? The original was 200 dpi and 1653x2337.
- Why is a printed PDF at a resolution of 612x792 perfectly readable in Okular, while it is apparently only 72 dpi?
- What am I doing wrong here?

jlinkels
Try using -r with two values. According to the man page, -r with a single number sets both x and y to that resolution. Experimenting here shows a huge difference in output size if I use only a single number.
 
Old 05-02-2014, 12:53 PM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Original Poster
Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Exactly the same result:
jlinkels@donald-pc:/tmp$ identify iomega-p.pdf
iomega-p.pdf PDF 1653x2337 1653x2337+0+0 16-bit Bilevel DirectClass 484KB 0.150u 0:00.040
jlinkels@donald-pc:/tmp$ gs -sPAPERSIZE=a4 -sDEVICE=png16m -r200x200 -o iomega-p.png iomega-p.pdf
GPL Ghostscript 8.71 (2010-02-10)
Copyright (C) 2010 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 1.
Page 1
jlinkels@donald-pc:/tmp$ identify iomega-p.png
iomega-p.png PNG 4592x6492 4592x6492+0+0 8-bit DirectClass 6.196MB 1.690u 0:01.690

jlinkels
 
Old 05-02-2014, 06:23 PM   #6
Spect73
Member
 
Registered: Aug 2013
Distribution: Slackware 14.1
Posts: 128

Rep: Reputation: Disabled
Using -r200x200 is the same as -r200. Try something like -r200x100.
 
Old 05-02-2014, 07:41 PM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Original Poster
Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
I tried -r72. Then the resolution is 200 dpi.

jlinkels
 
Old 05-07-2014, 07:25 AM   #8
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi

I think your confusion is about resolution in the PDF. A PDF doesn't not have any resolution by itself. It has page size in points (1/72 of an inch). But inside it, pictures can have any resolution and it can contain text and vector graphics. Inside the PDF, bitmaps pictures are stored with pixels mostly as they are (some formats need re-encoding), and one picture can be used many times on a page with different resolution. The postscript basically says "the bitmap picture with id X" should be placed in "specified rectangle".

So when you specify r200 you get a upscaled PNG. Ghostscript makes a bitmap image the size of the source, but instead of 72 DPI it will be 200 DPI. So it will be a bigger picture (4592x6492). Then it renders the PDF on that image and saves it as PNG. Because the picture really is smaller, it has to scale it.

When converting from PDF to PNG, it's usually better to use ImageMagick's convert directly. But "convert a.pdf a.png" can look ugly. To make it clearer add -density and some number higher than 72. The -density parameter doesn't affect the size of the resulting PNG, but matter how it's rendered in memory. For example, if you use -density 288, it will make a big bitmap in memory, render the PDF onto that, and then scale it down, an effect a bit like antializing.

Another way is to use pdfimages from poppler-utils. Then you can extract the bitmap pictures as they are inside the PDF, no quality loss at all.

Last edited by Guttorm; 05-07-2014 at 07:27 AM.
 
1 members found this post helpful.
Old 05-07-2014, 06:13 PM   #9
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Original Poster
Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Ok, let me see if I understand this.

Suppose I scan an image with 200dpi. The resulting image is 1600x2400 pixels. I use convert to convert it to PDF. Now there is a 1600x2400 image on the PDF. This image is A4 size so it fills the complete page in the PDF.

Now I use GS to convert the PDF to a raster image. GS assumes the PDF is 72dpi. So when I request r200, it upscales everything in the PDF with 200/72 = 2.77. Hence the resulting image is (1600x2400) x 2.7 = 4400 x 6600.

OTOH, when I request r72, GS doesn't upscale and gives me back whatever happened to be in the PDF, which is 1600x2400. That the image itself happens to be 200dpi on an 8" wide page is opaque to GS.

That is exactly what happens.

Now in case the PDF was created by printing an arbitrary document containing mostly text.

Again GS assumes this is 72dpi. So when I request a conversion from pdf with r72, GS produces a page which is actually 8" and characters are rasterized at 72dpi. Which looks ugly.

I order to have an acceptable rendered picture I should use r200 to tell GS to rasterize the characters at 200dpi.

Is that all correct?

I did do the conversion to bitmap at first with convert using -density, but then I modified it to use GS. I am not sure why, maybe it was because it was said that GS is the recommended way for the conversion, maybe because I found that GS was faster. The results in terms of functionality and resolution are identical.

I read about poppler as well. However my program is in TCL and no API is available for TCL. I didn't really want to start a project on PDF conversion. The application is some kind of document management system. I just want to view what is inside the PDF with reasonable quality.

jlinkels
 
Old 05-08-2014, 02:27 AM   #10
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi

You want the PNG to have a size corresponding to a screen size no? 1600x2400 is a bit strange. Anyway, this should make a decent result:

convert -geometry 1600x2400 -density 288 file.pdf file.png

This will scale it to 1600x2400 no matter what the source is. (Actually it will be 1600px wide or 2400 pixels high depending on the ascpect ratio).

The density will not affect the resulting size at all. Imagemagick will make a huge picture in memory, ghostscript will render the PDF on it, and then scale it down. Maybe it's not neccessary to use such a high value. It will use a lot of memory and be slow. The effect is the same as if you use r200 with ghoscript and scale it down after.

Another thing it can be better to use a multiply of 72 for density, for example 288. The downscaling will be easier and can be better if for example 8 pixels corresponds to 1 pixel in the result.
 
Old 05-08-2014, 08:05 PM   #11
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Original Poster
Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Quote:
Originally Posted by Guttorm View Post
You want the PNG to have a size corresponding to a screen size no? 1600x2400 is a bit strange. Anyway, this should make a decent result:
It is what I get when I scan an 8" page with 200 dpi. It doesn't have anything to do with screen size. I think 200dpi is a reasonable quality. 150 dpi is too low for my taste, 300dpi doesn't bring a significant improvement in picture quality. For my purpose that is.

Quote:
Originally Posted by Guttorm View Post
The density will not affect the resulting size at all. Imagemagick will make a huge picture in memory, ghostscript will render the PDF on it, and then scale it down. Maybe it's not neccessary to use such a high value. It will use a lot of memory and be slow. The effect is the same as if you use r200 with ghoscript and scale it down after.
What you say here is not very clear. When I render the pdf with density=72 or density=200 makes a huge difference in file size.
Code:
jlinkels@donald-pc:/tmp$ convert -density 200 sgb2.pdf sgb2-200.png
jlinkels@donald-pc:/tmp$ convert -density 72 sgb2.pdf sgb2-72.png
jlinkels@donald-pc:/tmp$ ll -h sgb2*png
-rw-r--r-- 1 jlinkels jlinkels 8.1M May  9 00:49 sgb2-200.png
-rw-r--r-- 1 jlinkels jlinkels 5.9M May  9 00:48 sgb2-72.png
Now this doesn't say a thing. Because there is no information added, the 72dpi png and the 200dpi png compress to roughly the same size. (Compressing is removing redundancy. The 200 dpi file contains a lot of redundancy because the information is the same as in the 72 dpi file)

Code:
jlinkels@donald-pc:/tmp$ convert -density 200 sgb2.pdf sgb2-200.ppm
jlinkels@donald-pc:/tmp$ convert -density 72 sgb2.pdf sgb2-72.ppm
jlinkels@donald-pc:/tmp$ ll -h sgb2*p*
-rw-r--r-- 1 jlinkels jlinkels 171M May  9 00:49 sgb2-200.ppm
-rw-r--r-- 1 jlinkels jlinkels  23M May  9 00:49 sgb2-72.ppm
However when I convert to uncompressed ppm the file size is a whole lot more.

Anyway, thank you for shedding the light to my problem. I have changed the application's code on converting the PDFs. Work is lot faster now.

jlinkels
 
Old 05-09-2014, 06:23 AM   #12
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi

-rw-r--r-- 1 jlinkels jlinkels 8.1M May 9 00:49 sgb2-200.png
-rw-r--r-- 1 jlinkels jlinkels 5.9M May 9 00:48 sgb2-72.png

The reason you get different file size and picture size is that you don't specify -geometry. If you specify geometry the picture size will be the same and the file size will differ slightly. If you compare the two files the 200 file should be sharper and the text more readable.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
slow performance lin_apps Linux - Newbie 3 10-02-2009 07:07 PM
Performance of CentOS 5.1 is slow...very slow! 90% mem used!! Why? Jazzmaster777 Linux - Server 5 04-13-2008 05:48 AM
Really Slow Performance in 12.0 ppr:kut Slackware 15 07-30-2007 08:30 AM
Slow performance!!! System using a lot of Physical Memory SLOW HELP! badgerbox76 Linux - Newbie 31 07-19-2007 10:23 AM
slow to boot, slow dns resolution after host name change FC5 edavis6678 Linux - Newbie 3 01-03-2007 06:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration