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/58MCP7KkVbDf9swsACg4vwMA4JVpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBk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GaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkmBYAGaYFQIZpAZBhWgBkXCWIBh2WAyGkAAAAAElFTkSuQmCC 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
Related
I'm trying to save images from the Spotify API
I get album art in the form of a link:
https://i.scdn.co/image/ab67616d00004851c96f7c7b077c224975b4c5ce
I think it's a jpg file.
I run into errors in trying to display or save this in python.
I'm not even sure how I'm meant to format something like:
Do I need str around the link?
str(https://i.scdn.co/image/ab67616d00004851c96f7c7b077c224975b4c5ce)
Or should I create a new variable e.g.
image_path = 'https://i.scdn.co/image/ab67616d00004851c96f7c7b077c224975b4c5ce'
And then:
im1 = im1.save(image_path)
Your second suggestion should work with an addition of actually downloading the image using urllib.request:
import urllib.request
image_path = 'https://i.scdn.co/image/ab67616d00004851c96f7c7b077c224975b4c5ce'
urllib.request.urlretrieve(image_path, "image.jpg")
I want to webscrape a site, and save some, but not all images to my computer. I want to save about 5,600 images, so doing it manually would be difficult. All of the images urls start with
https://assets.pokemon.com/assets/cms2/img/cards/
and then some other stuff that is specific to the image. How can I download only images that meet that criteria?
Also (sorry this is kind of 2 questions in 1, but its related) how can I save the images alt text as the file name?
Thanks!
Also sorry if this is a dumb question, if you can't tell by the fact that I'm scraping pokemon.com, I'm not exactly a professional.
Here's what I ended up doing:
import requests
import urllib.request
contents = requests.get(url) # Get request to site
data = contents.text # Get HTMl file as text
x = data.split("\"") # Splits it into an array using double quotes as separators (Because all of the image urls were in quotes)
for a in range(len(x)): # Runs this code for every member of the array
if 'https://assets.pokemon.com/assets/cms2/img/cards' in x[a]: # Checks for that URL snippet. (That's not the full URL, each full URL just started with that)
link = x[a] # If it is, store that member of the array separately to be extracted
name = x[a+2] # Alt text was always 2 members of the array later, not sure if this is true for all sites.
path = "/Users/myName/Desktop/Poke/" + name + ".png" # This is where I wanted to store the files
urllib.request.urlretrieve(link, path) # Retrieved the file from the link, and saved it to the path
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)
Python is known to be an easy and powerful language. I have a List, literally, of URL images,
>>> for i in images: print i
http://upload.wikimedia.org/wikipedia/commons/8/86/Influenza_virus_research.jpg
http://upload.wikimedia.org/wikipedia/commons/f/f8/Wiktionary-logo-en.svg
http://upload.wikimedia.org/wikipedia/en/e/e7/Cscr-featured.svg
http://upload.wikimedia.org/wikipedia/commons/f/fa/Wikiquote-logo.svg
http://upload.wikimedia.org/wikipedia/commons/4/4c/Wikisource-logo.svg
http://upload.wikimedia.org/wikipedia/commons/1/1b/Wikiversity-logo-en.svg
http://upload.wikimedia.org/wikipedia/commons/1/1b/Wikiversity-logo-en.svg
I wonder if there's some library (or snippet of code) in python to easily display a list of URL images in a browser, or maybe save them in a folder.
import urllib
urllib.urlretrieve("http://8020.photos.jpgmag.com/3670771_314453_2ee7120da5_m.jpg", "my.jpg")
The "my.jpg" is the path to save the file. It can be "/home/user/pics/my.jpg" etc..
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)