0

I regularly get tree-drilling-data out of a machine that should get into reports. The pdf-s contain too much empty space and useless information.

With convert i already managed to convert the pdf to png, cut out parts and rebuild an image i desire. It has a fine sharpness, its just too large: Output 1: Nice, just too large For my reports i need it in 45% size of that, or 660 pixels wide. The best output i managed up to now is this: Output 2: Perfect size but unsharp

Now, this is far away in quality from the picture before shrinking. For sure, i've read this article here, that already helped. But i think it must be possible to get an image as fine as the too large one in Output 1.

I've tried around for hours with convert -scale, -resize, -resample, playing around with values for density, sharpen, unsharpen, quality... nothing better than what i've got, using

convert -density 140 -trim input.pdf -quality 100 -sharpen 0x1.0 step1.png

then processing it to the new picture (output1, see up), that i'm putting to the correct size with

convert output1.png -resize 668x289! -unsharp 0x0.75+0.75+0.01 output2.png

I tried also "resize 668x" in order not to maybe disturb, no difference.

I find i am helpless in the end. I am not an IT-expert, i am a computer-affin tree-consultant. My understanding of image-processing is limited. Maybe it would make sense to stay on a vector-based format (i tried .gif and .svg ... brrrr).

I would prefer to stay with convert/imagemagick and not to install additional software.

It has to run from command-line, as it is part of a bash-script processing multiple files. I am using Suse Linux.

Grateful for your help!

treebee
  • 3
  • 5
  • @K J I need 6.5 inches, but i am, for different reasons, not at all a friend of online-solutions. On my trying you might see that i am not really in image-processing... – treebee Feb 04 '23 at 21:04
  • @K J excuse me, i was tired, thougt your links were upload-links;) the files look very fine, its only a bit confusing for me to translate your explanations into a syntax for e.g. the convert - command. Maybe i get a hint from linux-side? – treebee Feb 04 '23 at 22:58
  • @K J One thing already helps a lot: I reduced colours to 32 (16 was too few, the blue text-parts became pale) and [get an image with 95 KiB](https://hidrive.ionos.com/lnk/eJCgTHsU), which is for now absolutely satifying for me. – treebee Feb 05 '23 at 00:25
  • Even better: With `magick` - command i used `PNG8:`, get around 50 KiB - solution in 1 more Addition in my 2nd post below, with link to final, composed image. Thank you a lot! – treebee Feb 05 '23 at 01:04

3 Answers3

2

I realize you said no other software, but it can be easier to get good results from other PDF rendering engines.

ImageMagick renders PDFs by shelling out to ghostscript. This is terrific software, but it's designed for print rather than screen output. As a result, it generates very hard edges, because that's what you need if you are intending to control ink on paper. The tricks you see for rendering PDF at higher res and then resizing them fix this, but it can be tricky to get the parameters just right (as you know).

There are PDF rendering libraries which target screen output and will produce nice edges immediately. You don't need to render at high res and sample down, they just render correctly for screen in the first place. This makes them easier to use (obviously!) and a lot faster.

For example, vipsthumbnail comes with suse and includes a direct PDF rendering system. Install with:

zypper install vips-tools

Regarding the size, your 660 pixels across is too low. Some characters in your PDF will come out at only 3 or 4 pixels across and you simply can't make them sharp, there are just too few dots.

Instead, think about the size you want them printed on the paper, and the level of detail you need. The number of pixels across sets the detail, and the resolution controls the physical size of those dots when you print.

I would at least double that 668. Try:

vipsthumbnail P3_M002.pdf --size 1336 -o x.png

With your sample image I get:

enter image description here

Now when you print, you want those 1336 pixels to fill 17cm of paper. libvips lets you set resolution in pixels per millimetre, so you need 1336 pixels in 170 mm, or 1336 / 170, or 7.86. Try:

vips.exe copy x.png y.png[palette] --xres 7.86 --yres 7.86

Now y.png should load into librecalc at 17cm across and be nice and sharp when printed. The [palette] option after y.png enables palettised PNG, which shrinks the image to around 50kb.

The resolution setting is also called DPI (dots per inch). I find the name confusing myself -- you'll also see it called "pixels per printed inch", which I think is a much clearer.

jcupitt
  • 10,213
  • 2
  • 23
  • 39
  • Thank you, @jcupitt, vips looks nice and is quick. The thumbnail-option gives blurry image, but vips pngsave did work. It's not ready yet, but in the moment i am out of time... i'll be back. – treebee Feb 04 '23 at 15:08
  • It shouldn't be blurry. I added some sample output. – jcupitt Feb 05 '23 at 17:38
  • Yes, thats sharp enough, but far too large, it goes over more than 2 pages in horizontal width (in libre calc, there won´t be much difference in writer). And up to now, any shrinking produced blurryness. Also, it has some reddish spots, here a very little more than in your pic. 660px wide is not at all important, as long as it fits in 17cm. I found transpareny nice in this tryings, by the way. – treebee Feb 05 '23 at 21:53
  • The size the image is displayed at in librecalc etc. is set by the DPI, not by the size in pixels. I added some notes and updated the image, I would try again. – jcupitt Feb 06 '23 at 12:07
0

In Imagemagick, set a higher density, then trim, then resize, then unsharpened. The higher the density, the sharper your result, but the slower it will get. Note that PNG quality of 100 is not the proper scale. It does not have quality values corresponding to 0 to 100 as in JPG. See https://imagemagick.org/script/command-line-options.php#quality. I cannot tell you the "best" numbers to use as it is image dependent. You can use some other tool such as at https://imagemagick.org/Usage/formats/#png_non-im to optimize your PNG output.

So try,

convert -density 300 input.pdf -trim +repage -resize 668x289 -unsharp 0x0.75+0.75+0.01 output.png

Or remove the -unsharp if you find that it is not needed.

ADDITION

Here is what I get with

convert -density 1200 P3_M002.pdf -alpha off -resize 660x -brightness-contrast -35,35 P3_M002.png

enter image description here

I am not sure why the graph itself lost brightness and contrast. (I suspect it is due to an imbedded image for the graph). So I added -brightness-contrast to bring out the detail. But it made the background slightly gray. You can try reducing those values. You may not need it quite so strong.

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • Thanks for the input, but it gets more blurry than what i get up to now: [output from your code] (https://i.stack.imgur.com/wBLqk.png) Iḿ going to read your links... – treebee Feb 02 '23 at 21:19
  • Post your input PDF and let me test with it. – fmw42 Feb 02 '23 at 21:27
  • that link to [sample-pdf](https://hidrive.ionos.com/lnk/yFigz5Ya) should work. I guess, first resizing the .pdf, somehow just telling it to be another size, would be best method..? ... as up to now any resizing the .png goes with significant file-enlargement (3x) or quality-loss. – treebee Feb 04 '23 at 15:22
  • `@treebee` See my ADDITION in my answer above. – fmw42 Feb 04 '23 at 17:11
  • @ fmw42 thank you a lot! This is interesting and i will research more on it. The coming weeks my schedule is too full to do more... maybe i put a little more information later. – treebee Feb 04 '23 at 19:53
  • `@treebee` OK, but @JCupitt's solution sounds to be more direct and would give a better result. – fmw42 Feb 04 '23 at 20:00
  • The solution with pngcrush works best in sharpness and worst in filesize, for the moment i choose this. I added somthing to my 2nd post below. – treebee Feb 04 '23 at 21:44
  • Finished now, added `magick ... PNG8: ...` getting 50 KiB and enough sharpness. Theres 1 more additiion to my answer below with final solution. – treebee Feb 05 '23 at 01:06
0

Great, @fmw42,

pngcrush -res 213  graphc.png done.png

from your link did the job, as to be seen here:

perfect size and sharp graph

Thank you a lot.

Now i'll try to get file-size down, as the Original pdf has 95 KiB an d now i am on 350 KiB. So, with 10 or more graphs in a document it would be maybe unnecessary large, also working on the ducument might get slow.

-- Addition -- 2023-02-04

@fmw42 : Thanks for all your effort! Your solution with the .pdf you show does not really work - too gray for a good report, also not the required sharpness.

@jcupitt : Also thanks, vips is quick and looks interesting. vipsthumbnails' outcome ist unsharp, i tried around a bit but the docu is too abstract for me to get syntax-correct use. I could not find a dilettant-readable docu, maybe you know one?

General: With all my beginners-trials up to now i find:

  • the pdf contains all information to produce a large, absolutely sharp output (vector-typic, i guess)
  • it is no problem to convert to a png of same size without losing quality
  • any solutions of shrinking the png in size then result in significant (a) quality-loss or (b) file-size increase.

So, i (beginner) think that the pdf should be processed directly to the correct png-size, without later downsampling the png. This could be done (a) telling the conversion-process the output-size (if there is a possibility for this?) or (b) first creating a smaller pdf, like letting it look A5 instead of A4, so a fitting .png is directly created (i need 6.5 inches wide approx.).

For both solutions i miss ability to sensefully investigate, for it takes me hours and hours to try out things and learn about the mysteries of image-processing. The solution with pngcrush works for the moment, although i'm not really happy about the file-size (cpu and fan-power are not really important factors here).

--- Addition II --- final one 2023-02-05

convert -density 140 -trim "$datei" -sharpen 0x1.0 rgp-kopie0.png

magick rgp-kopie0.png +dither PNG8:rgp-kopie.png    ## less colours

## some convert -crop and -composite here to arrange new image

pngcrush -s -res 213 graphc.png "$namenr.png"

New image is as this, with around 50 KiB, definitely satisfying for me in quality and filesize.

I thank you all a lot for contributing, this makes my work easier from now on! ... and even if i do not completely understand everything, i learnt a bit.

treebee
  • 3
  • 5