Back up your photos before using this tool! You are responsible for any unexpected data loss that may occur through improper use of this package.
Open an image with EXIF metadata using the Python
open() built-in function. Ensure the
binary mode flag (i.e.
'rb') is set. Pass this image file object into the
>>> from exif import Image >>> with open('grand_canyon.jpg', 'rb') as image_file: ... my_image = Image(image_file) ...
Alternatively, supply a file path or image bytes to the
>>> my_image = Image('grand_canyon.jpg') >>> from exif import Image >>> with open('grand_canyon.jpg', 'rb') as image_file: ... image_bytes = image_file.read() ... >>> my_image = Image(image_bytes)
Verify that an image has EXIF metadata by leveraging the
>>> my_image.has_exif True
Write the image with modified EXIF metadata to an image file using
open() in binary
>>> with open('modified_image.jpg', 'wb') as new_image_file: ... new_image_file.write(my_image.get_file()) ...
Extract the thumbnail embedded within the EXIF data by using
get_thumbnail() instead of
Add geolocation metadata to an image by providing tuples of degrees, minutes, and decimal seconds:
>>> from exif import Image >>> image = Image("cleveland_public_square.jpg") >>> >>> image.gps_latitude = (41.0, 29.0, 57.48) >>> image.gps_latitude_ref = "N" >>> image.gps_longitude = (81.0, 41.0, 39.84) >>> image.gps_longitude_ref = "W" >>> image.gps_altitude = 199.034 # in meters >>> image.gps_altitude_ref = GpsAltitudeRef.ABOVE_SEA_LEVEL >>> >>> # Then, save image to desired location using code discussed above.
datetime_digitized to add timestamps to an
image (e.g., from a scanner):
>>> from exif import Image, DATETIME_STR_FORMAT >>> from datetime import datetime >>> datetime_taken = datetime(year=1999, month=12, day=31, hour=23, minute=49, second=12) >>> datetime_scanned = datetime(year=2020, month=7, day=11, hour=10, minute=11, second=37) >>> >>> image = Image("my_scanned_photo.jpg") >>> image.datetime_original = datetime_taken.strftime(DATETIME_STR_FORMAT) >>> image.datetime_digitized = datetime_scanned.strftime(DATETIME_STR_FORMAT) >>> # Then, save image to desired location using code discussed above.
This sample script was provided by Rune Monzel.
It demonstrates how to use this package with NumPy and an image encoder, specifically OpenCV in this case:
import exif import cv2 import numpy as np # Create a random 2D array within range [0 255] image = (np.random.rand(800, 1200) * 255).astype(np.uint8) # decode to the appropriate format # jpg -> compressed with information loss) status, image_jpg_coded = cv2.imencode('.jpg', image) print('successful jpg encoding: %s' % status) # tif -> no compression, no information loss status, image_tif_coded = cv2.imencode('.jpg', image) print('successful tif encoding: %s' % status) # to a byte string image_jpg_coded_bytes = image_jpg_coded.tobytes() image_tif_coded_bytes = image_tif_coded.tobytes() # using the exif format to add information exif_jpg = exif.Image(image_jpg_coded_bytes) exif_tif = exif.Image(image_tif_coded_bytes) # providing some information user_comment = "random image" software = "created in python with numpy" author = "Rune Monzel" # adding information to exif files: exif_jpg["software"] = exif_tif["software"] = software exif_jpg["user_comment"] = exif_tif["user_comment"] = user_comment # show existing tags print(exif_jpg.list_all()) # save image with open(r'random.tif', 'wb') as new_image_file: new_image_file.write(exif_tif.get_file()) with open(r'random.jpg', 'wb') as new_image_file: new_image_file.write(exif_jpg.get_file())