I use Imagemagick convert to convert pdf file to png as follows:
Magick convert -density 300 PointOnLine.pdf -quality 90 PointOnLine.png
It gives me the following warning:
convert: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG `PointOnLine.png' # warning/png.c/MagickPNGWarningHandler/1744.
And png image created is all black. However, convert to jpg image is fine.
Update: After adding -define profile:skip=ICC, image is still dark. But if convert to jpg and then to png, it is ok, but background is dark. The same warning is still there. What is the problem? Thanks.
The following works for me without error in ImageMagick 7.0.7.22 Q16 Mac OSX Sierra with Ghostscript 9.21 and libpng #1.6.34_0. Your PDF has an alpha channel, so you might want to flatten it.
magick -density 300 PointOnLine.pdf -flatten -quality 90 result.png
This also works without error, but leaves the alpha channel in the png, though you won't see it here until you extract the image:
magick -density 300 PointOnLine.pdf -quality 90 result2.png
Note that in IM 7 you should just use magick and not magick convert.
Check that you are using a current version of Ghostscript and libpng, if you do not get the same results.
Your delegates.xml file for PS:alpha should show sDEVICE=pngalpha rather than pnmraw as follows.
<delegate decode="ps:alpha" stealth="True" command=""gs" -sstdout=%%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s""/>
USER REQUESTED RESULTING IMAGES THAT I POSTED TO BE REMOVED!
Command which worked for me was:
magick -density 300 PointOnLine.pdf -depth 8 -strip -background white -alpha off PointOnLine.tiff
It did not gave any warning, also removed black blackground as well.
I was able to convert it to the text afterwards using tesseract:
tesseract PointOnLine.tiff PointOnLine
I understand you are using ImageMagick under Windows, even if not stated (and the respective versions of IM, Win were not posted)
I am under Ubuntu 16.04 LTS, and I will provide an answer possibly useful. (Under Win, prepend everything with Magick).
For me,
convert -density 300 -quality 90 PointOnLine.pdf PointOnLine.png
works fine, with no warnings, producing a suitable output.
I tried other things which work as well, some of them may suit you.
First convert your pdf to RGB and then to png.
convert -density 300 -colorspace RGB PointOnLine.pdf PointOnLine_rgb.pdf
convert -density 300 PointOnLine_rgb.pdf PointOnLine_rgb.png
If you post your PDF, I can check it out. Otherwise, perhaps it is CMYK, which PNG does not support. So try
magick -quiet -density 300 -colorspace srgb PointOnLine.pdf -quality 90 PointOnLine.png
Note in IM 7, use magick not magick convert. Also not that -quality is different for PNG than JPG. See https://www.imagemagick.org/script/command-line-options.php#quality
I had the same issue and resolved adding -colorspace RGB before the output filename.
convert -density 300 PointOnLine.pdf -quality 90 -colorspace RGB PointOnLine.png
Related
I am trying to convert .pdf files to .jpg using image-magic
convert -limit -limit map 300 -flatten -density 300 -quality 100 -crop '400x400+20+20' dummy.pdf[0] test.jpg
but the problem i am facing is when i convert the file, it cropping the area but marking all the other area as white.
for example if i am converting a pdf with 1000x1000 size and cropping it to a 100x100 size, the output am getting is an image with 1000x1000 size with 100x100 area croped from the pdf and rest is white space.
sample.pdf
i cannot use trim, since my pdf may or may not have white border and trim will remove it
Your syntax is not in the proper order for Imagemagick. Most of the settings and operators need to come after reading the input PDF. Using Imagemagick 6.9.10.71 Q16 Mac OSX Sierra:
convert -limit map 300 -density 300 dummy.pdf[0] -background white -flatten -crop '400x400+20+20' -quality 100 test.jpg
To convert pdfs/eps to pixel images we use ImageMagick. When converting a PDF the command can look like this:
convert -verbose -density 150 -trim -colorspace sRGB input.pdf -quality 90 -flatten -sharpen 0x1.0 output.png
However, the input PDF (which contains only a few paths) has a specified size of 300cm by 200cm, and Ghostscript doesn't like this, and creates a huge png. The verbose output of imagemagick shows this:
"gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r150x150" "-sOutputFile=/tmp/magick-3036AW7mUOP25w7J%d" "-f/tmp/magick-3036PxgJinljqMwV" "-f/tmp/magick-30369hcErAROr7V6"
/tmp/magick-3036AW7mUOP25w7J1 PNG 17717x11811 17717x11811+0+0 8-bit sRGB 1.003MB 3.910u 0:03.929
input.pdf PNG 17717x11811 17717x11811+0+0 16-bit sRGB 1.003MB 0.000u 0:00.000
PNG 17717x11811. Huge. I only need a 256x256 image.
I have tried the geometry (-geometry 256x256) options and the density option (before the filename, -density 150) in different configurations, but I does not change the ghostscript output, for example:
convert -verbose -density 150 -trim -geometry 265x265 -colorspace sRGB input.pdf -quality 90 output.png
"gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r150x150" "-sOutputFile=/tmp/magick-3233p3ofct0fiy5T%d" "-f/tmp/magick-3233Rads_vSSpKa6" "-f/tmp/magick-3233LQMrrEFgT0fi"
/tmp/magick-3233p3ofct0fiy5T1 PNG 17717x11811 17717x11811+0+0 8-bit sRGB 1.003MB 3.800u 0:03.799
input.pdf PNG 17717x11811 17717x11811+0+0 16-bit sRGB 1.003MB 0.000u 0:00.000
input.pdf=>output.png PNG 17717x11811=>265x38 321x213+28+65 16-bit sRGB 7.96KB 1.080u 0:00.559
How can you define the constraints for Ghostscript, when using ImageMagick?
I wouldn't say 'Ghostscript doesn't like this', if the media size is huge, then of course Ghostscript creates a huge PNG, what else did you expect it to do ?
300 cm is 118.11 inches, which at 150 dpi works out at a bitmap 17716.5, or rounded up to 17717 pixels.
200 cm is 78.74 inches, at 150 dpi that works out to 11811 pixels.
If you want it at a lower resolution, then alter the setting of -r ('density' in ImageMagick). For example, you could set 'density 10', presumably that would produce a file 1181x787. To get 256x256 you would need to set the resolution to ~2 dpi. You may, of course, find that its rather hard to see any detail when the result is that coarse.
Alternatively you can tell Ghostscript the size of media you want, and tell it 'FIXEDMEDIA' so it doesn't alter according to requests from the PostScript program or PDF file.
-g sets the media dimensions in pixels, and -dFIXEDMEDIA tells Ghostscript that the media is fixed. You will almost certainly want to also set -dFitPage, or you will only get a tiny portion of the bottom left hand corner. You will also need to not set -r.
-dFIXEDMEDIA -g and -r are described in use.htm in the ghostscript documentation
Depending on the age of your Ghostscript installation you may not be able to use -dFitPage but you might have to use -dPDFFitPage.
Almost certainly you will want to do this from the command line using Ghostscript instead of ImageMagick, I imagine constructing the command line in IM would be difficult.
I've various PDF files from differents origins.. charged from users in a website..
I want reduce size of those more than 700Ko
So, I've make a script for select all files bigger than 700Ko;
For each PDF ( because there's images too ) I make :
exec('convert -density 300 -trim "'.$file.'" -resize 1000 -quality 85 -colorspace RGB -background white "'.$destination.$rand.$save_to.'" &', $output, $return_var);
The & at the end is for make : page-1.png, page-2.png ...
All is OK at this nivel ( I use .png because with .jpg I've some black background.. :( )
After, I try to make a new PDF with this:
exec('convert '.$img.' +page -quality 100 -gravity center '.$dirname.'/page1.pdf');
where $img is a list of image .png
Here, I've a problem .. most of image are not centered in the page..
How to make each page with a 100% size with the image, without border??
So, I've find a solution for my problem ..
I use Ghostscript .. better, faster and without problem.;
my code:
exec('gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dBATCH -sOutputFile='.$rand.' '.$file.' ',$output, $return_var);
All PDF are smaller and don't lose quality !
More informations : http://milan.kupcevic.net/ghostscript-ps-pdf/
I want to shift from imagemagick to graphicsmagick
But I encounter some issues with the syntax
With imagemagick
First I need to merge some images into a PDF
convert -density 300 page_*.tif output.pdf
And then I need to create a thumbnail of the first page of the PDF
convert -density 300 file.pdf[0] -background white -alpha remove -resize 140x140 -strip -quality 40 thumb.jpg[0]
This works fine.. But I want to switch the first command to graphicsmagick
Width graphicsmagick/imagemagick
The graphicsmagick syntax here works fine
gm convert -density 300 page_*.tif output.pdf
But when creating the thumbnail with imagemagick the output has the right size but the acutal image is downsized inside the image itself?!
Thumbnail with imagemagick
https://secure.dyndev.dk/data/voucher/30000/400/30435_eb7e5d0a9df71b2783e2fa89efd9de12fcdb9679.pdf
Thumbnail with graphicsmagick
https://secure.dyndev.dk/data/voucher/30000/400/30433_7710d6404534b0868ab8da41dd651e971b70e16b.pdf
Just hit the same issue, and found a solution here:
https://blog.josephscott.org/2009/11/16/imagemagick-convert-pdf-to-jpg-partial-image-size-problem/
You need to change your convert command into:
convert -density 300 -define "pdf:use-cropbox=true" file.pdf[0] -background white -alpha remove -resize 140x140 -strip -quality 40 thumb.jpg[0]
And perhaps add a -resize "2000x2000>" to limit the size of the resulting JPEG, especially with high density values.
I have the following:
ghostscript-fonts-5.50-24
ImageMagick-6.7.2-1
ghostscript-9.02-1
Which I use to create a series of JPGs for each page using:
convert -density 175 -colorspace sRGB test.pdf -resize 50% -quality 95 test.jpg
When I run this on my windows machine all appears to work ok, but on our linux server we get the black background problem.
The resulting JPGs have a black background rendering the image un-readable, what am I missing or is there something I should be doing to correct this?
I've been all over google for days but each suggestion doesnt seem to work for me.
Any help is much appreciated, thanks in advance :)
EDIT
Just noticed this output when converting one of the PDFs that produces the black background:
**** Warning: Fonts with Subtype = /TrueType should be embedded.
The following fonts were not embedded:
Arial
Arial,Bold
Arial,BoldItalic
**** This file had errors that were repaired or ignored.
**** The file was produced by:
**** >>>> Microsoft« Word 2010 <<<<
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe's published PDF
**** specification.
This seems related but as we don't have control over how the PDFs are produced we need some way of fixing this server side.
Thanks again
Ran into this one today, found this:
https://www.imagemagick.org/discourse-server/viewtopic.php?t=20234
Based on that, these should all work:
-flatten
-alpha flatten
-alpha remove
I'm currently using the below for my specific case which works great:
convert -thumbnail "1280x800>" -density 300 -background white -alpha remove in.pdf out.jpg
Simple fix to this issue is to use an image format that supports transparency, such as png.
So:
convert -density 175 -colorspace sRGB test.pdf -resize 50% -quality 95 test.png
Problem solved :)
If you want a high quality result, use this command:
convert -density 700 input.pdf -resize 25% -append -quality 98 -alpha remove output.jpg
For windows users, use magick instead of convert