How To Use Google Places Photo API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to another Python tutorial we are continuing our exploration of the Google Maps API so in our last video we saw how we can do a basic search in order to go and get some information about particular places on Google Maps well we're gonna expand that and we're gonna now take a little segment that we finished off in the last day where we were talking about the details and we're gonna go and see how we can go and get photos for a particular business so sometimes you want to go and get the photos for a particular business or place how would we do that well it involves us kind of rewriting our script a little bit and then also it also requires us to use a different end point so just to kind of you know go over what you're seeing right now if you've seen the other videos it should look all familiar I've just deleted a bunch of the code all we're doing is we are importing our Google Maps library we're getting our API key we're defining our API key we're defining our client so this is our way of how we're gonna go and make requests to then get information sent back to us and then we do a nearby search so we pass through a location in a latitude longitude format we specify a radius so how far out do we want to look and then we have some optional parameters so do they have to be open now no they don't they can be closed and then also they can be they have to be a cafe so that's the category we are searching for so I'm gonna take this script and we're just gonna modify it a little bit but what we're gonna do first is we're gonna jump back into the documentation to go over you know the kind of the endpoint that we're planning to use okay so in my last video I did a couple things first I showed you how to enable the API I'm not going to do that in this video if you want to see how to do that please watch the last video where I cover that I'll make sure to put a link in the description ok and then I also went over the developer counsel and then some of the documentation so if you want to see the places API you can just go to developers google.com and then places web service intro and then he will be taken here this is the main overview and we covered a place search and place details so now we're gonna go to places photos so you just click that and a new page will open up for you and it will tell you how to use this particular endpoint and then basically it just goes over you know hey what are you looking for you know what are you gonna get back what we're gonna do is we're going to take information that we're gonna get from our places details and we're going to take that information and we're gonna go make another request so really if you think about it we're gonna be doing three different requests we're gonna be doing a search we're gonna go get the details and then we're gonna go get the photos so there's three different requests that we have to do in order to achieve this so that's just for you guys to keep in mind this can kind of add up if you're doing a lot of requests so you just gotta keep that in mind and then when we make our request for a photo there are certain parameters that we have to pass through now if you go down and you scroll a little bit you'll see the actual request section and it will tell you the required framers well one is your API key that's taken care of by the client one is called a photo reference so this is just a photo ID that you want actually go and get and the next are the max height or the max width so this is basically saying hey how wide and how high do you want this photo to look so basically its resolution and then you're gonna get back a raw image file so there's really just no like JSON string or anything like that it's actually like raw image data and what we're gonna have to do is take that image data put it into a file and then we're gonna open that file so again hopefully that didn't confuse too many people but what we're doing is we're gonna make a request using these different parameters and we're getting this photo reference ID from the place detail so this is the next page where I kind of explained this in the last video they don't show you in the actual description for the response but if you scroll down a little bit and you go to place detail results you'll go down to P photo you'll get photos so it's simply an array or a list and then in that list there's four different components that are sent back to you so there's a photo reference which is the ID the height and then the width so again this point nothing too complicated hopefully and we're just gonna go and get the ID from that result so now that we've done that I'm going to jump back individual studios and we'll actually write the code okay so here we're doing a search now we're gonna go and get the place details so we're gonna first we got to define some stuff so we're gonna have to take our place results and we're gonna have to go and get an actual you know ID and so how do we get that well I'm just gonna write it a little bit more short handily I'm just gonna do my place ID so it's just gonna store it in a variable I'm gonna go my places result and then what I'm gonna do is I'm gonna go in the results sections of that actual dictionary of information so technically it's just a dictionary you know Python dictionary and what we can do is we can access the key and it will return the information for that particular section of the dictionary so here I'm going into the results section and then from there I want the first place so I'm just gonna pass through 0 and then I'm gonna get the actual place ID okay so I'm going into my results I'm getting my first result and then I'm getting my place ID and just so you believe me I'm going to print out my place ID perfect so you can see it's simply a place ID if you wanted the next one you would just pass through one two three whatever you want to do or you can just put it into a loop so this section right here get the place ID and then we're gonna define our fields that we want to go and get back from our actual request well I want to only get the photos back so I'm going to define my fields and I'm gonna set that equal to my fields and it's simply going to be a list and it's gonna be photo so this is the fields that I want to turn back and only want the photos and then I'm gonna actually make my request so make a request for the details and so I'm gonna say place details equals G maps place and then place ID equals my place ID and then fields close equals my fields so two parameters place ID and fields and then we're gonna get something sent back to us we can just print that out so let me print out what sent back to us okay so a lot of different information as you can tell so if you look right up here at the top you see HTML attributions the result so this is the information we want and then in all it gave us was basically a dictionary object that has all the photos so we're gonna access this particular key and we're gonna get all the different pieces of information now in our particular example we're just looking for this little piece of information photo reference which gives us this huge key that is just astronomically long okay so sorry ready to adjust myself let's define our photo ID photo underscore ID equals place details and then I want to go into the results and then I want to go into the photos and then I want the first photo and then I want the photo reference so you know there's different ways to write this I'm just gonna make it again this consolidate as I can but you know keep in mind this isn't the only way to write this but now we have a photo ID so let's make sure we can see it before we make next request hopefully we should see a very long number well I had an S at the end that's why okay so here's our photo ID now that we have a photo ID we can actually make a request for that photo so we're pretty much almost there so we're going to define our next parameters we have our photo ID now we just need to specify a width and a height so I'll just put that right up here so it's kind of next to it and I want it a 400 by 400 so I define those parameters and I'll put it into a little section define dimensions and then we can go and get the raw image data so we're gonna make a request and we're gonna store in all that information in this variable called raw image data we're gonna go into G Maps we're gonna go into places underscore photo and then the first parameter is photo reference and we're gonna pass through our photo ID because that's what we got to pass through in that one and then we have something called max height well that's simply just our max height I mean not that so our photo height and then that it's gonna be our max width which is simply our photo with so define a request for the image data so three parameters an ID a height and a width now what sent back to us I'll show you it but basically it's just a bunch of raw image data but it's not gonna look like that type image data okay so it's a generator object response iterator content so in other words if we want to actually access this content we have to iterate through it and they we're going to iterate it through chunks and we're gonna take each one of those chunks we're gonna save it to a file and then we're gonna open that new file so let's dump all the information in a file and let us we're gonna open a new file assign a file to a variable but basically we're opening a file and we're gonna open and I'll put it something simple my image and then we'll do a jpg and then I want it to be open okay so that's that component so we've opened the file now what we're gonna do is we're gonna basically dump each chunk into that particular file so create a new file and then dump each chunk into the file and then we're gonna say for chunk in raw image data if there's a chunk and simply write that to the file so right the chunk to the file perfect and then from there you can close the file perfect so let's double check this you made our request we got our image data we're gonna create a new file and we're gonna take all that information and then put it into a file and then we're closing that file so we can save it so I'll run it perfect looks like it went smoothly and then if I go in my little directory you can see my image perfect now if I want to open this image from Python well I got to do one more thing then so I'm actually gonna have to import a library and this library allows us to easily you know in a sense kind of open you know different files that are related to images I have it on another thing but basically it's called pillow and from pillow we're gonna import the image module and then if you want to actually install it you can just do hip install pillow that will install your library sometimes it comes sometimes it doesn't so depending on your system you have to install it but once it's installed you import the image module and then from there you can actually open the image so that's all you need to open it and then to open it from within Python I'm going to create a new variable set it equal to image and I'm gonna call the image module I'm gonna call the open method and then I just need to pass through really the name of my file well geez what is going on can't select it for some reason perfect and then we're gonna call the show method so if we run it now we should see it kind of just pop up automatically perfect so you can see that the photo was opened and you can clearly see it all the information is there for us you know you could technically if you wanted to you could do this in a loop you know if you wanted you could get each photo for each individual place really what you would be doing and kind of already have it laid out for you guys all you would do is really you're making your request for the results like you were doing before and then you're getting each place ID from without results set you're defining your fields you're getting that information so you're getting the detailed information and then you're gonna loop through each of the photos and the detailed information again you're gonna get the ID you're going to define your width and your height you're gonna make a request for that information and then you're gonna save that information to a file now you have to obviously change the file name or it's just going to write over the existing file so and then that's kind of all exactly the same so here again you're just taking all the chunks of data and you're putting it in the file and then you're also what is it you're saving it so that way it's it's in we're actually opening the file at that point so this is kind of if you wanted to do it within a loop again this is how you would lay it out so again more of a simple example nothing too super complicated just at least showing you how you can leverage it all right but that does it for today's video so if you have any questions about how to get photos or any of that information from Google you know please put them down in the comments below also if you could make sure to LIKE the video we always appreciate the support and if you're not already make sure to subscribe to the channel so that way you get regular updates as we release new videos so after this one we're hoping to move into directions so the Google Maps directions API it's actually kind of fun I was actually I'm a little bit of fun with it I think you guys will too because it comes in handy might not necessarily have a particular use for it yet but I think it's cool that you can just find directions writing in a simple script so cool thanks again for watching guys we'll see in the next video
Info
Channel: Sigma Coding
Views: 7,688
Rating: undefined out of 5
Keywords: Coding, Coding Tutorial, Tutorial, Programming, Python, google API, Places API, Photos API, Python Tutorial, How To Python, Python Coding, Python API, Python API Tutorial, How to use Google Places API, How To Use Google Places Photos API, Google
Id: t2BYThHlk3k
Channel Id: undefined
Length: 16min 56sec (1016 seconds)
Published: Sun Jan 13 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.