[reportlab-users] Patch for inclusion of CMYK images as CMYK, not converted to RGB

Stevens, Ian IStevens at globeandmail.com
Mon Feb 2 17:09:06 EST 2009


The following patch to pdfimage includes unconverted CMYK images in the
PDF to preserve colours. We have clients who pass their PDFs through a
preflight tool before sending it to press and want all content to be
CMYK. The patch uses PIL's Image.mode to decide whether to convert the
image. We've been using this on our production servers since 2.1 without
problem.

Would it be possible to include this in the upcoming release?

Thanks,
Ian.

--- reportlab-trunk/src/reportlab/pdfgen/pdfimages.py (revision 3413)
+++ reportlab-trunk/src/reportlab/pdfgen/pdfimages.py (working copy)
@@ -95,17 +95,27 @@
self.source = 'PIL'
zlib = import_zlib()
if not zlib: return
- myimage = image.convert('RGB')
+
+ # Use the colorspace in the image
+ if image.mode == 'CMYK':
+ myimage = image
+ colorspace = 'DeviceCMYK'
+ else:
+ myimage = image.convert('RGB')
+ colorspace = 'RGB'
+
imgwidth, imgheight = myimage.size

# this describes what is in the image itself
# *NB* according to the spec you can only use the short form in
inline images
#imagedata=['BI /Width %d /Height /BitsPerComponent 8
/ColorSpace /%s /Filter [/Filter [ /ASCII85Decode /FlateDecode] ID]' %
(imgwidth, imgheight,'RGB')]
- imagedata=['BI /W %d /H %d /BPC 8 /CS /RGB /F [/A85 /Fl] ID' %
(imgwidth, imgheight)]
+ imagedata=['BI /W %d /H %d /BPC 8 /CS /%s /F [/A85 /Fl] ID' %
(imgwidth, imgheight, colorspace)]

#use a flate filter and Ascii Base 85 to compress
raw = myimage.tostring()
- assert len(raw) == imgwidth*imgheight*3, "Wrong amount of data
for image"
+
+ bpp = image.mode == 'CMYK' and 4 or 3
+ assert len(raw) == imgwidth*imgheight*bpp, "Wrong amount of
data for image"
compressed = zlib.compress(raw) #this bit is very fast...
encoded = pdfutils._AsciiBase85Encode(compressed) #...sadly
this may not be
#append in blocks of 60 characters

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20090202/9eba37f2/attachment.htm>


More information about the reportlab-users mailing list