<div dir="ltr"><div><font face="arial, helvetica, sans-serif">I have some code that writes individual images to pdfs (see below).</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">If there's a lot of "texture" in the images, these files can be very large indeed.</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">I assume that by default the image is encoded as a kind of embedded png.</font></div><div><br></div><div>I would like to be able to change the compression -- e.g. specify jpeg.</div><div><br></div><div>My currently workaround is to save from Racket as a png, and then convert</div><div>externally via ImageMagick: first to jpeg, then to pdf.</div><div><br></div><div>(How) can I achieve the same result directly from Racket?</div><div><br></div><div><br></div><div>Many thanks</div><div><br></div><div>Dan</div><div><font face="monospace"><br></font></div><div><font face="monospace">----</font></div><div><br></div><div><font face="monospace">#lang racket</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">(require racket/draw</font></div><div><font face="monospace"> (only-in 2htdp/image</font></div><div><font face="monospace"> save-image</font></div><div><font face="monospace"> image-width</font></div><div><font face="monospace"> image-height</font></div><div><font face="monospace"> scale</font><span style="font-family:monospace">))</span></div><div><font face="monospace"><br></font></div><div><font face="monospace">(define (image->bitmap image)</font></div><div><font face="monospace"> (let* ([width (image-width image)]</font></div><div><font face="monospace"> [height (image-height image)]</font></div><div><font face="monospace"> [bm (make-bitmap width height)]</font></div><div><font face="monospace"> [dc (send bm make-dc)])</font></div><div><font face="monospace"> (send dc clear)</font></div><div><font face="monospace"> (send image draw dc 0 0 0 0 width height 0 0 #f)</font></div><div><font face="monospace"> bm))</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">(define (image->pdf image output-file [mag 1.0])</font></div><div><font face="monospace"> (define dc</font></div><div><font face="monospace"> (new pdf-dc%</font></div><div><font face="monospace"> [ interactive #f ]</font></div><div><font face="monospace"> [ use-paper-bbox #f ]</font></div><div><font face="monospace"> [ width (* 0.8 mag (image-width image))] ; Default scale is 0.8 0.8</font></div><div><font face="monospace"> [ height (* 0.8 mag (image-height image))]</font></div><div><font face="monospace"> [ output output-file ]))</font></div><div><font face="monospace"> </font></div><div><font face="monospace"> (send* dc</font></div><div><font face="monospace"> (scale mag mag) </font></div><div><font face="monospace"> (start-doc "useless string")</font></div><div><font face="monospace"> (start-page))</font></div><div><font face="monospace"> </font></div><div><font face="monospace"> (send dc draw-bitmap (image->bitmap image) 0 0)</font></div><div><font face="monospace"> </font></div><div><font face="monospace"> (send* dc </font></div><div><font face="monospace"> (end-page)</font></div><div><font face="monospace"> (end-doc)))</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">(module+ test</font></div><div><font face="monospace"> (require (only-in 2htdp/image</font></div><div><font face="monospace"> circle text square overlay above))</font></div><div><font face="monospace"> </font></div><div><font face="monospace"> (define im </font></div><div><font face="monospace"> (overlay</font></div><div><font face="monospace"> (above</font></div><div><font face="monospace"> (circle 50 'solid 'red)</font></div><div><font face="monospace"> (text "This is a red circle" 12 'black))</font></div><div><font face="monospace"> (square 150 'solid 'lightblue)))</font></div><div><font face="monospace"> </font></div><div><font face="monospace"> (image->pdf im "image-test.pdf")</font></div><div><font face="monospace"> (save-image im "image-test.png"))</font></div><div class="gmail_signature"><div dir="ltr"><div style="font-family:arial;font-size:small"></div></div></div>
</div>