python 2.7 - How to get image Type and size when uploading it ? google app engine -
i using code upload image cloud storage:
class uploadimagetocloudstorage(webapp2.requesthandler): def post(self): image = self.request.get("file") gcs_file=gcs.open(gcs_bucket_name+'testimage.png', 'w') gcs_file.write(image.encod('utf-8')) gcs_file.close() self.response.write("succeed !")
and form send image:
<form action="http://myappid.appspot.com/test/uploadimage" method="post"> <input type="file" name="file" value='file'><br> <input type="submit" value="submit"> </form
how check size , type of image before saving in cloud storage ?
>
if add enctype="multipart/form-data" form element, can use image data instantiate image object using code following:
(pay particular attention way mime-type extracted , split detect valid image formats, , fact we're using google.appengine.api.images class image extract dimensions after using constructor , passing raw image data)
import webapp2 import google.appengine.api.images images class uploader(webapp2.requesthandler): def post(self): img_stream = self.request.get ('file') mimetype = self.request.post['file'].type img_format = mimetype.split('/')[1] if (img_format == 'jpeg' or 'jpg' or 'gif' or 'png' or 'bmp' or 'tiff' or 'ico' or 'webp'): imageobj = images.image (img_stream) width = imageobj.width height = imageobj.height self.response.out.write ('<html><body>uploaded image %s x %s [%s]</body></html>' % (width, height, img_format)) else: self.response.out.write ('not valid format')
Comments
Post a Comment