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)
Related
So when I use the pdf2image python import, and pass a multi page PDF into the convert_from_bytes()- or convert_from_path() method, the output array does contain multiple images - but all images are of the last PDF page (whereas I would've expected that each image represented one of the PDF pages).
The output looks something like this:
Any idea on why this would occur? I can't find any solution to this online. I've found some vague suggestion that the use_cropbox argument might be used, but modifying it has no effect.
def convert(opened_file)
# Read PDF and convert pages to PPM image objects
try:
_ppm_pages = self.pdf2image.convert_from_bytes(
opened_file.read(),
grayscale = True
)
except Exception as e:
print(f"[CreateJPEG] Could not convert PDF pages to JPEG image due to error: \n '{e}'")
return
# Do stuff with _ppm_pages
for img in _ppm_pages:
img.show() # ...all images in that list are of the last page
Sometimes the output is an empty 1x1 image, instead, which I also haven't found a reason for. So if you have any idea what that is about, please do let me know!
Thanks in advance,
Simon
EDIT: Added code.
EDIT: So, when I try this in a random notebook, it actually works fine.
I've removed a few detours I used in my original code, and now it works. Still not sure what the underlying reason was though...
All the same, thanks for your help, everyone!
I'm using this right now....
from pdf2image import convert_from_path
imgSet = convert_from_path(pathToPDF, 500)
That gives me a list of images within imgSet
I guess you have to do something like this as described in the unit tests of the package.
with open("./tests/test.pdf", "rb") as pdf_file:
images_from_bytes = convert_from_bytes(pdf_file.read(), fmt="jpg")
self.assertTrue(images_from_bytes[0].format == "JPEG")
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 have created a binary file of an image using the code:
f1=open('file_name.png','rb')
f2=open('newfile.txt ','wb')
byte=f1.read()
f2.write(byte)
I have got the binary file but now I want to load that image using that binary code. Now, I want to load that image (or maybe recreate) that image by the source code itself in other file. how do I do that?
please note: I'm a newbie at python.
I have looked over the Various and found programs that I didn't understand.
this code was present there most of the time I searched. It made a bit sense to me.
I have tried the code with base64:
import base64
with open("t.png", "rb") as imageFile:
str = base64.b64encode(imageFile.read())
print (str)
but this is not doing anything. not showing errors, not printing the string either.
if there is a simple way please I request you to explain by the code.
Thanks in advance!
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)
I'm trying to save a jpg image from a Flask app form. The following code works fine:
blob = request.files[canvas_key]
blob.stream.seek(0)
data = blob.stream.read()
string_io = cStringIO.StringIO(data)
string_io has type <cStringIO.StringI object at 0x10bf2bf10>.
Yet when I try: Image.open(string_io), I get this error: IOError: cannot identify image file.
Using stream.seek(0) seems to solve this problem for other people, but in my case it hasn't.
Thanks to #Mark, I changed the image type I capture from WebP to PNG.