Skip to content

Getting image sizes automatically

Giacomo Marchioro edited this page Sep 30, 2022 · 3 revisions

pyIIIFpres do not provide built-in methods for inferring information about the images. Depending on your need you might find useful different solutions.

Format not supported
Using ImageAPI None
Pillow None.
Exiftools None
OpenCV jpg 2000
Matplotlib jpg 2000

Using ImageAPI

import requests
# when you use a proxy you might have to use the original link e.g. "http://localhost:1080/iipsrv/iipsrv.fcgi?iiif=/imageapi//m0171_0/m0171_0visn20_0001a21.jp2/info.json"
iiifimageurl = "http://lezioni.meneghetti.univr.it//imageapi/m0171_0/m0171_0visn20_0001a21.jp2/info.json" 
imageinfo =  requests.get(iiifimageurl)
jsoninfo = imageinfo.json()
imgwidth = jsoninfo['width']
imgheight = jsoninfo['height']

Exiftool

Once installed Exiftool it can be called as a subprocess:

from subprocess import check_output
out = check_output(["exiftool", imagepath])
Metadata = dict((e[:32].strip(),e[33:].strip()) for e in out.decode('utf8').split('\n'))
width = Metadata['Image Width']
height = Metadata['Image Height']

Using Pillow

pip install Pillow

from PIL import Image
image = PIL.Image.open("image.png")
width, height = image.size

Using OpenCV

pip install opencv-python

import cv2
img = cv2.imread('image.jpg')
width, height, channelsN = img.shape