PS to JPG conversion munges page origin

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
rbd
Posts: 21
Joined: 2012-09-13T12:41:06-07:00
Authentication code: 67789

PS to JPG conversion munges page origin

Post by rbd »

Hi all,

I am trying to convert PostScript files to JPEGs using 'convert', and the conversion process is incorrectly translating the PS image so that the lower-left corner of the 8.5" x 11" portrait PS input image is below and to the left of the lower-left corner of the JPEG output image. More precisely, the lower edge of the JPEG output appears to correspond to the vertical center of the PS input, while the left edge of the JPEG output coresponds to a point about 1/8th of the distance from the left edge of the PS input to the right edge of the PS input. The command being used for this conversion is

convert -antialias -density 300 in.ps out.jpg

If I simply try to display the PS image with

display -flatten -colorspace RGB in.ps

I get exactly the same incorrect page origin translation.

This command used to work exactly as desired with a previous IM release, but I recently upgraded from CentOS 5.x to 6.3, and am now running IM 6.5.4-7 on x8664 hardware. (Sorry, I don't know which IM release I used to run under CentOS 5.x where this command actually worked properly.)

I can convert the PS file to PDF with

ps2pdf13 in.ps

on the same CentOS 6.3 system, and then do

convert -density 300 in.pdf out.jpg

and everything looks OK (i.e., no incorrect page origin translation), but I would prefer not to do this as these files are quite large and the conversion is very time-consuming.

I have spent several happy hours playing with various IM resize/repage options and offset values but cannot get the direct conversion from PS to JPEG to work. Although I can manage to translate the output image to various locations within the JPEG, there is always blank space in many areas where there should be imagery, i.e., the only non-blank area of the JPEG is the non-blank area in the untranslated JPEG output which appeared at the lower-left corner of that image. If I try to translate things to the upper-right where they actually belong I only get blank filler along the left and lower edges of the JPEG output where I should be seeing imagery.

Can anyone tell me how to do a direct conversion of PS to JPG without munging the page origin? What happened in the recent IM release to break what used to work so nicely?

Thanks!

Roger Davis
Univ. of Hawaii
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PS to JPG conversion munges page origin

Post by fmw42 »

Does it happen with all ps images? Or is it just that one?

It may be a missing or changed version of Ghostscript. Type

gs --version
9.05

What version do you get?

Can you post a link to a small ps file that has the problem you descibed?
rbd
Posts: 21
Joined: 2012-09-13T12:41:06-07:00
Authentication code: 67789

Re: PS to JPG conversion munges page origin

Post by rbd »

Hi fmw42,

'gs -version' says

GPL Ghostscript 8.70 (2009-07-31)
Copyright (C) 2009 Artifex Software, Inc. All rights reserved.

This is whatever came standard with CentOS 6.3, I did not install this (or IM, for that matter) manually outside of the CentOS installation.

The problem does not occur with all PS files, but it does occur with all files generated by a rather complicated map-making script we employ which is based on GMT, a commonly-used map-making package in our community. And all of these files *did* used to convert properly under whatever combination of IM and gs versions were part of CentOS 5.7. Besides CentOS 6.3's ps2pdf13 being able to correctly convert these to PDF, Mac OS will also correctly convert these PS files to PDF for display with Preview.

Not sure how I can give you a link to these files, but I did put two of them (in1.ps and in2.ps) on our site's anonymous FTP server located at ftp.soest.hawaii.edu. Go to pub/rbd to find the files, which are about 14Mb each. If you can tell me how to upload them somewhere else where you can get at them easier I would be very happy to do that as well. I also placed an in1.pdf file there which I created with

ps2pdf13 in.ps

If you run

display -flatten -colorspace RGB in1.pdf

you'll see what the image is actually supposed to look like.

Thanks!

Roger
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PS to JPG conversion munges page origin

Post by fmw42 »

Roger,

I downloaded in1.ps and in1.pdf

For future reference, all you have to do is post the files somewhere and just provide the link here to those files. If they are on your server and permissions are ok for public access then that should suffice.

Your in1.ps is in CMYK colorspace. I don't know how well ghostscript will handle that, esp. via IM. You could try using GS standalone.

When I tried the command below, it left the background black rather than white and I had to display it in IM. Many viewers do not display CMYK jpg properly.

I used

convert -density 300 in1.ps in1.jpg

To make it look proper to me, I converted it to RGB (actually sRGB). On your version -colorspace RGB on the current IM version 6.7.9.4 (which I used), it would be -colorspace sRGB.

convert -colorspace RGB -density 300 in1.ps in1.jpg

To me that looked the same as your PDF. I do not see any offsets regarding the origin.

See if the latter works. If not then it must be either your version of GS or IM. If possible try to upgrade GS. It is possible that the version you are using may have a bug in it.

Also check your delegates.xml file and see if you have the following setup.

<delegate decode="ps" encode="eps" mode="bi" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=epswrite" "-sOutputFile=%o" "-f%i""/>

<delegate decode="ps" encode="pdf" mode="bi" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pdfwrite" "-sOutputFile=%o" "-f%i""/>

<delegate decode="ps" encode="print" mode="encode" command="lpr "%i""/>

<delegate decode="ps:alpha" stealth="True" command=""gs" -q -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""/>

<delegate decode="ps:cmyk" stealth="True" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pam" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s""/>

<delegate decode="ps:color" stealth="True" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pnmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s""/>

<delegate decode="ps:mono" stealth="True" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pbmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s""/>


It is possible that there is a misconfiguration or one of the above is missing, esp. the cmyk and/or color depending upon whether you added -colorspace RGB as mentioned above.

Hope one these helps.
rbd
Posts: 21
Joined: 2012-09-13T12:41:06-07:00
Authentication code: 67789

Re: PS to JPG conversion munges page origin

Post by rbd »

Hi fmw42,

I had figured out that I needed to use -colorspace RGB. Oddly, however, I only seem to need it when running display. convert got the colorspace right, it just messed up the page origin. IM's display program messed up the origin *and* the colorspace unless I used -colorspace RGB.

I just tried your recommended

convert -colorspace RGB -density 300 in1.ps in1.jpg

Unfortunately, the true page origin is still shifted off the lower-left corner of the output JPEG. What version of GS are you using?

I have the same delegates file entries that you show, but there are some minor discrepancies in detail. Here's the cmyk entry:

<delegate decode="ps:cmyk" stealth="True" command=""gs" -q -dQUIET -dPARANOIDSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 "-sDEVICE=pam" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s""/>

Note that mine has -dPARANOIDSAFER and -dGridFitTT=0 whereas yours has -dSAFER and -dGridFitTT=2, otherwise I think they're the same. Could the GridFitTT setting be the source of the problem? I have no idea what that does. My ps to pdf entry also has -dGridFitTT=0, but my GS' ps to pdf conversion (at least via ps2pdf13) seems to work OK.

You suggested that I could use GS standalone. Are you saying there's a way to use GS to convert PS to JPEG directly? I'm not seeing how to do that, but I know little about GS (or IM for that matter). Any advice?

Thanks again!

Roger
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PS to JPG conversion munges page origin

Post by fmw42 »

Roger,

gs --version
9.05

Sorry I know little more than what I suggested. I have not changed my delegates.xml file and have never run GS directly, but others on the forum have. Perhaps one of them can suggest syntax. Or go to the GS home page, search with Google and see if there are any docs.

Fred

P.S. The IM display function, I believe has been improved in more current versions of IM. I did not have any trouble with origins, only the black background without using -colorspace RGB. I don't think that helps you on your old version of IM. Too bad you are stuck with that. Though you could try installing from source code for IM and for the delegates you need if you want a more current IM version and GS version. It can be a pain, though.

P.S. 2 Are you sure it is not just a display issue? Have you tried other viewers?
rbd
Posts: 21
Joined: 2012-09-13T12:41:06-07:00
Authentication code: 67789

Re: PS to JPG conversion munges page origin

Post by rbd »

Hi Fred,

Both display and eog show the same shifted page origin, so I don't think it's a viewer problem.

Today I will try installing the latest versions of IM and GS and see where that goes, it does seem highly likely one of them (my older versions) is buggy since you cannot duplicate the problem.

Thanks a miilion for all your help!

Roger
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PS to JPG conversion munges page origin

Post by fmw42 »

rbd wrote:Hi Fred,

Both display and eog show the same shifted page origin, so I don't think it's a viewer problem.

Today I will try installing the latest versions of IM and GS and see where that goes, it does seem highly likely one of them (my older versions) is buggy since you cannot duplicate the problem.

Thanks a miilion for all your help!

Roger
Let me make a small version of your image and upload it before you go to the trouble, to be sure that what I got was what you expect. Back shortly with the example.

Fred

OK. here it is.

convert -density 150 -colorspace sRGB in1.ps in1d.jpg

http://www.fmwconcepts.com/misc_tests/p ... t/in1d.jpg
rbd
Posts: 21
Joined: 2012-09-13T12:41:06-07:00
Authentication code: 67789

Re: PS to JPG conversion munges page origin

Post by rbd »

Hi Fred,

Problem fixed! It was a buggy IM release (6.5.4-7) -- I downloaded and installed 6.7.9-5 and all now works as it should with regard to the page origin. (The image you uploaded is in fact correct, thanks for doing that.)

I have since been playing (for hours) with the colorspace issue to figure out what our best solution is on that point. There are clearly tradeoffs to be made. If I convert the PS to JPEG using sRGB the output files are only about 1/3 the size in comparison with preserving the CMYK colorspace via convert's default behavior with no -colorspace option specified. That's a big plus for sRGB. Unfortunately, the displayed text strings are of noticeably lower quality in the sRGB JPEGs no matter which viewer I use (we use both eog and IM's display). Depending upon the viewer, however, the color imagery of sRGB may be substantially better (using IM display) or pretty much the same (using eog) as CMYK's quality. I seem to have opened an annoying can of worms here -- for now I'm probably going to punt and just continue doing what we've done in the past (store in CMYK) (i) so our clients aren't surprised by any change and (ii) since we get maximum overall viewing quality using the combination of CMYK format and eog viewer, but this may bear further thought at some point.

Thanks again for all of your help!

Roger
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PS to JPG conversion munges page origin

Post by fmw42 »

use supersampling. this should preserve the ps image size, but make the quality much better. If you need a larger resulting image, change the density

Nominal density is 72 dpi. So multiply by 4 for density and then resize by 1/4 = 25%

convert -density 288 -colorspace sRGB in1.ps -resize 25% out.jpg
rbd
Posts: 21
Joined: 2012-09-13T12:41:06-07:00
Authentication code: 67789

Re: PS to JPG conversion munges page origin

Post by rbd »

Hi Fred,

Thanks for the supersampling suggestion -- yet more tradeoffs, unfortunately. The text does indeed look much better, but the fine-scale contour lines in the imagery virtually disappear, and the conversion takes about 3x longer, which is also unfortunately a significant consideration. However, thanks to you I am indeed learning a lot about IM!

Roger
Post Reply