Post image retrieved from urlopen

369 Views Asked by At

I would to send a file to the Telegram Bot API in order to post a picture. I am using python on Google App Engine and I would like to download a picture from a URL and send it in a Telegram message, without creating any intermediate file on disk (which I believe isnt't even possible given the platform).

I have successfully sent an image taken from disk, but no matter what I try, I am not able to send a picture retrieved from a URL. Here is the working code:

import urllib2
import requests

photo = open('cat.jpg', 'r')
#photo = urllib2.urlopen('http://scontent-b.cdninstagram.com/hphotos-xfa1/t51.2885-15/e15/10919672_584633251672188_179950734_n.jpg')

resp = requests.post(BASE_URL + 'sendPhoto',
    files={'photo' : photo},
    data={'chat_id' : '95297807'},
)

If I get photo with urlopen it won't work. I tried using StringIO, but it doesn't work either. Any ideas?

1

There are 1 best solutions below

2
On
import urllib2
from StringIO import StringIO
from PIL import Image

url='http://scontent-b.cdninstagram.com/hphotos-xfa1/t51.2885-15/e15/10919672_584633251672188_179950734_n.jpg'

photo=Image.open(StringIO(urllib2.urlopen(url).read()))