Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. """Simple Image Grabber using PIL
  2. Flask==0.10.1
  3. simplejson==3.8.0
  4. Pillow==3.1.1
  5. """
  6. import uuid
  7. import io
  8. import os
  9. import urllib.request
  10. from urllib.parse import unquote
  11. from flask import Flask, send_file, jsonify, request
  12. from PIL import Image
  13.  
  14. app = Flask(__name__)
  15. request_methods = ['POST', 'GET']
  16.  
  17.  
  18. @app.route('/get_image', methods=request_methods)
  19. def get_image():
  20. """
  21. Get image from URL
  22. """
  23. filename = ""
  24. try:
  25. filename = unquote(unquote(request_field('url'))).replace(" ", "+")
  26. req = urllib.request.Request(filename)
  27. req.add_header('User-agent', 'Mozilla/5.0 (X11; U; Linux i686) \
  28. Gecko/20071127 Firefox/2.0.0.11')
  29. url_open = urllib.request.urlopen(req)
  30. image_file = io.BytesIO(url_open.read())
  31. image_size = image_file.getbuffer().nbytes
  32. image_obj = Image.open(image_file)
  33. image_format = image_obj.format
  34.  
  35. if image_format not in ["PNG", "GIF", "JPG", "JPEG"]:
  36. return jsonify(dict(error='INVALID_FORMAT'))
  37. # check for min resolution
  38. if request_field('min_res') and int(request_field('min_res')) > \
  39. max(image_obj.width, image_obj.height):
  40. return jsonify(dict(error='LOW_RESOLUTION'))
  41.  
  42. if request_field('max_size') and \
  43. int(request_field('max_size')) < image_size:
  44. return jsonify(dict(error='LARGE_IMAGE'))
  45.  
  46. baseheight = int(request_field('height')) \
  47. if request_field('height') else 0
  48. basewidth = int(request_field('width')) \
  49. if request_field('width') else 0
  50.  
  51. if basewidth and baseheight:
  52. image_obj.thumbnail((basewidth, baseheight), Image.ANTIALIAS)
  53. elif basewidth and not baseheight:
  54. wpercent = (basewidth / float(image_obj.size[0]))
  55. hsize = int((float(image_obj.size[1]) * float(wpercent)))
  56. image_obj = image_obj.resize((basewidth, hsize), Image.ANTIALIAS)
  57. elif not basewidth and baseheight:
  58. wpercent = (baseheight / float(image_obj.size[0]))
  59. hsize = int((float(image_obj.size[1]) * float(wpercent)))
  60. image_obj = image_obj.resize((hsize, baseheight), Image.ANTIALIAS)
  61. unique_filename = uuid.uuid4()
  62. tmp_file_name = "/tmp/" + str(unique_filename) + \
  63. "." + image_format.lower()
  64. image_obj.save(tmp_file_name, image_format, optimize=True, quality=95)
  65. return send_file(
  66. tmp_file_name, mimetype='image/' + image_format.lower())
  67. os.remove(tmp_file_name)
  68. except:
  69. return jsonify(dict(error="UNKNOWN"))
  70.  
  71.  
  72. def request_field(var):
  73. """
  74. Get values from POST or GET
  75. """
  76. if request.method == 'POST' and var in request.form:
  77. return request.form[var]
  78. elif var in request.args:
  79. return request.args.get(var)
  80. else:
  81. return None
  82.  
  83.  
  84. if __name__ == "__main__":
  85. app.run(host='0.0.0.0', port=8000, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement