CairoSVG icon indicating copy to clipboard operation
CairoSVG copied to clipboard

Add an option for forcing conversion of external images into a given mode

Open Alphare opened this issue 7 years ago • 0 comments

Hi, Within our use-case of CairoSVG, we convert user-submitted SVG that may contain external images. Some of those images use the CMYK colorspace, which breaks the conversion process with the following message: OSError: cannot write mode CMYK as PNG

One quick, dirty and non-reusable fix is the following in image.py:88:

image = Image.open(BytesIO(image_bytes))
if forced_external_image_mode and image.mode != forced_external_image_mode:
    image = image.convert(forced_external_image_mode)
image.save(png_file, 'PNG')

In our case, this will fix the issue when forced_external_image_mode is "RGBA".

I have started my own fork to be able to continue using CairoSVG in production without errors. I can create a PR if you like, but I figured you would want to choose how we can go about adding this option without breaking the current TAGS internal API.

Thanks in advance

Alphare avatar Feb 05 '18 10:02 Alphare