(fname)
Determine the image type of fhandle and return its size.
    Taken from https://stackoverflow.com/a/39778771
Determine the image type of fhandle and return its size.
    Taken from https://stackoverflow.com/a/39778771
Determine the image type of fhandle and return its size . Taken from https : // stackoverflow . com / a / 39778771
def get_image_size(fname):
    """
    Determine the image type of fhandle and return its size.
    Taken from https://stackoverflow.com/a/39778771
    """
    with open(fname, 'rb') as fhandle:
        head = fhandle.read(24)
        if len(head) != 24:
            return
        what = imghdr.what(None, head)
        if what == 'png':
            check = struct.unpack('>i', head[4:8])[0]
            if check != 0x0d0a1a0a:
                return
            width, height = struct.unpack('>ii', head[16:24])
        elif what == 'gif':
            width, height = struct.unpack('<HH', head[6:10])
        elif what == 'jpeg':
            try:
                fhandle.seek(0) # Read 0xff next
                size = 2
                ftype = 0
                while not 0xc0 <= ftype <= 0xcf or ftype in (0xc4, 0xc8, 0xcc):
                    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))
            except Exception:  #IGNORE:W0703
                return
        else:
            return

        return width, height
def get_image_size ( fname ) : with open ( fname , 'rb' ) as fhandle : head = fhandle . read ( 24 ) if len ( head ) != 24 : return what = imghdr . what ( None , head ) if what == 'png' : check = struct . unpack ( '>i' , head [ 4 : 8 ] ) [ 0 ] if check != 0x0d0a1a0a : return width , height = struct . unpack ( '>ii' , head [ 16 : 24 ] ) elif what == 'gif' : width , height = struct . unpack ( '<HH' , head [ 6 : 10 ] ) elif what == 'jpeg' : try : fhandle . seek ( 0 ) # Read 0xff next size = 2 ftype = 0 while not 0xc0 <= ftype <= 0xcf or ftype in ( 0xc4 , 0xc8 , 0xcc ) : 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 ) ) except Exception : #IGNORE:W0703 return else : return return width , height