PDF to PNG: Newbie victory and a question
PDF to PNG: Newbie victory and a question
This is my first post. For the experts reading, this will be deadly dull, and I invite you to skip to the last paragraph. Mainly, I'm writing for other newbies who have landed here after a Google search, and they're desperate for answers to the same problems that I had. It took two full days from the time I downloaded ImageMagick to the time I wrote this post, with much desk-pounding during those two days, but this story has a happy ending.
My problem: Convert a 300-dpi, sRGB-colorspace, PDF 1.3-format file into a 300-dpi PNG file with white background and no degradation of image.
BTW, the PDF file was created by using Scribus desk-top publishing software. Gotta love open-source.
First problem: When I tried to do the conversion, IM gave me error messages that mentioned "gs32c." I have gs32c on my computer because I "print" MS Word files to PDF using PDF995.
First solution: I downloaded and loaded 64-bit Ghostscript (gs64c).
Second problem: I still got the error message about 32-bit Ghostscript. ImageMagick didn't know that I now had the 64-bit version on my computer.
Second solution, part 1: I wrote a little BATCH file that I call in CMD.EXE, before I call "convert":
set PATH=C:\Program Files\gs\gs9.14\bin;%PATH%
Second solution, part 2: I edited DELEGATES.XML, globally replacing
& q u o t ; @PSDelegate@ & q u o t ;
with
& q u o t ; gswin64c & q u o t ;
Third problem: I couldn't save DELEGATES.XML after search-and-replace. The file didn't have its "System," "Hidden," or "Read-Only" attributes set, so how could it refuse to save? Windows insisted that the file was "in use." That was news to me.
Third solution: I shut down my computer. I started it up using the Windows Bootable Diagnostic CD (which takes a while to load, btw). Once I saw a menu, I chose "Command line" from the menu. After trying unsuccessfully to call "edlin" and "edit." I successfully tried "Notepad." Once Notepad had opened up, I loaded DELEGATES.XML into it.
(BTW number 1: When I'm working from my Windows Bootable Diagnostic CD, my C drive is now "D.")
(BTW number 2: When DELEGATES.XML loads in the Diagnostic CD version of Notepad, carriage-returns/linefeeds don't display. They're there, and this version of Notepad saves them afterward, they just don't display. All of DELEGATES.XML takes up only five very long lines in Diagnostic-CD Notepad.)
Anyway, I did the global search-and-replace within DELEGATES.XML that I described above, and saved (successfully). I restarted Windows normally.
For the first time, I could call
convert "big image.pdf" "big image.png"
and not get an error message.
Fourth problem: The background layer of the PDF came out transparent in the PNG file. Higher layers came out properly opaque, and this included parts of upper layers that were white. I tried using
-alpha off
but that not only made the transparent parts turn white, but it made the text look awful.
Fourth solution: I created an intermediary PNG file, then flattened it. The code:
convert -density 300 "big image.pdf" temp_png.png
convert temp_png.png -background white -flatten "big image.png"
MY QUESTION FOR THE EXPERTS: I wrote a BATch file that works, but it takes two lines of code, plus it takes time to write to, and read from, the big PNG file temp_png.png. How do I do what I want in one command, and without creating a temporary file? I can't figure out how to do stuff with parentheses. I have IM 6.8.9-Q16 and Windows 7 64-bit.
My problem: Convert a 300-dpi, sRGB-colorspace, PDF 1.3-format file into a 300-dpi PNG file with white background and no degradation of image.
BTW, the PDF file was created by using Scribus desk-top publishing software. Gotta love open-source.
First problem: When I tried to do the conversion, IM gave me error messages that mentioned "gs32c." I have gs32c on my computer because I "print" MS Word files to PDF using PDF995.
First solution: I downloaded and loaded 64-bit Ghostscript (gs64c).
Second problem: I still got the error message about 32-bit Ghostscript. ImageMagick didn't know that I now had the 64-bit version on my computer.
Second solution, part 1: I wrote a little BATCH file that I call in CMD.EXE, before I call "convert":
set PATH=C:\Program Files\gs\gs9.14\bin;%PATH%
Second solution, part 2: I edited DELEGATES.XML, globally replacing
& q u o t ; @PSDelegate@ & q u o t ;
with
& q u o t ; gswin64c & q u o t ;
Third problem: I couldn't save DELEGATES.XML after search-and-replace. The file didn't have its "System," "Hidden," or "Read-Only" attributes set, so how could it refuse to save? Windows insisted that the file was "in use." That was news to me.
Third solution: I shut down my computer. I started it up using the Windows Bootable Diagnostic CD (which takes a while to load, btw). Once I saw a menu, I chose "Command line" from the menu. After trying unsuccessfully to call "edlin" and "edit." I successfully tried "Notepad." Once Notepad had opened up, I loaded DELEGATES.XML into it.
(BTW number 1: When I'm working from my Windows Bootable Diagnostic CD, my C drive is now "D.")
(BTW number 2: When DELEGATES.XML loads in the Diagnostic CD version of Notepad, carriage-returns/linefeeds don't display. They're there, and this version of Notepad saves them afterward, they just don't display. All of DELEGATES.XML takes up only five very long lines in Diagnostic-CD Notepad.)
Anyway, I did the global search-and-replace within DELEGATES.XML that I described above, and saved (successfully). I restarted Windows normally.
For the first time, I could call
convert "big image.pdf" "big image.png"
and not get an error message.
Fourth problem: The background layer of the PDF came out transparent in the PNG file. Higher layers came out properly opaque, and this included parts of upper layers that were white. I tried using
-alpha off
but that not only made the transparent parts turn white, but it made the text look awful.
Fourth solution: I created an intermediary PNG file, then flattened it. The code:
convert -density 300 "big image.pdf" temp_png.png
convert temp_png.png -background white -flatten "big image.png"
MY QUESTION FOR THE EXPERTS: I wrote a BATch file that works, but it takes two lines of code, plus it takes time to write to, and read from, the big PNG file temp_png.png. How do I do what I want in one command, and without creating a temporary file? I can't figure out how to do stuff with parentheses. I have IM 6.8.9-Q16 and Windows 7 64-bit.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: PDF to PNG: Newbie victory and a question
try
or
Vector files such as pdf, need settings before reading the input pdf. Raster files need to read the input first then add the settings and operators.
-density and -background are settings but -flatten is an operator.
Code: Select all
convert -density 300 -background white "big image.pdf" result.png
Code: Select all
convert -density 300 "big image.pdf" -background white -flatten result.png
Vector files such as pdf, need settings before reading the input pdf. Raster files need to read the input first then add the settings and operators.
-density and -background are settings but -flatten is an operator.
Re: PDF to PNG: Newbie victory and a question
To fmw42--
The first code results in once again the PDF's background layer showing up as transparent in the PNG.
The second code results in a strange result. I normally view graphics using Graphic Workshop (from Alchemy Mindworks), and this second code results in a PNG file that has a normal thumbnail, and looks normal when viewed in "imdisplay," but which appears totally transparent when Graphic Workshop displays it. I've had Graphic Workshop since 2000, and I've never seen GWS behave this way except for a few ImageMagick-generated PNG files.
The first code results in once again the PDF's background layer showing up as transparent in the PNG.
The second code results in a strange result. I normally view graphics using Graphic Workshop (from Alchemy Mindworks), and this second code results in a PNG file that has a normal thumbnail, and looks normal when viewed in "imdisplay," but which appears totally transparent when Graphic Workshop displays it. I've had Graphic Workshop since 2000, and I've never seen GWS behave this way except for a few ImageMagick-generated PNG files.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: PDF to PNG: Newbie victory and a question
Post your input pdf to some free hosting service such as dropbox.com (public folder) and put the URL here. I would need to have your PDF, so I can check it and try the commands.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: PDF to PNG: Newbie victory and a question
IM sometimes generates a superfluous alpha channel, even when all pixels are opaque. It's worth trying "-alpha off" before writing the output file.
snibgo's IM pages: im.snibgo.com
Re: PDF to PNG: Newbie victory and a question
To snibgo: I tried your suggestion.
convert -density 300 "big image.pdf" -background white -flatten -alpha off result.png
^^^^ Results in "transparent" PNG file when viewed in Graphic Workshop
convert -density 300 "big image.pdf" -background white -alpha off -flatten result.png
^^^^ Text is degraded
convert -density 300 "big image.pdf" -alpha off -background white -flatten result.png
^^^^ Text is degraded
convert -density 300 "big image.pdf" -background white -flatten -alpha off result.png
^^^^ Results in "transparent" PNG file when viewed in Graphic Workshop
convert -density 300 "big image.pdf" -background white -alpha off -flatten result.png
^^^^ Text is degraded
convert -density 300 "big image.pdf" -alpha off -background white -flatten result.png
^^^^ Text is degraded
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: PDF to PNG: Newbie victory and a question
Very strange. What version of IM, on what platform?ThomasHR wrote:convert -density 300 "big image.pdf" -background white -flatten -alpha off result.png
^^^^ Results in "transparent" PNG file when viewed in Graphic Workshop
Can you post your PDF somewhere?
snibgo's IM pages: im.snibgo.com
Re: PDF to PNG: Newbie victory and a question
I'll post the PDF later today.snibgo wrote:Very strange. What version of IM, on what platform?ThomasHR wrote:convert -density 300 "big image.pdf" -background white -flatten -alpha off result.png
^^^^ Results in "transparent" PNG file when viewed in Graphic Workshop
Can you post your PDF somewhere?
I have IM 6.8.9-Q16 and Windows 7 64-bit.
Graphic Workshop Professional is version 3.0a Patch 43, from 2010.
Last edited by ThomasHR on 2014-08-11T23:04:18-07:00, edited 1 time in total.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: PDF to PNG: Newbie victory and a question
Can you ...
... and paste the contents of r.txt here, between [ code ] and [ /code ]?
Code: Select all
identify -verbose result.png >r.txt
snibgo's IM pages: im.snibgo.com
Re: PDF to PNG: Newbie victory and a question
You mean for the "transparent" result.png?snibgo wrote:Can you ...... and paste the contents of r.txt here, between [ code ] and [ /code ]?Code: Select all
identify -verbose result.png >r.txt
Code: Select all
Image: result.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: DirectClass
Geometry: 3924x2775+0+0
Resolution: 300x300
Print size: 13.08x9.25
Units: Undefined
Type: TrueColor
Endianess: Undefined
Colorspace: sRGB
Depth: 16-bit
Channel depth:
red: 16-bit
green: 16-bit
blue: 16-bit
Channel statistics:
Red:
min: 0 (0)
max: 65535 (1)
mean: 43270 (0.660259)
standard deviation: 23254.3 (0.354838)
kurtosis: -1.3956
skewness: -0.464055
Green:
min: 0 (0)
max: 65535 (1)
mean: 43712.8 (0.667014)
standard deviation: 22085.6 (0.337005)
kurtosis: -1.24999
skewness: -0.484068
Blue:
min: 0 (0)
max: 65535 (1)
mean: 38787.5 (0.59186)
standard deviation: 24381.6 (0.37204)
kurtosis: -1.56771
skewness: -0.148037
Image statistics:
Overall:
min: 0 (0)
max: 65535 (1)
mean: 41923.5 (0.639711)
standard deviation: 23259.4 (0.354916)
kurtosis: -1.40522
skewness: -0.371079
Rendering intent: Perceptual
Gamma: 0.45455
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Background color: white
Border color: srgb(223,223,223)
Matte color: grey74
Transparent color: black
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 3924x2775+0+0
Dispose: Undefined
Iterations: 0
Compression: Zip
Orientation: Undefined
Properties:
date:create: 2014-08-12T01:20:49-05:00
date:modify: 2014-08-12T01:20:55-05:00
pdf:HiResBoundingBox: 941.666x666+0+0
pdf:Version: PDF-1.3
png:bKGD: chunk was found (see Background color, above)
png:cHRM: chunk was found (see Chromaticity, above)
png:gAMA: gamma=0.45454544 (See Gamma, above)
png:IHDR.bit-depth-orig: 16
png:IHDR.bit_depth: 16
png:IHDR.color-type-orig: 2
png:IHDR.color_type: 2 (Truecolor)
png:IHDR.interlace_method: 0 (Not interlaced)
png:IHDR.width,height: 3924, 2775
png:pHYs: x_res=300, y_res=300, units=0
png:sRGB: intent=0 (Perceptual Intent)
png:text: 4 tEXt/zTXt/iTXt chunks were found
signature: 8a9398f71fff9b2ca644f8b6126db493dc7b324d247d569f4e3e5684bd95db96
Artifacts:
filename: result.png
verbose: true
Tainted: False
Filesize: 9.592MB
Number pixels: 10.89M
Pixels per second: 10.81MB
User time: 0.796u
Elapsed time: 0:02.006
Version: ImageMagick 6.8.9-6 Q16 x64 2014-07-22 http://www.imagemagick.org
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: PDF to PNG: Newbie victory and a question
Yes, that's what I meant. I can't see anything in there that would make software think it should be transparent. It does have "pdf:HiResBoundingBox: 941.666x666+0+0". I don't know what that means.
snibgo's IM pages: im.snibgo.com
Re: PDF to PNG: Newbie victory and a question
Just for comparison, here are the numbers for "big image.png" (see original post), which meets all specifications, including being viewable by Graphic Workshop Professional:
Code: Select all
Image: big image.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: DirectClass
Geometry: 3924x2775+0+0
Resolution: 300x300
Print size: 13.08x9.25
Units: Undefined
Type: TrueColorAlpha
Endianess: Undefined
Colorspace: sRGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
alpha: 1-bit
Channel statistics:
Red:
min: 0 (0)
max: 255 (1)
mean: 168.366 (0.660258)
standard deviation: 90.4836 (0.354838)
kurtosis: -1.3956
skewness: -0.464054
Green:
min: 0 (0)
max: 255 (1)
mean: 170.089 (0.667014)
standard deviation: 85.9362 (0.337005)
kurtosis: -1.24999
skewness: -0.484066
Blue:
min: 0 (0)
max: 255 (1)
mean: 150.924 (0.591859)
standard deviation: 94.8702 (0.37204)
kurtosis: -1.56771
skewness: -0.148033
Alpha:
min: 255 (1)
max: 255 (1)
mean: 255 (1)
standard deviation: 0 (0)
kurtosis: 0
skewness: 0
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 122.345 (0.479783)
standard deviation: 78.3783 (0.307366)
kurtosis: 1.41301
skewness: 0.301559
Rendering intent: Perceptual
Gamma: 0.45455
Chromaticity:
red primary: (0.64,0.33)
green primary: (0.3,0.6)
blue primary: (0.15,0.06)
white point: (0.3127,0.329)
Background color: white
Border color: srgba(223,223,223,1)
Matte color: grey74
Transparent color: none
Interlace: None
Intensity: Undefined
Compose: Over
Page geometry: 3924x2775+0+0
Dispose: Undefined
Iterations: 0
Compression: Zip
Orientation: Undefined
Properties:
date:create: 2014-08-12T04:11:47-05:00
date:modify: 2014-08-12T04:11:52-05:00
pdf:HiResBoundingBox: 941.666x666+0+0
pdf:Version: PDF-1.3
png:bKGD: chunk was found (see Background color, above)
png:cHRM: chunk was found (see Chromaticity, above)
png:gAMA: gamma=0.45454544 (See Gamma, above)
png:IHDR.bit-depth-orig: 8
png:IHDR.bit_depth: 8
png:IHDR.color-type-orig: 6
png:IHDR.color_type: 6 (RGBA)
png:IHDR.interlace_method: 0 (Not interlaced)
png:IHDR.width,height: 3924, 2775
png:pHYs: x_res=300, y_res=300, units=0
png:sRGB: intent=0 (Perceptual Intent)
png:text: 4 tEXt/zTXt/iTXt chunks were found
signature: 8eca60fa3268f91caac84db3a0049962de603c851712d04739afc8ed5833d7a9
Artifacts:
filename: big image.png
verbose: true
Tainted: False
Filesize: 7.844MB
Number pixels: 10.89M
Pixels per second: 19.04MB
User time: 0.577u
Elapsed time: 0:01.572
Version: ImageMagick 6.8.9-6 Q16 x64 2014-07-22 http://www.imagemagick.org
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: PDF to PNG: Newbie victory and a question
It would be best to post your image so we can experiment with it to try to duplicate your bad results.
Re: PDF to PNG: Newbie victory and a question
The "verbose" outputs show that your "transparent" PNG is color-type 2, and does not have
a tRNS chunk, so it's opaque. But it has 16-bit samples, so maybe GWS has trouble
reading 16-bit PNGs. The other output is 8-bit RGBA, which could be transparent but
only if any of the pixels have a non-255 alpha sample.
You could try adding "-depth 8" as the final option while writing the "transparent" one,
and see if the 8-bit result gets read properly.
a tRNS chunk, so it's opaque. But it has 16-bit samples, so maybe GWS has trouble
reading 16-bit PNGs. The other output is 8-bit RGBA, which could be transparent but
only if any of the pixels have a non-255 alpha sample.
You could try adding "-depth 8" as the final option while writing the "transparent" one,
and see if the 8-bit result gets read properly.
Re: PDF to PNG: Newbie victory and a question
Here's my PDF:
http://tomhrichardson.files.wordpress.c ... bcover.pdf
I'll keep it up for a week (through August 19).
http://tomhrichardson.files.wordpress.c ... bcover.pdf
I'll keep it up for a week (through August 19).