Extracting EXIF (Meta)Data From Images With Python by steempytutorials

View this thread on steempeak.com
· @steempytutorials · (edited)
$27.91
Extracting EXIF (Meta)Data From Images With Python
<center>![banner.png](https://cdn.steemitimages.com/DQmXUKa4wD5mekkSu2DesdjCwviNPrLYmLmVngcZfqcqRsy/banner.png)</center>

This tutorial is the first part of a series where different aspects of programming with Python are explained, using Python and public libraries to make useful tools.

---

#### Repository
https://github.com/ianare/exif-py

#### What will I learn

- Install ExifRead
- What is EXIF data
- Extract EXIF data from images
- Process EXIF data
- Analyse images

#### Requirements

- Python3.6
- ExifRead

#### Difficulty

- basic

---

### Tutorial



#### Setup
Download the files from [Github](https://github.com/Juless89/python-tutorials/tree/master/EXIF). There are 6 files. The code is contained in `exif.py` the other 5 files are images which can be used to test the code on. `exif.py` takes 1 argument which is the filename for the image. In addition, the argument `analysis` is used to perform a model analysis on all the images.

Run scripts as following:
`> python exif.py 1.jpg`

#### Install ExifRead
Installation of the required package is recommended using the PyPI package manager.

```
pip install exifread
```

#### What is EXIF data

Also referred to as `metadata`. When taking a photo the device stores a lot of additional information inside the image file. Information like the `datetime` and `location` or more specific data like the`ISO`, `aperture` and `focal length`.  This information can be used by photographers when comparing images to see which settings worked best, learning from other photographers by looking at which settings they used or data analysis.

<center>
![Screenshot 2018-07-10 16.19.36.png](https://cdn.steemitimages.com/DQmPKr1hAivopVWxt4ArC9pAKai8Z1RLEkJM8HsNuu9CBq6/Screenshot%202018-07-10%2016.19.36.png)
(Right click->get info (for Mac))
</center>

#### Extract EXIF data from images
Processing the image with `exifread.process_image()` returns a dictionary containing all the `EXIF` data.
```
def process_image(filename):
    # Open image file for reading (binary mode)
    f = open(filename, 'rb')

    # Return Exif tags
    tags = exifread.process_file(f)
    return tags
```
<br>
Quite a lot of information can be stored which also adds to the image file size. Photographer can choice to omit` EXIF` data to to save on bandwidth or to keep information about their ways of working private.
```
{
	'Image Make': (0x010F) ASCII = SONY @ 110,
	'Image Model': (0x0110) ASCII = ILCE - 7 M3 @ 116,
	'Image XResolution': (0x011A) Ratio = 240 @ 126,
	'Image YResolution': (0x011B) Ratio = 240 @ 134,
	'Image ResolutionUnit': (0x0128) Short = Pixels / Inch @ 66,
  .
  .
  .
}
```
[Pastebin to the entire EXIF](https://pastebin.com/QwcEJvwE)


#### Process EXIF data
Photographers often share the main settings used for capturing a photo and what gear was used. For this example the data will be extracted from the the image and put into a `html table` for easy sharing. The same works for applying the code on a downloaded image from the web, learning more how the image was taken. The code can easily be adjusted depending on which data is needed.

```
def create_table(tags):
    # extract required data
    brand = tags['Image Make']
    model = tags['Image Model']
    date = tags['Image DateTime']
    shutterspeed = tags['EXIF ExposureTime']
    focallength = tags['EXIF FocalLength']
    aperture = tags['EXIF FNumber']
    iso = tags['EXIF ISOSpeedRatings']
    lens = tags['EXIF LensModel']

    # generate a htlm table
    print('<table>')
    print(f'<tr><td>Settings</td><td><b>ISO {iso} {focallength} ' +
          f' mm f/{aperture} {shutterspeed} sec </b></td></tr>')
    print(f'<tr><td>Camera</td><td><b>{brand} {model}</b></td></tr>')
    print(f'<tr><td>Lens</td><td><b>{lens}</b></td></tr>')
    print(f'<tr><td>Date</td><td><b>{date}</b></td></tr>')
    print('</table>')
```

#### Analyse images
Imagine having a batch of images and you want to know which camera was used the most often. By looping through all the images, creating keys for unique camera `models` with a value indicating their `frequency` this can be easily answered.

```
def perform_analysis():
    # Empty dict
    camera_models = {}

    # Loop through images 1-5.jpg
    for image_num in range(1, 6):
        filename = f'{image_num}.jpg'
        # Extract str from the IfdTag
        model = str(process_image(filename)['Image Model'])

        # Fill dict with unique models and increase frequency
        if model in camera_models:
            camera_models[model] += 1
        else:
            camera_models[model] = 1

    print(camera_models)
```

#### Running the code
Five photos come with the code, running `exif.py` on the first three of them shows how useful `EXIF` data is when working with different cameras and different lenses.

---

![1.jpg](https://cdn.steemitimages.com/DQmZcvatWYFzKUCV46pfwiiMUEGk2Y3Bh88fNsQymrM8z2N/1.jpg)

```
python exif.py 1.jpg

```

<table>
<tr><td>Settings</td><td><b>ISO 100 192  mm f/11 3/5 sec </b></td></tr>
<tr><td>Camera</td><td><b>SONY ILCE-7M3</b></td></tr>
<tr><td>Lens</td><td><b>FE 70-200mm F4 G OSS</b></td></tr>
<tr><td>Date</td><td><b>2018:07:04 20:56:11</b></td></tr>
</table>
<br>

![2.jpg](https://cdn.steemitimages.com/DQmPKS8zxS6Pw7dQKUayb9xoeRW4TdUYSz1QmyTtX1RgtLs/2.jpg)

```
python exif.py 2.jpg

```

<table>
<tr><td>Settings</td><td><b>ISO 3200 50  mm f/16/5 1/60 sec </b></td></tr>
<tr><td>Camera</td><td><b>NIKON CORPORATION NIKON D7500</b></td></tr>
<tr><td>Lens</td><td><b>50.0 mm f/1.8</b></td></tr>
<tr><td>Date</td><td><b>2017:12:17 17:24:47</b></td></tr>
</table>
<br>

![3.jpg](https://cdn.steemitimages.com/DQmPfTJ1JNtCb4UBJk43isNvG9EwksMRBLcg56zkeqCxog2/3.jpg)

```
python exif.py 3.jpg

```

<table>
<tr><td>Settings</td><td><b>ISO 100 32  mm f/25 13 sec </b></td></tr>
<tr><td>Camera</td><td><b>NIKON CORPORATION NIKON D3400</b></td></tr>
<tr><td>Lens</td><td><b>18.0-140.0 mm f/3.5-5.6</b></td></tr>
<tr><td>Date</td><td><b>2018:05:11 01:46:48</b></td></tr>
</table>
<br>

Using the argument `analysis` will perform the analysis, looping over all the images and counting which `model `camera was used with what `frequency`.

```
python exif.py analysis

{'ILCE-7M3': 3, 'NIKON D7500': 1, 'NIKON D3400': 1}

```

---

The code for this tutorial can be found on [Github](https://github.com/Juless89/python-tutorials/tree/master/EXIF)!

This tutorial was written by @juliank.
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
post_id56,091,669
authorsteempytutorials
permlinkextracting-exif-meta-data-from-images-with-python
categoryutopian-io
json_metadata{"links":["https:\/\/github.com\/ianare\/exif-py","https:\/\/github.com\/Juless89\/python-tutorials\/tree\/master\/EXIF","https:\/\/pastebin.com\/QwcEJvwE"],"users":["juliank"],"app":"steemit\/0.1","image":["https:\/\/cdn.steemitimages.com\/DQmXUKa4wD5mekkSu2DesdjCwviNPrLYmLmVngcZfqcqRsy\/banner.png"],"tags":["utopian-io","tutorials","python","programming"],"format":"markdown"}
created2018-07-10 15:05:36
last_update2018-07-10 15:53:39
depth0
children14
net_rshares13,937,357,831,970
last_payout2018-07-17 15:05:36
cashout_time1969-12-31 23:59:59
total_payout_value21.268 SBD
curator_payout_value6.639 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length6,409
author_reputation31,061,485,529,806
root_title"Extracting EXIF (Meta)Data From Images With Python"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (28)
@usahil ·
Thats is amazing ☺
properties (22)
post_id56,092,090
authorusahil
permlinkre-steempytutorials-extracting-exif-meta-data-from-images-with-python-20180710t150924620z
categoryutopian-io
json_metadata{"links":[],"users":[],"app":"busy\/2.5.1","image":[],"community":"busy","tags":["utopian-io"],"format":"markdown"}
created2018-07-10 15:09:27
last_update2018-07-10 15:09:27
depth1
children0
net_rshares0
last_payout2018-07-17 15:09:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length18
author_reputation853,318,401,451
root_title"Extracting EXIF (Meta)Data From Images With Python"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@rajudas36 ·
how to work this project
properties (22)
post_id56,092,385
authorrajudas36
permlinkre-steempytutorials-extracting-exif-meta-data-from-images-with-python-20180710t151203274z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit\/0.1"}
created2018-07-10 15:12:15
last_update2018-07-10 15:12:15
depth1
children0
net_rshares0
last_payout2018-07-17 15:12:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length24
author_reputation124,292,362,915
root_title"Extracting EXIF (Meta)Data From Images With Python"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@dhimmel ·
$1.15
Nice, [I've used](https://github.com/dhimmel/fratjuice/blob/9700b638147574580200d9f1cf388b559ad5269f/generate-details.py#L29-L37) `exifread` in Python to extract JPEG metadata such as the timestamp and GPS coordinates of the photo. IIRC, I tried scikit-image, but it was overkill and much less straightforward.
👍  ,
properties (23)
post_id56,096,173
authordhimmel
permlinkre-steempytutorials-extracting-exif-meta-data-from-images-with-python-20180710t154813923z
categoryutopian-io
json_metadata{"app":"busy\/2.5.1","tags":["utopian-io"],"image":[],"format":"markdown","community":"busy","users":[],"links":["https:\/\/github.com\/dhimmel\/fratjuice\/blob\/9700b638147574580200d9f1cf388b559ad5269f\/generate-details.py#L29-L37"]}
created2018-07-10 15:48:15
last_update2018-07-10 15:48:15
depth1
children3
net_rshares577,050,500,135
last_payout2018-07-17 15:48:15
cashout_time1969-12-31 23:59:59
total_payout_value1.148 SBD
curator_payout_value0.005 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length310
author_reputation33,884,415,613,920
root_title"Extracting EXIF (Meta)Data From Images With Python"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (2)
@juliank ·
$0.91
Nice! I also found exifread very straightforward to work with. Do not know about scikit-image, did not have to look further as this worked quite well ;p 

I tried downloading images from an url in python and extracting the EXIF data from the downloaded image, but could not get this to work. For some reason the EXIF data got omitted, while downloading the image manually did make it work.
👍  
properties (23)
post_id56,097,034
authorjuliank
permlinkre-dhimmel-re-steempytutorials-extracting-exif-meta-data-from-images-with-python-20180710t155659964z
categoryutopian-io
json_metadata{"app":"steemit\/0.1","tags":["utopian-io"]}
created2018-07-10 15:57:00
last_update2018-07-10 15:57:00
depth2
children2
net_rshares459,112,011,405
last_payout2018-07-17 15:57:00
cashout_time1969-12-31 23:59:59
total_payout_value0.831 SBD
curator_payout_value0.084 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length389
author_reputation117,790,729,406,006
root_title"Extracting EXIF (Meta)Data From Images With Python"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@dhimmel ·
$1.38
Hmm. I feel like it should be possible to extract EXIF metadata from an image URL. The `exifread.process_file` [source code](https://github.com/ianare/exif-py/blob/fd12746a9b000b192ebbbc056f0179cec0d51c7f/exifread/__init__.py#L19) seems to expect a bytestream file-like object. If you're using `requests`, did you try wrapping `response.content` in `io.BytesIO` before passing it to `exifread.process_file`?
👍  ,
properties (23)
post_id56,098,227
authordhimmel
permlinkre-juliank-re-dhimmel-re-steempytutorials-extracting-exif-meta-data-from-images-with-python-20180710t160808162z
categoryutopian-io
json_metadata{"app":"busy\/2.5.1","tags":["utopian-io"],"image":[],"format":"markdown","community":"busy","users":[],"links":["https:\/\/github.com\/ianare\/exif-py\/blob\/fd12746a9b000b192ebbbc056f0179cec0d51c7f\/exifread\/__init__.py#L19"]}
created2018-07-10 16:08:06
last_update2018-07-10 16:08:06
depth3
children1
net_rshares692,317,423,956
last_payout2018-07-17 16:08:06
cashout_time1969-12-31 23:59:59
total_payout_value1.373 SBD
curator_payout_value0.009 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length407
author_reputation33,884,415,613,920
root_title"Extracting EXIF (Meta)Data From Images With Python"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (2)
@freyman ·
$0.03
Thanks a lot for your hard work.
I'm not your best student, but your post are very useful for me for approach to python and the steem blockchain.
and also exif and a lot more, of course...  !!
**;-)**
Regards.
👍  
properties (23)
post_id56,104,871
authorfreyman
permlinkre-steempytutorials-extracting-exif-meta-data-from-images-with-python-20180710t171948060z
categoryutopian-io
json_metadata{"app":"steemit\/0.1","tags":["utopian-io"]}
created2018-07-10 17:19:48
last_update2018-07-10 17:19:48
depth1
children0
net_rshares15,297,627,646
last_payout2018-07-17 17:19:48
cashout_time1969-12-31 23:59:59
total_payout_value0.023 SBD
curator_payout_value0.006 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length209
author_reputation3,114,105,584,446
root_title"Extracting EXIF (Meta)Data From Images With Python"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@setik-001 ·
Отличная статья. Мне очень понравилось. Удачи Вам и Любви
Excellent article. I really liked it. Good luck to you and Love.
👍  
properties (23)
post_id56,116,193
authorsetik-001
permlinkre-steempytutorials-extracting-exif-meta-data-from-images-with-python-20180710t193521876z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit\/0.1"}
created2018-07-10 19:35:18
last_update2018-07-10 19:35:18
depth1
children0
net_rshares70,099,571
last_payout2018-07-17 19:35:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length122
author_reputation1,102,103,095,546
root_title"Extracting EXIF (Meta)Data From Images With Python"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@portugalcoin ·
Thank you for your contribution.

The tutorial is basic however it is interesting.

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/8/31323324).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
👍  
properties (23)
post_id56,120,515
authorportugalcoin
permlinkre-steempytutorials-extracting-exif-meta-data-from-images-with-python-20180710t203438935z
categoryutopian-io
json_metadata{"links":["https:\/\/join.utopian.io\/guidelines","https:\/\/review.utopian.io\/result\/8\/31323324","https:\/\/support.utopian.io\/","https:\/\/discord.gg\/uTyJkNm","https:\/\/join.utopian.io\/"],"tags":["utopian-io"],"app":"steemit\/0.1"}
created2018-07-10 20:34:39
last_update2018-07-10 20:34:39
depth1
children0
net_rshares4,670,714,268
last_payout2018-07-17 20:34:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length574
author_reputation214,343,891,436,406
root_title"Extracting EXIF (Meta)Data From Images With Python"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@yalzeee ·
Nicely done @steempytutorials. Although  am curious to know if the PyPI package could be available to be installed on Qpython for mobile. It would be a really valuable package while trying to write code on the go.
properties (22)
post_id56,158,160
authoryalzeee
permlinkre-steempytutorials-extracting-exif-meta-data-from-images-with-python-20180711t055341927z
categoryutopian-io
json_metadata{"users":["steempytutorials"],"app":"steemit\/0.1","tags":["utopian-io"]}
created2018-07-11 05:53:45
last_update2018-07-11 05:53:45
depth1
children0
net_rshares0
last_payout2018-07-18 05:53:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length213
author_reputation12,492,997,891,187
root_title"Extracting EXIF (Meta)Data From Images With Python"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@utopian-io ·
Hey @steempytutorials
**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
👍  
properties (23)
post_id56,234,489
authorutopian-io
permlinkre-extracting-exif-meta-data-from-images-with-python-20180711t205009z
categoryutopian-io
json_metadata{"app":"beem\/0.19.42"}
created2018-07-11 20:50:09
last_update2018-07-11 20:50:09
depth1
children0
net_rshares5,634,922,455
last_payout2018-07-18 20:50:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length308
author_reputation152,913,012,544,965
root_title"Extracting EXIF (Meta)Data From Images With Python"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@drsensor · (edited)
$3.16
Hi! Seems `exif` now become like a standard de'facto for inserting and extracting metadata from image file. Curious if it can insert or extract metadata from `.gif`?
There is also technique called steganography which can hide information like [text](https://github.com/DominicBreuker/stego-toolkit) or [image](https://github.com/kelvins/steganography) inside the original image, what do you think if using that instead of exif? (by the way I hid some image in my steemit profile picture xD)
👍  ,
properties (23)
post_id57,654,521
authordrsensor
permlinkre-steempytutorials-extracting-exif-meta-data-from-images-with-python-20180725t095258880z
categoryutopian-io
json_metadata{"app":"steemit\/0.1","links":["https:\/\/github.com\/DominicBreuker\/stego-toolkit","https:\/\/github.com\/kelvins\/steganography"],"tags":["utopian-io"]}
created2018-07-25 09:53:00
last_update2018-07-25 09:54:21
depth1
children1
net_rshares1,532,873,119,541
last_payout2018-08-01 09:53:00
cashout_time1969-12-31 23:59:59
total_payout_value2.602 SBD
curator_payout_value0.560 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length490
author_reputation17,692,034,507,824
root_title"Extracting EXIF (Meta)Data From Images With Python"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (2)
@utopian-io ·
Hey @drsensor
Here's a tip for your valuable feedback! @Utopian-io loves and incentivises informative comments.

**Contributing on Utopian**
Learn how to contribute on <a href="https://join.utopian.io">our website</a>.

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for Utopian Witness!</a>
properties (22)
post_id57,656,083
authorutopian-io
permlink20180725t101427363z
categoryutopian-io
json_metadata{"tags":["utopian.tip"],"app":"utopian-io"}
created2018-07-25 10:14:27
last_update2018-07-25 10:14:27
depth2
children0
net_rshares0
last_payout2018-08-01 10:14:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length408
author_reputation152,913,012,544,965
root_title"Extracting EXIF (Meta)Data From Images With Python"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@davidwilliams ·
Do you know a way to get at the exif data for cr2 raw files?
properties (22)
post_id63,163,954
authordavidwilliams
permlinkre-steempytutorials-extracting-exif-meta-data-from-images-with-python-20180924t015551807z
categoryutopian-io
json_metadata{"app":"steemit\/0.1","tags":["utopian-io"]}
created2018-09-24 01:55:51
last_update2018-09-24 01:55:51
depth1
children0
net_rshares0
last_payout2018-10-01 01:55:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length60
author_reputation422,236,288,651
root_title"Extracting EXIF (Meta)Data From Images With Python"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000