I'm working on a REST API using Flask in which I have to retrieve an Image from a URL which is provided by the user inside the Url Parameters.
Here's What I have tried:
import urllib
from io import BytesIO
from PIL import Image
from flask import Flask
app = Flask(__name__)
@app.route('/<path:image_url>')
def build_mask_rmv_bg(image_url):
f = urllib.request.urlopen(image_url)
jpeg_str = f.read()
original_im = Image.open(BytesIO(jpeg_str))
return original_im
if __name__ == '__main__':
app.run()
And, Here my request:
http://127.0.0.1:5000/http://raw.githubusercontent.com/tensorflow/models/master/research/deeplab/g3doc/img/image2.jpg
I have tried both HTTP & HTTTPs paths in URL Parameter.But it return the following error:
urllib.error.URLError: <urlopen error [SSL:CERTIFICATE_VERIFY_FAILED] certificate
verify failed (_ssl.c:749)>
127.0.0.1 - - [01/Sep/2018 09:37:51] "GET /http://raw.githubusercontent.com/tensorflow/models/master/research/deeplab/g3doc/img/image2.jpg HTTP/1.1" 500 -
Update:
I have updated it by using wget
, now it doesn't return any error but the file is still not downloading the image.
Here's the updated code:
from flask import Flask
import wget
app = Flask(__name__)
@app.route('/<path:image_url>')
def build_mask_rmv_bg(image_url):
url = str(image_url)
# download the file contents in binary format
print(url)
wget.download(url, "img/image1.jpg")
return 'something happened'
if __name__ == '__main__':
app.run()
What's wrong here?
Help me, please!
Thanks in advance!