def get_image_size(fname):
    'Determine the image type of fhandle and return its size.\n    from draco'
    with open(fname, 'rb') as fhandle:
        head = fhandle.read(24)
        if (len(head) != 24):
            return
        if (imghdr.what(fname) == 'png'):
            check = struct.unpack('>i', head[4:8])[0]
            if (check != 218765834):
                return
            (width, height) = struct.unpack('>ii', head[16:24])
        elif (imghdr.what(fname) == 'gif'):
            (width, height) = struct.unpack('<HH', head[6:10])
        elif (imghdr.what(fname) == 'jpeg'):
            try:
                fhandle.seek(0)
                size = 2
                ftype = 0
                while (not (192 <= ftype <= 207)):
                    fhandle.seek(size, 1)
                    byte = fhandle.read(1)
                    while (ord(byte) == 255):
                        byte = fhandle.read(1)
                    ftype = ord(byte)
                    size = (struct.unpack('>H', fhandle.read(2))[0] - 2)
                fhandle.seek(1, 1)
                (height, width) = struct.unpack('>HH', fhandle.read(4))
            except Exception:
                return
        else:
            return
        return (width, height)
Determine the image type of fhandle and return its size.
from draco
def get_image_size(fname):
    'Determine the image type of fhandle and return its size.\n    from draco'
    with open(fname, 'rb') as fhandle:
        head = fhandle.read(24)
        if (len(head) != 24):
            return
        if (imghdr.what(fname) == 'png'):
            check = struct.unpack('>i', head[4:8])[0]
            if (check != 218765834):
                return
            (width, height) = struct.unpack('>ii', head[16:24])
        elif (imghdr.what(fname) == 'gif'):
            (width, height) = struct.unpack('<HH', head[6:10])
        elif (imghdr.what(fname) == 'jpeg'):
            try:
                fhandle.seek(0)
                size = 2
                ftype = 0
                while (not (192 <= ftype <= 207)):
                    fhandle.seek(size, 1)
                    byte = fhandle.read(1)
                    while (ord(byte) == 255):
                        byte = fhandle.read(1)
                    ftype = ord(byte)
                    size = (struct.unpack('>H', fhandle.read(2))[0] - 2)
                fhandle.seek(1, 1)
                (height, width) = struct.unpack('>HH', fhandle.read(4))
            except Exception:
                return
        else:
            return
        return (width, height)