Skip to content

Commit f5c04c2

Browse files
committed
Add support for saving CMYK images
1 parent 68255e3 commit f5c04c2

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

drawBot/context/imageContext.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ def _tiffCompressionConverter(value):
3232
return t.get(value.lower(), AppKit.NSTIFFCompressionNone)
3333

3434

35+
def _colorSpaceConverter(value):
36+
if value == "CMYK":
37+
return AppKit.NSDeviceCMYKColorSpace
38+
return AppKit.NSCalibratedRGBColorSpace
39+
40+
3541
_nsImageOptions = {
3642
# DrawBot Key: (
3743
# NSImage property key,
@@ -118,6 +124,7 @@ class ImageContext(PDFContext):
118124
"A Boolean value that specifies whether subpixel quantization of glyphs is allowed. Default is True.",
119125
),
120126
("multipage", "Output a numbered image for each page or frame in the document."),
127+
("colorSpace", "Set color space (RGB, CMYK). Default is RGB."),
121128
]
122129

123130
ensureEvenPixelDimensions = False
@@ -138,6 +145,10 @@ def _writeDataToFile(self, data, path, options):
138145
imageResolution = options.get("imageResolution", 72.0)
139146
antiAliasing = options.get("antiAliasing", True)
140147
fontSubpixelQuantization = options.get("fontSubpixelQuantization", True)
148+
149+
colorSpace = options.get("colorSpace", "RGB")
150+
colorSpaceName = _colorSpaceConverter(colorSpace)
151+
141152
properties = {}
142153
for key, value in options.items():
143154
if key in _nsImageOptions:
@@ -154,7 +165,12 @@ def _writeDataToFile(self, data, path, options):
154165
antiAliasing=antiAliasing,
155166
fontSubpixelQuantization=fontSubpixelQuantization,
156167
imageResolution=imageResolution,
168+
colorSpaceName=colorSpaceName,
157169
)
170+
171+
if "imageColorSyncProfileData" in options:
172+
imageRep.setProperty_withValue_(AppKit.NSImageColorSyncProfileData, options["imageColorSyncProfileData"]) # doing this with representationUsingType_properties_ does not work
173+
158174
if self.ensureEvenPixelDimensions:
159175
if imageRep.pixelsWide() % 2 or imageRep.pixelsHigh() % 2:
160176
msg = f"Exporting to {', '.join(self.fileExtensions)} doesn't support odd pixel dimensions for width and height."
@@ -189,13 +205,17 @@ def _makeBitmapImageRep(
189205
elif nsImage is not None:
190206
width, height = nsImage.size()
191207

208+
hasAlpha = True
209+
if colorSpaceName == AppKit.NSDeviceCMYKColorSpace:
210+
hasAlpha = False # Quartz doesn’t support alpha for CMYK bitmaps.
211+
192212
rep = AppKit.NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(
193213
None, # planes
194214
int(width * scaleFactor), # pixelsWide
195215
int(height * scaleFactor), # pixelsHigh
196216
8, # bitsPerSample
197217
4, # samplesPerPixel
198-
True, # hasAlpha
218+
hasAlpha, # hasAlpha
199219
False, # isPlanar
200220
colorSpaceName, # colorSpaceName
201221
0, # bytesPerRow

0 commit comments

Comments
 (0)