Python - get user details based on photo information (wechat sends original picture or divulges location information)

Keywords: Python Attribute JSON

Preface

The text and pictures of this article are from the Internet, only for learning and communication, not for any commercial purpose. The copyright belongs to the original author. If you have any questions, please contact us in time for handling.

By Mona Lippo

PS: if you need Python learning materials, you can click the link below to get them by yourself

http://note.youdao.com/noteshare?id=3054cce4add8a909e784ad934f956cef

Preface

Some media exposed that wechat sent the original picture or there was a risk of leaking location information. In response, Tencent's wechat team Weibo announced in a statement on December 1 that the photos sent by the circle of friends have been automatically compressed by the system without information such as location. If you are worried, you can post them after finishing the picture, as shown in the following figure:

Wechat team mentioned Exif, what is Exif?

Exchangeable image file format (Exif) is specially set for the photos of digital cameras. It can record the attribute information and shooting data of digital photos.

Exif was originally developed by Japan Electronics Industry Development Association in 1996, version 1.0. In 1998, upgrading to 2.1 added support for audio files. In March 2002, version 2.2 was published.

Python Library

Here we need two libraries of Python, one is to read the Exif information, the other is to get the detailed address information according to the longitude and latitude of geopy;

The installation is as follows:

pip3 install exifread
​
pip3 install geopy

 

Python source code

 1 import exifread
 2 import json
 3 import urllib.request
 4 import sys
 5 from geopy.geocoders import Nominatim
 6  7 # Get photo details
 8 def get_img_infor_tup(photo):
 9     img_file = open(photo, 'rb')
10     image_map = exifread.process_file(img_file)
11 12     try:
13         #Longitude of picture
14         img_longitude_ref = image_map["GPS GPSLongitudeRef"].printable
15         img_longitude = image_map["GPS GPSLongitude"].printable[1:-1].replace(" ","").replace("/",",").split(",")
16         img_longitude = float(img_longitude[0])+float(img_longitude[1])/60+float(img_longitude[2])/float(img_longitude[3])/3600
17         if img_longitude_ref != "E":
18             img_longitude = img_longitude * (-1)
19 20         #Latitude of picture
21         img_latitude_ref = image_map["GPS GPSLatitudeRef"].printable
22         img_latitude = image_map["GPS GPSLatitude"].printable[1:-1].replace(" ","").replace("/",",").split(",")
23         img_latitude = float(img_latitude[0])+float(img_latitude[1])/60+float(img_latitude[2])/float(img_latitude[3])/3600
24         if img_latitude_ref != "N":
25             img_latitude = img_latitude*(-1)
26 27         #Time taken
28         img_create_date = image_map["EXIF DateTimeOriginal"].printable
29 30         img_file.close()
31 32         # Return latitude and longitude tuple
33         return img_longitude, img_latitude, img_create_date
34 35     except Exception as e:
36         print('ERROR:Not included in picture Gps information')
37 38 # Obtain detailed information according to latitude and longitude
39 def get_detail_infor(lat, lon):
40     reverse_value = str(lat) + ', ' + str(lon)
41     geolocator = Nominatim()
42     location = geolocator.reverse(reverse_value)
43 44     print('Longitude and latitude information of photos:')
45     print((location.latitude, location.longitude))
46 47     print('Photo address information:')
48     print(location.address)
49     
50     print('Full information of the photo:')
51     print(location.raw)
52 53 if __name__ == '__main__':
54     infor_tup = get_img_infor_tup('./image/IMG_2174.JPG')
55     get_detail_infor(infor_tup[1], infor_tup[0])

Posted by hubardz on Tue, 17 Dec 2019 00:06:51 -0800