[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:

gsave
 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)
  colorimage
grestore
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:

(ffd8ffe000104a46494600010102002700270000ffdb004300030202020202030202020303030304060404040404080606050609080a0a090809090a0c0f0c0a0b0e0b09090d110d0e0f101011100a0c12131210130f101010ffdb00430103030304030408040408100b090b1010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010ffc00011080010001003011100021101031101ffc400160001010100000000000000000000000000060507ffc40026100002020201030207000000000000000001020304051106071221001315163132414252ffc400160101010100000000000000000000000000070403ffc4002911000201030105090100000000000000000102030004210711123151531314324142617381d1d3ffda000c03010002110311003f00de311d00e0478be19acddc79b0f8ba734aef8aa8a59a4af1c9bdc96159beef275e4efd1ccfa5f2aceea2f8e09f41e7f252a47ab4c4093ba71ceced387b7828b724e87705b588c8478ecac114e28d89e36f83d65d7643ee7eb60b03a23f1f5dff002daaacf4ae479954df1e3d33fd2b593599628d89b0071d5fae9d3bc5750b8a3f1ae3cc9cd3031b4789c689236ce568de374af543ab21b51b2b03138208076a3cef4c8b935acaf3bb05c12685036e285e550b3bccf8a41c7b2327ce78c9a6188b917b2995ab20676a8102af6dc76624c680011f9d8f0005095da5b491ccaec303f0d4f292ebba01cecf23cc57ffd9>)
  /ASCIIHexDecode
  filter             % ascii to bytes
  0 dict
  /DCTDecode         % jpg to explicit
  filter

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