Categories
Python Answers

How to read the EXIF data for an image in Python?

Sometimes, we want to read the EXIF data for an image in Python.

In this article, we’ll look at how to read the EXIF data for an image in Python.

How to read the EXIF data for an image in Python?

To read the EXIF data for an image in Python, we can use the PIL.ExifTags module.

For instance, we write:

import PIL.ExifTags
import PIL.Image

img = PIL.Image.open('Canon_40D.jpg')
exif_data = img._getexif()

exif = {
    PIL.ExifTags.TAGS[k]: v
    for k, v in img._getexif().items() if k in PIL.ExifTags.TAGS
}
print(exif)

We open the image with:

img = PIL.Image.open('Canon_40D.jpg')

Then we get the EXIF data with:

exif_data = img._getexif()

Next, we get the EXIF tags and values from the EXIF data with:

exif = {
    PIL.ExifTags.TAGS[k]: v
    for k, v in img._getexif().items() if k in PIL.ExifTags.TAGS
}

As a result, we get something like:

{'GPSInfo': {0: b'\x02\x02\x00\x00'}, 'ResolutionUnit': 2, 'ExifOffset': 214, 'Make': 'Canon', 'Model': 'Canon EOS 40D', 'Software': 'GIMP 2.4.5', 'Orientation': 1, 'DateTime': '2008:07:31 10:38:11', 'YCbCrPositioning': 2, 'XResolution': 72.0, 'YResolution': 72.0, 'ExifVersion': b'0221', 'ComponentsConfiguration': b'\x01\x02\x03\x00', 'ShutterSpeedValue': 7.375, 'DateTimeOriginal': '2008:05:30 15:56:01', 'DateTimeDigitized': '2008:05:30 15:56:01', 'ApertureValue': 5.625, 'ExposureBiasValue': 0.0, 'MeteringMode': 5, 'UserComment': b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 'Flash': 9, 'FocalLength': 135.0, 'ColorSpace': 1, 'ExifImageWidth': 100, 'ExifInteroperabilityOffset': 948, 'FocalPlaneXResolution': 4438.356164383562, 'FocalPlaneYResolution': 4445.969125214408, 'SubsecTime': '00', 'SubsecTimeOriginal': '00', 'SubsecTimeDigitized': '00', 'ExifImageHeight': 68, 'FocalPlaneResolutionUnit': 2, 'ExposureTime': 0.00625, 'FNumber': 7.1, 'ExposureProgram': 1, 'CustomRendered': 0, 'ISOSpeedRatings': 100, 'ExposureMode': 1, 'FlashPixVersion': b'0100', 'WhiteBalance': 0, 'SceneCaptureType': 0}

for exif.

Conclusion

To read the EXIF data for an image in Python, we can use the PIL.ExifTags module.

Categories
Python Answers

How to percent-encode URL parameters in Python?

Sometimes, we want to percent-encode URL parameters in Python.

In this article, we’ll look at how to percent-encode URL parameters in Python.

How to percent-encode URL parameters in Python?

To percent-encode URL parameters in Python, we can use the urllib.parse.quote method.

For instance, we write:

import urllib.parse

query = urllib.parse.quote("Müller".encode('utf8'))
print(query)
print(urllib.parse.unquote(query))

We call urllib.parse.quote to percent encode the "Müller" string.

We’ve to call encode to encode it as a utf-8 binary string before we do the percent encoding.

Then we call urllib.parse.unquote to percent decode the string back to the original string.

Therefore, we see:

M%C3%BCller
Müller

printed.

Conclusion

To percent-encode URL parameters in Python, we can use the urllib.parse.quote method.

Categories
Python Answers

How to avoid exceeding max retries with URL in requests with Python?

Sometimes, we want to avoid exceeding max retries with URL in requests with Python.

In this article, we’ll look at how to avoid exceeding max retries with URL in requests with Python.

How to avoid exceeding max retries with URL in requests with Python?

To avoid exceeding max retries with URL in requests with Python, we can make our requests with the requests module.

For instance, we write:

import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

url = 'http://example.com'
session = requests.Session()
retry = Retry(connect=3, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)

session.get(url)

We use the Retry class to set the max number of retries.

total is the total retries to allow.

backoff_factor is the factor to apply between attempts.

Then we can use the returned object as the value of max_retries parameter of the HTTPAdapter constructor.

And we use the adapter for the requests with http and https requests by calling session.mount.

Finally, we make our GET request with session.get.

Conclusion

To avoid exceeding max retries with URL in requests with Python, we can make our requests with the requests module.

Categories
Python Answers

How to choose longest string in list with Python?

Sometimes, we want to choose longest string in list with Python.

In this article, we’ll look at how to choose longest string in list with Python.

How to choose longest string in list with Python?

To choose longest string in list with Python, we can use the max function with the key parameter set to len.

For instance, we write:

l = ['123', '123456', '1234']
m = max(l, key=len)
print(m)

We call max on list l with key set to len compare each entry in l by their lengths.

Therefore, the longest string will be returned.

And m is '123456'.

Conclusion

To choose longest string in list with Python, we can use the max function with the key parameter set to len.

Categories
Python Answers

How to send email to multiple recipients using Python smtplib?

Sometimes, we want to send email to multiple recipients using Python smtplib.

In this article, we’ll look at how to send email to multiple recipients using Python smtplib.

How to send email to multiple recipients using Python smtplib?

To send email to multiple recipients using Python smtplib, we can use the sendmail method.

For instance, we write:

import smtplib
from email.mime.text import MIMEText

s = smtplib.SMTP('smtp.live.com')
s.set_debuglevel(1)
msg = MIMEText("""body""")
sender = 'me@example.com'
recipients = ['john.doe@example.com', 'john.smith@example.co.uk']
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
s.sendmail(sender, recipients, msg.as_string())

We create the SMTP instance by using the SMTP server address as the argument.

Then we create the message with the MIMEText class.

Next, we set the subject of the message with:

msg['Subject'] = "subject line"

We set the sender email address with:

msg['From'] = sender

And we set the recipients’ emails with:

msg['To'] = ", ".join(recipients)

We combine the recipients into a string with join.

Finally, we send the email with:

s.sendmail(sender, recipients, msg.as_string())

Conclusion

To send email to multiple recipients using Python smtplib, we can use the sendmail method.