SoFunction
Updated on 2024-11-18

Python data acquisition to implement image data extraction

For example, if I upload a random picture from my cell phone to my computer, I can get all the information about this picture through python. If it's a photo taken by a digital camera, we can find the time the photo was taken, the latitude, longitude, and altitude of the photo in the properties.
So what is the purpose of this information?

There are many features...such asUser profiling, customer information taggingWait, the season in which the user prefers to take photos, the point in time, the parameter indicators of the camera used can reflect theA person's monetary situationFor the content of its shot, we can extract the content information of the photo by means of AI, so as toDetermining a person's interests

在这里插入图片描述

First, the use of exifread to extract the picture EXIF information

exifreadPresentation:

EXIFInformation, which stands for Exchangeable Image File, is specially set up for digital camera photos to record attribute information and shooting data of digital photos.EXIF can be attached to theJPEGTIFFRIFFThis section describes how to add the contents of the digital camera recording information and the index map or the version information of the image processing software to the file.

First, you have to install theExifRead

pip3 install ExifRead
pic=r'D:\S072003Python\input\test\'

import exifread
f = open(pic, 'rb')
tags = exifread.process_file(f)
print(tags) # With camera model, shooting time, latitude and longitude, etc.

在这里插入图片描述

tags

在这里插入图片描述

print(tags) and tags get data in a different format.

tags['Image ImageWidth']
tags['Image ImageLength']
tags['Image ExifOffset']
tags['Image Orientation']
tags['Image DateTime']
tags['EXIF WhiteBalance']
tags['EXIF ISOSpeedRatings']
tags['EXIF FocalLength']
tags['EXIF Flash']
tags['EXIF LightSource']
exifcolumns=['Image ImageWidth','Image ImageLength','Image ExifOffset','Image Orientation','Image DateTime','EXIF WhiteBalance','EXIF ISOSpeedRatings','EXIF FocalLength','EXIF Flash','EXIF LightSource'] # Wrap all the data to be extracted in a list
for i in range(len(exifcolumns)):
    print(tags[exifcolumns[i]]) # Use a loop to get all the data

在这里插入图片描述

Second, the loop traverses the picture information

Task: Get a one-time access to the following images of"Image ImageWidth"Message. Just write a loop:

在这里插入图片描述

import exifread
import os
import pandas as pd 
import glob 
pic_list=(r'C:\Users\Lenovo\Pictures\Saved Pictures\*.jpg')  # How to set the data format if it is png,jpeg,bmp?

for i in pic_list:
    fr=open(i,'rb')
    tags=exifread.process_file(fr)
    if "Image ImageWidth" in tags:  # Conditional judgment, because not all photos have "Image ImageWidth".
        print(tags["Image ImageWidth"])   
# Warp and longitude acquisition
import exifread
import os
import pandas as pd 
import glob 
pic_list=(r'C:\Users\Lenovo\Pictures\Saved Pictures\*.jpg')  

latlonlists=[]
for i in pic_list:
    fr=open(i,'rb')
    tags=exifread.process_file(fr)
    if "GPS GPSLatitude" in tags:  # Conditional judgment, because not all photos have "Image ImageWidth".
        # Dimension conversion
        lat_ref=tags["GPS GPSLatitudeRef"]
        lat=tags["GPS GPSLatitude"].printable[1:-1].replace(" ","").replace("/",",").split(",")
        lat=float(lat[0])+float(lat[1])/60+float(lat[2])/3600
        if lat_ref  in  ["N"]:    # Indicates data for the southern hemisphere
            lat=lat*(-1)
        # Longitude shift
        lon_ref=tags["GPS GPSLongitudeRef"]
        lon=tags["GPS GPSLongitude"].printable[1:-1].replace("","").replace("/",",").split(",")
        lon=float(lon[0])+float(lon[1])/60+float(lon[2])/3600
        if lon_ref  in  ["E"]:    # Indicates data for the Western Hemisphere
            lon=lon*(-1)
            
        print("Dimension:",lat,"Longitude:",lon)
        latlonlist=[lat,lon]
        (latlonlist)  

This article on Python data acquisition to achieve image data extraction is introduced to this article, more related Python image data extraction content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!