GPS Location From Image Metadata in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back in today's video we're going to learn how to display gps information that we extract from an image's metadata using python so let's get right into it [Music] all right so in this video i'm going to use a jupyter notebook inside of jupyter lab just because it's a little bit more interactive and easier to work with however as always you can use whatever development environment you're the most comfortable with because the code stays the same it doesn't matter if you use pycharm vs code the default python idle bim or a jupyter notebook i just like it a little bit more because it's more interactive so what are we going to do in this video we're going to take a simple image like this one here and we're going to extract the gps information that is uh contained in that image so we have gps information inside of that image that we cannot see right away uh we have to look at the metadata but this gps location then tells us exactly where this image was taken at and this is not a special image this is not a one in a thousand image most of your images on your phone will have this so if you have your phone in uh in the default settings if you didn't change any gps tagging settings proactively if you take an image with your phone usually it's going to add a location tag to that image so that for example your phone can can tell you when you uh were in spain for a week it can tell you here we have a collection of a couple of images that were taken in spain maybe you're interested in that those are the features that are enabled by the gps location tagging for example but at the end of the day if you take an image with your phone most likely it will have a gps location tag and if you send that exact image to someone else they will be able to see that location tag if they extract it of course so they can see where this image was taken at and maybe you don't want them to know so maybe you want to be a little bit careful with that uh one thing that i need to mention though is that when you take an image like this one here and you upload it to instagram you upload it to facebook or even to whatsapp or any messenger the image goes through an algorithm that usually compresses the image changes the image and so on so at the end of the day when you send that image to someone they don't receive the exact same image with the metadata if i take this image full of metadata and upload it to instagram it is not the same image the location tag will not be part of the uploaded image so you cannot just download the image and then you see the location tags because you would need to have the original image so i would have to send it through dropbox google drive or something like that or usb flash drive whatever but you cannot just send it via whatsapp and then you have the location tags not it doesn't work like that but if you have an image however you get it with a location tag you can extract it using python and it's actually kind of simple to do that so we're going to need a couple of libraries here we're going to open up a command line and we're going to type pip install first of all we're going to need pillow to actually load the image and get the metadata but then we're also going to need uh geopi to get the actual address once we have the gps location and we're also going to need um gm plot was it gm plot let me just look it up again yeah gm plot is the library this is going to visualize our location so the first thing we want to do is we want to say import pill dot image and we want to say image equals pill dot image open and we want to open the sample.jpg file that i have here now so this is the image and now we can get the metadata now i have a video on this channel already on the metadata in general so how to get the metadata overall and how we did that is we said import pill dot exif tags and then we said exif equals and we had this dictionary comprehension here where we said we have pill dot exif tags dot tax which is a dictionary here we get uh the key and this points to a value um and this is done for k and v in pill oh actually not pill sorry image dot get exif which is actually an underscore function so get exif and then from that we get the items so we get key value pairs and we're gonna say if the key is part of pill dot exif tax dot tax so that is that if i print this you can see here we have a bunch of things and right away in the beginning we have gps info and we have these uh values here so if i go ahead and say exif gps info like this here if i access this key you can see we have a bunch of values here and what's interesting to uh to us is this north with the coordinates and east with the coordinates those are the values that we're interested in so what we're going to do now is we're going to extract them we're going to say that north is going to be equal to exif gps info 2 because that's the key as you can see here and then we're going to do the same thing for east but east is 4. so when i now print this you can see this is north and this is east now don't ask me about the exact format what means what i'm not a gps expert but this is a format that we cannot work with in gm plot so we need to turn this into latitude and longitude so we need to take these values and convert them again don't ask me what the difference is and what it means i haven't read into that i'm interested in the coding part here in the metadata part but i have figured out a formula through googling so what we do here is we say lat equals and now we take north zero so the first the first value of 43 in this case we multiplied by 60 i think this is something with minutes and seconds i i read something about that but again don't ask me about the details here so we multiply with 60 we add to that north 1 times 60 we put that in parentheses as well and then we add north 2 without multiplying and then once we have that the result is divided by 60 and divided by 60 again so this is because we divide minutes and seconds i think as far as i understood it and for the longitude we just say the same thing with east so there you go so now if i print the value you can see it's some funny fraction we don't want to have a fraction so what we do is we say lat long equals float flat float long so that we get actual floating point numbers that we can work with as you can see here so those are the actual coordinates those are the actual things that we need to display to display the location let me just see one thing because i have an extra parent i have an extra bracket actually in my prepared code so i have one here oh yeah sorry i thought that the values were a little bit off but we need to i'm not sure if actually this changes anything no it actually doesn't change anything so let me just double check if that's correct one here actually no here we need it sorry i messed this up here i just have double brackets now there you go those are the values that you want to have now it's correct so you want to have four parentheses here when i have one closing bracket here one closing bracket here one here and one here and then you divide by 60 divided by 60 again then you get these coordinates okay so this was a little mistake but now we have the coordinates and all we need to do to visualize them is we need to say from gm plot import gm plot and then we say gmap equals gm plot dot google map plotter lat long and then we need to um specify a zoom assume 12 is a good zoom to get an overview so this is not a coordinate this is just how much you zoom into the location but you can then zoom out or in and out also interactively then we're going to say gmap marker at lat long we want to have a corn flower blue which is just a little pin that is going to be there in that color and then we're going to say gmap draw location dot html for example this is going to export an html file now let me get out of the directory and back in if i now double click here trust html you can see that this is the location that this image was taken at so you can see it's somewhere in italy uh near florence in in this this city here i don't know how it's pronounced and not italian but essentially this is the exact location at least the exact location of the data now we don't know if the phone for example saves uh or stores or tacks with an approximate location or with the exact location but that's definitely the location that the phone um put into that image so if we want to now get an actual address what we can do is we can say from geopi dot uh geocoders we want to import nomi natim [Music] and then we're going to say geolog equals nomen atom user agent is going to be get lock and then we're going to say location name is going to be equal to geo lock dot reverse and we're gonna pass here as an string lat long there you go then i'm gonna print the location name dot address and this is in this case where this image was taken at this is just some google image now so don't uh don't worry this is not some personal information this is just from from a block or something i think um but yeah if you want to also open it automatically you can also say import web browser i think this is part of the core python stack i'm not sure and then you can just say web browser dot open [Music] then location html and you can open this in a new tab so new equals two and then it opens this up in a new tab and you can see that you can go around here and look at the location so that's how you do that in python you just take the metadata you take the gps information you uh convert it into latitude and longitude and then essentially you can just visualize it and you can also get the information as text form now i recommend you try this with your own image uh just take your phone make an image of something uh make a photo of something or take some from your vacation and see if your phone gives you that information just pick your phone connect it to your computer or upload to drive download the image try to do the exact same thing here on the image and see if your address is revealed in the python code so that's it for today's video i hope you enjoyed it and hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below also don't forget to subscribe and hit the notification bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 23,061
Rating: undefined out of 5
Keywords: python metadata, python, metadata, meta data, exif metadata, exif data, exif, python exif, python exif gps, image gps exif, image metadata gps, image location, image gps, image exif gps
Id: BpE9mk1FeJ4
Channel Id: undefined
Length: 11min 48sec (708 seconds)
Published: Fri Jul 22 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.