Handling image from Google Maps APIs (staticmap) - python

I am trying to get an image from GoogleMaps APIs, more precisely from the staticmap API.
The problem is that in other APIs from GoogleMaps you can choose wether you want your info from the API in JSON or XML, but with staticmap (which returns an image) it seems you can't.
So I don't know how to handle the image provided by the URL since I don't know how it is coded.
This is what I´m trying to do:
import requests
url = ("https://maps.googleapis.com/maps/api/staticmap?size=400x400path=weight:3%7Ccolor:orange%7Cenc:polyline_data")
response = requests.get(url)
print(response.json())
Given that the info is probably not in Json it raises the following error:
ValueError: Expecting value: line 1 column 1 (char 0)
Hope you've got any advice about how to turn the response into something usable.

ummmm... ok, you are thinking too much.
staticmap (which returns an image)
Yes, since you are right, so this is what you have put it <img src="here"/>:
Following is a demo of it. I used the example from the documentation.
<img src="https://maps.googleapis.com/maps/api/staticmap?size=400x400&path=weight:3%7Ccolor:orange%7Cenc:_fisIp~u%7CU}%7Ca#pytA_~b#hhCyhS~hResU%7C%7Cx#oig#rwg#amUfbjA}f[roaAynd#%7CvXxiAt{ZwdUfbjAewYrqGchH~vXkqnAria#c_o#inc#k{g#i`]o%7CF}vXaj\h`]ovs#?yi_#rcAgtO%7Cj_AyaJren#nzQrst#zuYh`]v%7CGbldEuzd#%7C%7Cx#spD%7CtrAzwP%7Cd_#yiB~vXmlWhdPez\_{Km_`#~re#ew^rcAeu_#zhyByjPrst#ttGren#aeNhoFemKrvdAuvVidPwbVr~j#or#f_z#ftHr{ZlwBrvdAmtHrmT{rOt{Zz}E%7Cc%7C#o%7CLpn~AgfRpxqBfoVz_iAocAhrVjr#rh~#jzKhjp#``NrfQpcHrb^k%7CDh_z#nwB%7Ckb#a{R%7Cyh#uyZ%7CllByuZpzw#wbd#rh~#%7C%7CFhqs#teTztrAupHhyY}t]huf#e%7CFria#o}GfezAkdW%7C}[ocMt_Neq#ren#e~Ika#pgE%7Ci%7CAfiQ%7C`l#uoJrvdAgq#fppAsjGhg`#%7ChQpg{Ai_V%7C%7Cx#mkHhyYsdP%7CxeA~gF%7C}[mv`#t_NitSfjp#c}Mhg`#sbChyYq}e#rwg#atFff}#ghN~zKybk#fl}A}cPftcAite#tmT__Lha#u~DrfQi}MhkSqyWivIumCria#ciO_tHifm#fl}A{rc#fbjAqvg#rrqAcjCf%7Ci#mqJtb^s%7C#fbjA{wDfs`BmvEfqs#umWt_Nwn^pen#qiBr`xAcvMr{Zidg#dtjDkbM%7Cd_#"/>

I was able to solve the problem, this is the code:
import requests
url = ("https://maps.googleapis.com/maps/api/staticmap?size=400x400path=weight:3%7Ccolor:orange%7Cenc:polyline_data")
r = requests.get(url)
image = r._content
with open("map.png","wb") as file: #with this you create a usable file .png
file.write(image)

Related

Is there any way by which I can apply imagehash function in online images in Python

I want to get fingerprints for images with the help of the imagehash function in Python but in order to apply
hash = imagehash.average_hash(Image.open(path))
the image needs to be in storage. Is there any way from which by just giving the image URL I can get the fingerprint of the image?
You can use requests:
url = 'https://commons.wikimedia.org/wiki/File:Example.png'
import requests
response = requests.get(url, stream=True)
ihash = imagehash.average_hash(Image.open(response.raw))

How to tell a null image from a valid image?

I test a code, with one of its outputs is an image.
I need to make sure the image has a valid output, but if it is a NULL, I seem to can't tell from a valid image in the inspection section.
The only difference I could tell is a field called src where a null image may appear like this:
<img src=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjwAAAGQCAIAAAAlWy3FAAAABmJLR0QA/wD/AP+gvaeTAAAF+klEQVR4nO3VQQ0AIBDAMMC/58MCP7KkVbDf9swsACg4vwMA4JVpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmB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YFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkXCWIBh2WAyGkAAAAAElFTkSuQmCC width="572" height="400">
Whereas for valid images, the src field is MUCH longer.
Is there a python command which analyzes an image via a link to see if it is too... "white"?
Also, I did notice that in the field I just quoted here there is a repeating substring "pAZBhWgBkmBYAGaYFQIZ". is this significant?
If you just want to check whether an image is currently present at a specific URL, there is quite an easy way to check that.
from urllib.request import urlopen
import requests
image_formats = ("image/png", "image/jpeg", "image/gif")
url = "http://localhost/img.png"
site = urlopen(url)
meta = site.info() # get header of the http request
if meta["content-type"] in image_formats:
print("it is an image")
More such solutions/alternatives can be found in check if a URL to an image is up and exists in Python

Getting a URL of some picture from Google search

New to Python. I'm trying to find a way to get a url of the first picture I get from google search for some string. For example if I type "dog" I would like to get the first picture url for dog. I don't care which one just some url from Google image search.
Is it possible? what is the easiest way to do it? I saw from previous threads many ways to extract/download the image - but I just need the url and it doesn't matter which one.
This should work, simply replace the word to get images of anything.
Make sure you have requests and BeautifulSoup, if not run this command:
pip install requests beautifulsoup4
import requests
from bs4 import BeautifulSoup
word = 'dog'
url = 'https://www.google.com/search?q={0}&tbm=isch'.format(word)
content = requests.get(url).content
soup = BeautifulSoup(content,'lxml')
images = soup.findAll('img')
for image in images:
print(image.get('src'))
I don't know about Google, but I do know an easy way to do this with Bing. There's a PyPI module called bing-image-urls (https://pypi.org/project/bing-image-urls) , and this will do the job nicely. It's pretty easy to use. Just install it with:
pip install bing-image-urls
Then, in your python script, have this code:
from bing_image_urls import bing_image_urls
url = bing_image_urls("dog", limit=1)[0]
print(url)
Just replace "dog" in this example with whatever you want.
Hopefully this answers your question
Thanks!

How to change the recognition language

Using Google vision from here I was successfully able to create a client and an image using vision.Client() and client.image(content=data) respectively. And then send my image using image.detect_text(), attempting to read the digits within the image. however Google-vision has been inaccurate and I heard, from this question, that by setting the language to another (non-latin) language would help with this.
But that is where I am stuck, I'm not sure where to set the languageHints, and yes I have seen this link to the documentation of the AnnotateImageRequest, but I am still confused as to where this comes in.
I am not an expert in this but the following seems to work for me:
First you create an image_context object, as follows:
image_context = types.ImageContext(language_hints =["en"])
Then you call text_detection with the image_context you created as parameter, as follows:
response = client.text_detection(image=image, image_context=image_context)
image_context = vision.ImageContext(language_hints =["en"])
response = client.text_detection(image=image, image_context=image_context)

Cassandra 2 - CQLEngine : Store Images error

I'm trying to store images in database.This is my code for get an Image :
image = Image.open(...a resource on web...)
imageData = StringIO.StringIO()
image.save(imageData, image.format)
myImage = imageData.getvalue()
But when trying to store in database by this:
myTable.create(...some fields , image=myImage)
I catch an exception with this message:
Bad Request: Invalid STRING constant(ffd8ffe0.. and so on...adss4das) for image of type blob
I previously store images by these codes using Cassandra1.2.9!
But when I installed Cassandra2.0 , this problem happened!
I check my code line by line,and I'm sure that error in the way of storing images in C2.0 or getting image.
I think you're having problems with this: https://github.com/datastax/python-driver/pull/39. I'm sure that cqlengine isn't updated yet to take advantage of that fix (I just merged the pull request today), but that at least explains what the problem is.
As a workaround, you might be able to do something like:
from binascii import hexlify
hex_image = '0x' + hexlify(myImage)
myTable.create(..., image=hex_image)

Categories