issue of converting .eps file to .png file with gs - ghostscript

I tried to use the command to convert the .eps file to .png file
gs -dSAFER -dEPSCrop -r300 -sDEVICE=pngalpha -o test.png test.eps
But the figure has the wrong bbox, just like viewing it in the letter size in gv.
If I used
gs -dEPSCrop -r300 -sDEVICE=bbox -o test.png test.eps
gs is able to find the correct bbox as (well close, the exact BoudingBox in the .eps file is "21 54 1101 558"):
%%BoundingBox: 35 62 1083 554
%%HiResBoundingBox: 35.040001 62.400002 1082.640041 553.440021
Is there something I miss to convert the .eps file to .png file with the bbox? Thank you.

Related

ImageMagick: guess raw image height

I'm using convert utility from ImageMagick to convert raw image bytes to usable image format such as PNG. My raw files are generated by code, so there is no any headers, just pure pixels.
In order to convert my image I'm using command:
$ convert -depth 1 -size 576x391 -identify gray:image.raw image.png
gray:image.raw=>image.raw GRAY 576x391 576x391+0+0 1-bit Gray 28152B 0.010u 0:00.009
The width is fixed and pretty known for me. However I have to evaluate the height of the image from the file size each time which is annoying.
Without height specified or if wrong height is specified the utility compains:
$ convert -depth 1 -size 576 -identify gray:image.raw image.png
convert-im6.q16: must specify image size `image.raw' # error/gray.c/ReadGRAYImage/143.
convert-im6.q16: no images defined `image.png' # error/convert.c/ConvertImageCommand/3258.
$ convert -depth 1 -size 576x390 -identify gray:iphone.raw iphone.png
convert-im6.q16: unexpected end-of-file `image.raw': No such file or directory # error/gray.c/ReadGRAYImage/237.
convert-im6.q16: no images defined `image.png' # error/convert.c/ConvertImageCommand/3258.
So I wonder is there a way to automatically detect the image height based on the file/blob size?
A couple of ideas...
You may not be aware of the NetPBM format, but it is very simple and you may be able to change your software that creates the raw images so that it directly generates PBM format images which are readable and useable by OpenCV, Photoshop, GIMP, feh, eog and ImageMagick of course. It would not require any libraries or extra dependencies in your software, all you need to do is put a textual PBM header on the front, so your file looks like this:
P4
576 391
... YOUR EXISTING BINARY DATA ...
Do not forget to put newlines (i.e. linefeed character) after P4 and after 391.
You can try it for yourself and add a header onto one of your files like this and then view it with GIMP or other tool:
printf "P4\n576 391\n" > image.pbm
cat image.raw >> image.pbm
If you prefer a one-liner, just use a bash command grouping like this - which is equivalent to the 2 lines above:
{ printf "P4\n576 391\n"; cat image.raw; } > image.pbm
Be careful to have all the spaces and semi-colons exactly as I have them!
Another idea, just putting some meat on Fred's answer, might be the following one-liner which uses a bash arithmetic context and a bash command substitution, you can do this:
convert -depth 1 -size "576x$(($(stat -c "%s" image.raw)*8/576))" gray:image.raw image.png
Note that if you are on macOS, stat is a little different, so you may prefer the slightly less efficient, but more portable:
convert -depth 1 -size "576x$(($(wc -c < image.raw)*8/576))" gray:image.raw image.png
You have to know the -depth and width to compute the height for ImageMagick raw format. If depth is 1, then your image is binary (b/w). So height = 8 * file size (in B)/(width). 28152*8/391 = 576

how to save GhostScript output

I am creating an image on the fly using ghostscript. How do I write the my_image.png to /home/pi/Desktop location. I can get the image to print to screen, but not save the output to a location.
gs -sDEVICE=pngalpha -sOutputFile=- -q -r100 -g600x100 - >my_image.png
I changed the OutputFile line to
gs -sDEVICE=pngalpha -sOutputFile=/home/pi/my_image.png -q -r100 -g600x100 -

ghostscript pdfwrite specify jpeg quality

I'm trying to concatenate multiple pdf files which basically are the pages of a photobook containing jpg images. For my output pdf file I wish to adjust the image resolution to 300 dpi and I want to keep the best quality. The commands I'm using are:
gswin64c.exe -dNOPAUSE -dBATCH ^-dDownsampleColorImages=true -dColorImageResolution=300 ^-dDownsampleGrayImages=true -dGrayImageResolution=300 ^-dDownsampleMonoImages=true -dMonoImageResolution=300 ^-sDEVICE=pdfwrite -dJPEGQ=100 -sOutputFile=out.pdf in1.pdf in2.pdf
However, it seems that -dJPEGQ=100 has no effect on the output. Changing this parameter leads to the same filesize and artifacts are visible in the images for all values. Running the command with the option -dPDFSETTINGS=/printer I get better results without artifacts, however this option should also result in 300 dpi. So what is the correct command to specify the quality of the jpg images in the output file?
The solution is adjusting the DCTEncode filter with follwing commands:
gswin64c.exe -sOutputFile=out.pdf -dNOPAUSE -dBATCH ^-sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -c "<< /ColorACSImageDict << /VSamples [ 1 1 1 1 ] /HSamples [ 1 1 1 1 ] /QFactor 0.08 /Blend 1 >> /ColorImageDownsampleType /Bicubic /ColorConversionStrategy /LeaveColorUnchanged >> setdistillerparams" -f in1.pdf
which lead to a compressed file with satisfactory quality for me and can be adjusted to each individual needs.
Edit:
the .setpdfwrite argument is deprecated for recent ghostscript releases (> 9.50), so I removed it in the answer

Resize A4 PDF to SRA4 and center content

I'd like to be able to resize an A4 PDF to the slightly bigger SRA4 format, used by printing professionals. The content shouldn't be resized, but only centered. In other words, I'd like to add margins to my A4 document.
I managed more or less to do that with this Ghostscript (9.05) call:
gs -dPDFSETTINGS=/default -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite
-sOutputFile=out.pdf -dDEVICEWIDTHPOINTS=637 -dDEVICEHEIGHTPOINTS=907
-dFIXEDMEDIA in.pdf
The problem is that the A4 content is not centered in the SRA4 page, but is placed at the bottom left corner.
Any idea how to center the content?
I found a solution here, by using -c "<> setpagedevice", like so:
gs -dPDFSETTINGS=/default -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite
-sOutputFile=out.pdf -dDEVICEWIDTHPOINTS=637 -dDEVICEHEIGHTPOINTS=907
-dFIXEDMEDIA -c "<</PageOffset [21 32]>> setpagedevice" -f in.pdf
Content is perfectly centered in the SRA4 document.

How to convert CMYK eps to CMYK jpeg with ghostscript?

Ghostscript changes colospace to RGB when converting CMYK eps to jpeg.The problem is to keep colorspace untouched during conversion. Thanx in advance.
Assuming you want to convert any RGB that may be in the EPS to CMYK, use this command on Windows:
gswin32c.exe ^
-o c:/path/to/output-cmyk.jpeg ^
-sDEVICE=jpegcmyk ^
input-rgb.eps
and this on Linux/Unix/MacOSX:
gs \
-o /path/to/output-cmyk.jpeg \
-sDEVICE=jpegcmyk \
input-rgb.eps
Note, by default Ghostscript uses a resolution of 72 dpi for the JPEG output. If you want to change that to, say, 300 dpi, then add -r300 to the commandline.

Resources