python - Open Image From Online, Save To Server Flask -


I am using a flask to open an image from the URL.

  file = cStringIO StringIO (urllib.urlopen (URL) .read ()) img = Image.open (file)  

Then move the image I want to save it on my site. When I do this, I get

  traceback (most recent call final): file "/ library / picture / 2/2 / site-package / flask / search", line Return to 1836, __call__ Wsgi_app response = self.make_response (self.handle_exception (e)) file "/ Library / Python / Wsgi_app (environment, start_response) file" / library / Python 2/7 / site-package / flask / search ", line 1820, 2.7 / site-packages / flask / app.py ", line 1403, handle_exception reraise (exc_type, exc_value, tb) file" / library / python 2/7 / site-package / flux / "line 1817, wsgi_app in response = Self.full_dispatch_request () file "/ library / python 2/2 / site-package / flask / search", line 1477, full_dispatch_request rv = self.handle_user_exception (e) file "/ library / python / 2.7 / s "/ Library / python 2/7 / site-package / flax / extension" in the it-package / flask / app.py ", line 1381, handle_user_exception reraise (exc_type, exc_value, tb), line 1475, full_dispatch_request rv = self In the .dispatch_request () file "/ library / Pyt Hon / 2.7 / site-packages / flask / app.py", line 1461, in the send_refying request self.view_functions [rule.endpoint] (** req.view_args) file " /Users/User/Desktop/Flask/fl.py ", line 37, img.save (os.path.join (app.config ['UPLOAD_FOLDER'], file name) in the index file" / Library / Python 2/2 Save / site-package / PIL / image ", line 1648, KeyError (ext) for #Exception Extension KeyError: ''  

Here's the code to save:

  filename = secure_filename (img. File name) img.save (os.path.join (app.config ['UPLOAD_FOLDER'], filename)) print url_for ('uploaded_file', filename = filename) @ app.route ('/ uploads / & lt; filename & Gt; ') Def uploaded file (filename): return send_from_directory (app.config [' UPLOAD_FOLDER '], filename)  

Why is this happening? How can I fix this?

Save object in an image without filename , a Like the StringIO object, you have to tell PIL what type of image it is:

  img.save (os.path.join (app.config ['UPLOAD_FOLDER '], File name), format =' PNG ')  

Here I said that the format is a PNG, but you have to do content-type enlightenment Header with feedback and see how the header is To map the content in the appropriate format PIL based.

You will also need to come up with a better file name; img.filename is an empty string because you have never given img.open () a file name instead of the example URL Use; The file name will probably be:

  formats = {'picture / JPEG': 'JPEG', 'image / PNG': 'PNG', 'image / gif': 'gif'} Response = urllib.urlopen (URL) image_type = response.info (). ('Content-Type'): Format = Format [image_type] Excluding KeyError: Increase ValueError ('Not supported image format') file = cStringIO .tringIO (response.read ()) img = Image.open (file ) # ... filename = secure_filename (URL.rpartition ('/') [- 1]) img.save (os.path.join (app config ['UPLOAD_FOLDER'], file name), format = format) / Code> 

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -