ImageMagick convert adds whitespace when converting PDF to PNG - pdf

I'm using ImageMagick to convert the following PDF to an PNG file.
Click here to download the PDF from IMSLP (Permalink if the direct download is broken)
In a PDF viewer it looks nice:
but when converting with convert -density 300 -background white -alpha off -alpha remove file.pdf /tmp/file.png
the image gets a large white margin:
I do not want to trim the image afterwards, I just want ImageMagick to somehow respect the view-port or however that viewing information is being encoded in the PDF. Does anyone know which command-line parameter might enable this behavior?
Edit 10.03.2022: I'm using ImageMagick 7.1.0.16 with Ghostscript 9.55.0 inside an Alpine Linux docker image.

I do not get your extra margin in ImageMagick 6.9.12-42 using Ghostscript 9.54. But changing the density does not seem to have any effect.
convert -density 300 -background white file.pdf[1] x2.png
The issues may be a malformed PDF. How was it created? Also what version of Ghostscript are you using? It could be a GS version issue.
If this was a scanned PDF that is a raster image in a vector PDF shell, then you could just use pdfimages to extract the raster files. See https://manpages.debian.org/testing/poppler-utils/pdfimages.1.en.html

The hint from KenS was exactly what I was looking for - the PDF defines a CropBox that ImageMagick 7.1.0 was not using by default. The solution therefore is to modify the command to include the following -define information:
convert -define pdf:use-cropbox=true file.pdf /tmp/file.png
Thank you all for your help!

Related

ImageMagick convert pdf with multiple pages to high quality PNG

I am trying to convert a multi-page PDF to one long png with the following command:
convert -append -flatten -density 300 in.pdf out.png
I am using -flatten to lose transparency, since I want a white background in the final PNG. The problem is that it takes only the first page instead of using all the pages.
How can I convert the PDF to one long PNG while losing the transparency and using a white background?
This command works for me on IM 6.9.9.22 Q16 Mac OSX with Ghostscript 9.21
convert -density 300 -colorspace sRGB itc101_13.pdf -alpha off -append out.png
If it does not work for you, then what is your ImageMagick version and what is your Ghostscript version.
You have your syntax wrong. You must read the PDF before applying append. Try
convert -density 300 -colorspace sRGB in.pdf +adjoin -append -background white -flatten out.png
If that does not work, then what is your ImageMagick version and platform? What is your Ghostscript version and your libpng version? Can post a link to your PDF file?
Note that +adjoin is not usually necessary for output to PNG, but won't hurt.

ImageMagick convert produces a darker CMYK PDF than PhotoShop

The ImageMagick (IM) result of this command
convert myRGB.png -colorspace cmyk cmyk.pdf <br>
is not as bright or as close to the screen colors as a Photoshop produced CMYK PDF. myRGB.png is a PNG file produced using GIMP.
I don't own Photoshop, and would like to stick with open source tools.
The current Ubuntu release of of IM is 6.7.7. That IM version produces very dark, totally unusable, CMYK PDF.
I built 7.0.2-6 Q16 from source on Ubuntu 14.0.4, after also building LCMS package from source, and the above command works better, but the CMYK PDF as stated above is less bright and less close to the screen colors than the similar Photoshop output. E.g. blacks are not totally black; the sky color is dull blue instead of bright blue/cyan.
I've tried using ICC files downloaded from Adobe as in the following
convert myRGB.png -colorspace cmyk -profile WebCoatedSWOP2006Grade5.icc cmyk.pdf
I've tried this command with all 14 Adobe ICC files and there is no difference in any of them. Although, I admit I do not understand under what circumstances the ICC comes into play or if it is appropriate to this problem at all.
The simple question is why does IM convert tool not match the Photoshop results for CMYK?
The second question is, if IM can't be made to do it: is there any open source tool or tools that can match the Photoshop results for producing a CMYK PDF from and RGB PNG?
There are two applications involved, as you presumably know since you tagged this with Ghostscript. You haven't said which version of Ghostscript you have installed but the first thing I would do is remove ImageMagick from the equation.
Find out whether IM is having Ghostscript produce RGB or CMYK output, my bet is that it is getting RGB from GS. You'll need to find out what Ghostscript command line IM is using and I can't tell you how to do that. Assuming that the Ghostscript output is RGB then this would explain why altering the IM settings makes no difference.
Proceeding on the assumption that the above is correct, use the png16m device in Ghostscript to produce RGB PNG files directly, this reduces the scope of the problem:
gs -sDEVICE=png16m -o out.png input.pdf
Now, you don't say what version of Ghostscript you have installed, but assuming its relatively recent you can look in the /ghostpdl/doc directory and find considerable information on using colour management in Ghostscript, the document GS9_Color_Management.pdf may be helpful. It will certainly give you a myriad of opportunities to alter the output.

Convert multipage PDF to PNG with transparency

in the moment i run into several problems by converting a PDF file to PNG.
The transparence is lost from the source pdf file.
I have tested the following terminal tools to create the png:
GhostScript, Imagemagick and pdf tools from poppler-tools, always on a debian system.
The image should have the same dimension as the pdf, also the same transparency.
used commands:
gs -dNOPAUSE -sDEVICE=pngalpha -sOutputFile=test%d.png -r96 -q design.pdf -c quit
convert design.pdf test%d.png
convert design.pdf -channel rgba -alpha on PNG32:test%d.png
convert -background none -colorspace srgb design.pdf -colorspace srgb -channel rgba -alpha on PNG32:test%d.png
pdftoppm -png file.pdf test
The result is not the expected png with transparency. The Background is white, should be 100% transparent. Additionaly there is a green bar and should be semi-transparent. In all my tries the result ends up in a lighter green box with no transparency.
To see my result, i have uploaded the source pdf, faulty created png and the expected result (export from photoshop).
PDF: http://speedy.sh/W75HP/source-file.pdf
Result: http://speedy.sh/hfZMt/faulty-created-design.png
Expected: http://speedy.sh/7mpEk/design-the-way-it-should-be.png
I managed to get the white background to be transparent, but the actual file transparency including the semi-transparent green bar/box is not converted properly.
Whats the solution for my issue?
Best regards,
Chris
//UPDATE
Okay we have found a solution with another 3th party tool which produces my expected result on a easy way.
inkscape design.pdf -z --export-dpi=100 --export-png=design.png
Thx for help
Using Imagick (PHP Extension) I converted a background color into transparent with some code like this (I converted a JPG with white background into transparent PNG):
$mask = new Imagick('/your/file/path.jpg');
$mask->setImageFormat('png');
$mask->paintTransparentImage('white', 0, 1000); // $fuzz = 1000 (3rd parameter) is just a guess
Take a look here:
http://de3.php.net/manual/en/imagick.painttransparentimage.php
Corresponding Imagick documentation:
http://www.imagemagick.org/script/command-line-options.php#transparent
Regards,
Michael

Converting from pdf to png with ghostscript, result with many white boxes

I'm converting pdf (created with adobe illustrator) into transparent png file, with following command:
gs -q -sDEVICE=pngalpha -r300 -o target.png -f source.pdf
However, there's undesired white boxes in the resulting PNG, looks like it's auto generated by ghostscript, some bounding box. (see attached image)
Tryied both gs-9.05 and gs-9.10, same bad result.
I've tried to export to PNG file from Illustrator or Inkscape manually, the result is good.
What does Inkscape do to render it correct, and
How could I eliminate those white boxes using ghostscript?
Try mudraw of latest (1.3) muPDF, as far as I checked it creates nice PNGs from PDF files with 1.4 transparency:
mudraw -o out.png -c rgba in.pdf
"rgba" being, as you understand, RGB + alpha
In the general case, you can't. PDF does support transparency, but the underlying media is always assumed to be white and opaque. So anywhere that marks are made on the medium is no longer transparent, its white.
You don't say which version of Ghostscript you are using, but if its earlier than 9.10 you could try upgrading.

ImageMagick PDF to JPEG conversion results in green square where image should be

I'm attempting to convert a PDF to a JPEG using ImageMagick.
The PDF:
baby_aRCWTU.pdf
The command:
convert -density 260 -profile 'SWOP.icc' -profile 'sRGB.icm' 'baby_aRCWTU.pdf' 'baby_aRCWTU.jpg'
The resulting JPEG:
baby_aRCWTU.jpg
As you can see, the text is rendered nicely, but the embedded image shows up as a green square. Any ideas? This occurs with and without the colour profiles.
edit: reposted due to broken links
On a site we convert hundreds of PDF's on a daily basis where we need to create JPGs and we found it only reliable to convert the PDF's to postscript first.
We use the "pdftops" command, try
pdftops baby_aRCWTU.pdf baby_aRCWTU.ps
then your convert command above, but on the ps. Works for me, the image is then included.