facebook graph api - Is it possible to open an in-memory image using the python open function? -


i using python library (poster) takes file-like object argument, documentation states:

the file-like objects must support .read() , either .fileno() or both .seek() , .tell().

i have tried library using python open function , works fine. downloading image url using following:

access_token = "xxxxxxxxxxxxxxxxxxxxx" postphotourl = "https://graph.facebook.com/me/photos?access_token=%s" % access_token  register_openers()  # image external url data = urllib2.urlopen("http://example.com/image.png") data = stringio(data.read())  ### data, headers = multipart_encode({"source":open("file.png")}) <- works fine data, headers = multipart_encode({"source":data}) request = urllib2.request(postphotourl,data,headers) 

edit: goal fetch image external url , post using facebook graph api. when use python open function have no issues. when try use stringio, no body sent post request.

if idea use poster package streaming http upload, shouldn't converting image data pil image object. do...

data = urllib2.urlopen("http://example.com/image.png") data = stringio(data.read()) 

...then can pass data variable poster.

unless, of course, wanted convert image pil first, should mention in question.

update

as why fails stringio, might poster checking filename of open file, , using determine correct content-type or somesuch, won't able when reading stringio.

i've never used package, , examples aren't comprehensive, might worth checking difference between headers variables when call like...

from poster.encode import multipart_encode  data = open('example.png', 'rb') datagen, headers = multipart_encode({"image1": data}) 

...versus...

from poster.encode import multipart_encode  data = urllib2.urlopen("http://example.com/image.png") data = stringio(data.read()) datagen, headers = multipart_encode({"image1": data}) 

update #2

looks right content-type thing. poster source code of encode.py lines 168-174...

        if hasattr(value, 'read'):             # looks file object             filename = getattr(value, 'name', none)             if filename not none:                 filetype = mimetypes.guess_type(filename)[0]             else:                 filetype = none 

...although there may other issues if filename none. try this...

from poster.encode import multipart_encode, multipartparam  data = urllib2.urlopen("http://example.com/image.png") data = stringio(data.read()) param = multipartparam(name='source',                        filename='image.png',                        filetype='image/png',                        fileobj=data) datagen, headers = multipart_encode({"source": param}) 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -