[racket] pdf-dc%: Generating a pdf with image embedded in jpeg format?

From: Daniel Prager (daniel.a.prager at gmail.com)
Date: Sun Dec 7 22:53:45 EST 2014

I have some code that writes individual images to pdfs (see below).

If there's a lot of "texture" in the images, these files can be very large
indeed.

I assume that by default the image is encoded as a kind of embedded png.

I would like to be able to change the compression -- e.g. specify jpeg.

My currently workaround is to save from Racket as a png, and then convert
externally via ImageMagick: first to jpeg, then to pdf.

(How) can I achieve the same result directly from Racket?


Many thanks

Dan

----

#lang racket

(require racket/draw
         (only-in 2htdp/image
                  save-image
                  image-width
                  image-height
                  scale))

(define (image->bitmap image)
  (let* ([width (image-width image)]
         [height (image-height image)]
         [bm (make-bitmap width height)]
         [dc (send bm make-dc)])
    (send dc clear)
    (send image draw dc 0 0 0 0 width height 0 0 #f)
    bm))

(define (image->pdf image output-file [mag 1.0])
  (define dc
    (new pdf-dc%
         [ interactive #f ]
         [ use-paper-bbox #f ]
         [ width (* 0.8 mag (image-width image))]   ; Default scale is 0.8
0.8
         [ height (* 0.8 mag (image-height image))]
         [ output output-file ]))

  (send* dc
    (scale mag mag)
    (start-doc "useless string")
    (start-page))

  (send dc draw-bitmap (image->bitmap image) 0 0)

  (send* dc
    (end-page)
    (end-doc)))

(module+ test
  (require (only-in 2htdp/image
                    circle text square overlay above))

  (define im
    (overlay
     (above
      (circle 50 'solid 'red)
      (text "This is a red circle" 12 'black))
     (square 150 'solid 'lightblue)))

  (image->pdf im "image-test.pdf")
  (save-image im "image-test.png"))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20141208/e9e5bf5f/attachment.html>

Posted on the users mailing list.