#!/usr/bin/env python # # Copyright (C) 2016 GNS3 Technologies Inc. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program 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 General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import io import struct from xml.etree.ElementTree import ElementTree def get_size(data, default_width=0, default_height=0): """ Get image size :param data: A buffer with image content :return: Tuple (width, height, filetype) """ height = default_height width = default_width filetype = None # Original version: # https://github.com/shibukawa/imagesize_py # # The MIT License (MIT) # # Copyright © 2016 Yoshiki Shibukawa # # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. size = len(data) # handle GIFs if size >= 10 and data[:6] in (b'GIF87a', b'GIF89a'): # Check to see if content_type is correct try: width, height = struct.unpack("= 24 and data.startswith(b'\211PNG\r\n\032\n') and data[12:16] == b'IHDR': try: width, height = struct.unpack(">LL", data[16:24]) filetype = "png" except struct.error: raise ValueError("Invalid PNG file") # Maybe this is for an older PNG version. elif size >= 16 and data.startswith(b'\211PNG\r\n\032\n'): # Check to see if we have the right content type try: width, height = struct.unpack(">LL", data[8:16]) filetype = "png" except struct.error: raise ValueError("Invalid PNG file") # handle JPEGs elif size >= 2 and data.startswith(b'\377\330'): try: # Not very efficient to copy data to a buffer fhandle = io.BytesIO(data) size = 2 ftype = 0 while not 0xc0 <= ftype <= 0xcf: fhandle.seek(size, 1) byte = fhandle.read(1) while ord(byte) == 0xff: byte = fhandle.read(1) ftype = ord(byte) size = struct.unpack('>H', fhandle.read(2))[0] - 2 # We are at a SOFn block fhandle.seek(1, 1) # Skip `precision' byte. height, width = struct.unpack('>HH', fhandle.read(4)) filetype = "jpg" except struct.error: raise ValueError("Invalid JPEG file") # End of https://github.com/shibukawa/imagesize_py # handle SVG elif size >= 10 and data.startswith(b' 3: if size[-2:] in conversion_table: return round(float(size[:-2]) * conversion_table[size[-2:]]) return round(float(size))