The Python Imaging Library adds image processing capabilities to your Python interpreter.
This library provides extensive file format support, an efficient internal representation, and fairly powerful image processing capabilities.
The core image library is designed for fast access to data stored in a few basic pixel formats. It should provide a solid foundation for a general image processing tool.
Let’s look at a few possible uses of this library.
import math, operator
from PIL import Image
def compare(file1, file2):
image1 = Image.open(file1)
image2 = Image.open(file2)
h1 = image1.histogram()
h2 = image2.histogram()
rms = math.sqrt(reduce(operator.add,map(lambda a,b: (a-b)**2, h1, h2))/len(h1))
return rms
if __name__=='__main__':
import sys
file1, file2 = sys.argv[1:]
print compare(file1, file2)
from PIL import Image
def get_exif_tags(img_obj):
ret = {}
try:
info = img_obj._getexif()
if info:
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
ret[decoded] = value
except: pass
return ret
if __name__ == "__main__":
url="http://www.scipy-lectures.org/_images/face.png"
response = requests.get(url,verify=False).content
img_obj = PIL.Image.open(BytesIO(response))
get_exif_tags(img_obj)
from PIL import Image
image_file = Image.open(filename)
print("File Size In Bytes:- "+str(len(image_file.fp.read()))
def sizeof_fmt(self,num, suffix='o'):
"""Readable file size
:param num: Bytes value
:type num: int
:param suffix: Unit suffix (optionnal) default = o
:type suffix: str
:rtype: str
"""
for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
if abs(num) < 1024.0:
return "%3.1f %s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix)