Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

How to obtain a jpeg resolution in python

def get_image_size(fname):
    '''Determine the image type of fhandle and return its size.
    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 != 0x0d0a1a0a:
                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) # Read 0xff next
                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))
            except Exception: #IGNORE:W0703
                return
        else:
            return
        return str(width) + "x" + str(height)
Comment

PREVIOUS NEXT
Code Example
Python :: python __div__ 
Python :: Python __ne__ 
Python :: Python how to use __div__ 
Python :: Python __le__ magic method 
Python :: modles en django 
Python :: NumPy rot90 Example Rotating Once 
Python :: pandas listagg equivalent in python 
Python :: get forex exchange rates in python 
Python :: Create a list of multiples of 3 from 0 to 20. 
Python :: NumPy unpackbits Code Unpacked array along default axis 
Python :: ROS subscribes to image type video frames (Python) through topic Publishing 
Python :: using .get() for deep dictionary 
Python :: Python matplotlib multiple bars 
Python :: penggunaan values di python 
Python :: Accessing range() with index value 
Python :: knn compute_distances_one_loop 
Python :: lda from scratch implementation on iris python 
Python :: dnpy notify 
Python :: matrix implement 
Python :: find sum of all elements in a matrix by python 
Python :: python pyramid pattern 
Python :: cours python 
Python :: python without creating pyc 
Python :: ring Sort List Item 
Python :: can you make a class in a class python 
Python :: python list insert multiple 
Python :: module not found after sucessful install 
Python :: python getpass save file 
Python :: foreign key on delete arguments 
Python :: long type python 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =