# This file is part of CairoSVG # Copyright © 2010-2018 Kozea # # This library is free software: you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free # Software Foundation, either version 3 of the License, or (at your option) any # later version. # # This library is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # details. # # You should have received a copy of the GNU Lesser General Public License # along with CairoSVG. If not, see . """ Images manager. """ import os.path from io import BytesIO from PIL import Image from .helpers import node_format, preserve_ratio, size from .parser import Tree from .surface import cairo from .url import parse_url IMAGE_RENDERING = { 'optimizeQuality': cairo.FILTER_BEST, 'optimizeSpeed': cairo.FILTER_FAST, } def image(surface, node): """Draw an image ``node``.""" base_url = node.get('{http://www.w3.org/XML/1998/namespace}base') if not base_url and node.url: base_url = os.path.dirname(node.url) + '/' url = parse_url(node.get_href(), base_url) image_bytes = node.fetch_url(url, 'image/*') if len(image_bytes) < 5: return x, y = size(surface, node.get('x'), 'x'), size(surface, node.get('y'), 'y') width = size(surface, node.get('width'), 'x') height = size(surface, node.get('height'), 'y') if image_bytes[:4] == b'\x89PNG': png_file = BytesIO(image_bytes) elif (image_bytes[:5] in (b'