[ACCEPTED]-Simple way to add an image in postscript-postscript

Accepted answer
Score: 11

There is a simple method and Postscript 4 does support the jpeg format. If you are 3 using ghostscript you may have to use the 2 -dNOSAFER option to open files. Here is 1 an example:

 360 72 translate     % set lower left of image at (360, 72)
  175 47 scale         % size of rendered image is 175 points by 47 points
  500                   % number of columns per row
  133                    % number of rows
  8                    % bits per color channel (1, 2, 4, or 8)
  [500 0 0 -133 0 133]       % transform array... maps unit square to pixel
  (myJPEG500x133.jpg) (r) file /DCTDecode filter % opens the file and filters the image data
  false                 % pull channels from separate sources
  3                    % 3 color channels (RGB)
Score: 3

Use a program like convert and then remove any 1 extra code it generated.

Score: 1

You can download the PostScript Language Reference, third edition from adobe (this is the 2 "bible book" for postscript). Chapter 1 4.10 Images would be a good starting point.

Score: 0

Is this a late answer! The problem with 17 -dNOSAFER prevented me from using the other solutions, so 16 I did the following:

Use Python to read the 15 JPG file as binary and make it a string, compatible 14 with /ASCIIHexDecode:

''.join(["%02x" % ord(c) for c in open(filename, "rb").read()])

Then instead of reading and decoding 13 the image file from the postscript file, paste 12 the above computed string in the postscript 11 file, and filter it, first through /ASCIIHexDecode then /DCTDecode:

  filter             % ascii to bytes
  0 dict
  /DCTDecode         % jpg to explicit

the 10 above snippet replaces (myJPEG500x133.jpg) (r) file /DCTDecode filter in the otherwise 9 very helpful @Hath995 answer.

if you want 8 something else than JPEG but still RGB (i.e.: you 7 want something for which postscript has 6 no decoder), and you can use Python to prepare 5 your postscript file, you can use PIL, like 4 this (it ignores the transparency byte, which 3 is a on/off operation in postscript):

import PIL.Image
i = PIL.Image.open("/tmp/from-template.png")
import itertools
''.join(["%02x" % g 
         for g in itertools.chain.from_iterable(
                    k[:3] for k in i.getdata())])

for 2 indexed files I would not know, but it can't 1 be difficult to work it out.

More Related questions