PDA

View Full Version : Qt Printing Good Image Quality.



cydside
10th August 2013, 15:16
Hi to all,
I'm having trouble printing an image inseted into a QTextDocument. The image has a 1900 dpi and the preview(Screenshot-1.png) looks good but printing it the resulting quality is poor(Screenshot-2.png).
I'm attaching the project to allow you a test and give me a feedback.
Thanks in advance, Danilo.

Lykurg
10th August 2013, 18:44
Ehm, the image has a width of 387 pixel. Assuming 1900dpi this would result in about 5mm! Your code seems to be all right, but the image quality is too poor.

cydside
10th August 2013, 20:05
The image is created by Inkscape, I scaled according to this formula DesignWidth = WantedWidth * 90 / WantedDpi.
Any hints?

wysota
10th August 2013, 21:58
Why *90?

If you want an image that is 1 inch wide and you want it to be 1900dpi then it has to be 1900 pixels wide. 387 pixels and 1900 dpi i 0.2'' which is about 4.9mm.


BTW. Have you checked the actual resolution that the printer is set into when using QPrinter::HighResolution?

cydside
11th August 2013, 09:10
Why *90?

I've found this answer on an Inkscape forum:


Your drawing in Inkscape must be 551,4x832,5 px so when you export it at 300dpi the size of the .png file will be 1838x2775px.
the formula I used was a=1838*90/300=551,4

so I scaled my drawing to 4.9mm in order to export an image of 387 pixels per 1900dpi. It has been an attempt to have a better result in printing. Before that I used to create my drawing with real size or rather as in that case a 387 pixels drawing at 90dpi. It's frustrating that the old software I'd like to replace has a better printing than I can do with Qt.


BTW. Have you checked the actual resolution that the printer is set into when using QPrinter::HighResolution?

Do you mean override the resolution with QPrinter::setResolution( int dpi )?

wysota
11th August 2013, 09:30
I mean to print a line of known length and use a ruler to calculate its resolution.

Lykurg
11th August 2013, 10:15
So if you use Inkscape, then the easiest (= with no calculation of your own) would be to work with mm units while painting. In the export dialog you can then simply say what dpi you would like to have. By the way, scaling a svg only to export it perverts the whole purpose of vector drawing. Also please do not state that Qt has bad printing support, when it seems that you haven't understand the way Qt handle things.

And about the 90: I guess that has to do with the screen resolution in combination with wysota's approach. (It is always an advantage to understand snippets something before c&p.)

cydside
11th August 2013, 10:56
I mean to print a line of known length and use a ruler to calculate its resolution.

So, I printed a line of 200 pixels using Paint.exe and, on the paper, his length was about 5,60 cm.
The formula I used to calculate DPI is:

DPI = (Pixels * 2,54) / Length expressed in cm

resulted of 91dpi.

Using a
QPrinter printer(QPrinter::HighResolution); then I got the printer.resolution(); of 600 an the resulting length of the same line was of 5,3 cm about 95dpi.

Sorry but I don't get it!

wysota
11th August 2013, 11:37
It means your printer prints with 90 dpi and not 1900dpi. Did you set it to 1900dpi? Did you use QPrintDialog for that?

cydside
11th August 2013, 12:45
It means your printer prints with 90 dpi and not 1900dpi. Did you set it to 1900dpi? Did you use QPrintDialog for that?

I didn't anything but use the project as it is.
Here is the project with the different dpi images I tried an the SVG file too. I got the same poor result!
9401

wysota
11th August 2013, 19:57
The problem is not with the image but rather with your printer setup. Use QPrintDialog and set the printer resolution correctly. Your printer is set to 90 dpi so it will print in 90 dpi unless you tell it to print in higher resolution.

ChrisW67
12th August 2013, 06:55
Why *90?
The magic number comes from the unit used in Inkscape SVG file coordinates: 1/90 inches (90 DPI). In the program these are the "px" unit. I don't know why they chose that unit rather than something more common like points, mm, or inches.
Not saying it is relevant here, just where it came from.


So, I printed a line of 200 pixels using Paint.exe and, on the paper, his length was about 5,60 cm.
...
Sorry but I don't get it!
Microsoft Paint has a notional pixel size of 1/90". You draw a line 200 pixels long and the program treats it as 200 * 1/90 = 2.22" (56.4mm).
When Paint comes to print it it looks at the reported printer resolution and draws a number of printer pixels to give a line of 2.22". If the printer reports 300DPI (many lasers) then 667 printer pixels make up the line. If the printer reports 600DPI then it needs to draw 1333 printer pixels to span the 2.22" line on paper. (The 1/90" thickness of the line is 3 or 7 printer pixels respectively) The actual length of the line on paper should be close to invariant, but that depends on the invisible internals of Paint.

When you take a raster image like the one in your first post scale it up to fit page width (essentially what you are doing with QTextDocument) each individual pixel in the original image is scaled up. You gave Qt 337 image pixels to stretch across the printer page. For a 200mm wide page each pixel gets scaled up to around 0.6x0.6mm. That is the finest detail that can be shown and leads to a jaggy appearance. If the printer reports 300DPI then the page has 2362 printer pixels across and each source image pixel is grown to be a block on the printed page of 7x7 pixels.

To get "print quality" a raster image should be generated to have 300 pixels (or more) for every inch of physical size you want it to occupy on the printed page. For 200mm wide results the raster should be 200 / 25.4 * 300 = 2362 pixels across (approx). This can mean lots of large image files in a document.

The limited scalability of raster images is part of the reason for the existence of vector formats in the first place. Can you show how you were using a an SVG that was giving poor results?

cydside
12th August 2013, 09:44
...
The limited scalability of raster images is part of the reason for the existence of vector formats in the first place. Can you show how you were using a an SVG that was giving poor results?

First of all, thanks for you explanation. The last attempt I've done was to use directly the svg (400 x 228 pixels) file in my QTextDocument as showed in the example project attached here.
At the same time a dropped that svg file in firefox and printed it. The last was better! Try it!

wysota
12th August 2013, 15:52
How did you create that SVG file? Did you convert it from PNG?

cydside
12th August 2013, 21:26
How did you create that SVG file? Did you convert it from PNG?

I created the SVG file using Inkscape. Just made a simple orange object and some text objects with Arial font. Then I turned the text objects into paths in order to avoid font's problem so one might visualize the drawing as pure vectorial group of objects. That's it. I often use Inkscape to make simple icons or logos.
The PNG file is generated by exporting that drawing from Inkscape as well.