Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import argparse
  4. import subprocess
  5. import logging
  6. import sys
  7. import cgi
  8.  
  9. logging.basicConfig(
  10. level=logging.DEBUG, filename='/tmp/image_converter.log', filemode='a',
  11. format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
  12. datefmt='%H:%M:%S')
  13.  
  14.  
  15. class Picture:
  16. def __init__(self, data):
  17. self.data = data
  18.  
  19. def convert_to_max_resolution(self, max_side):
  20. cmd = ['convert', '-resize',
  21. '{:d}x{:d}>'.format(max_side, max_side), '-', '-']
  22. logging.info(' '.join(cmd))
  23. p = subprocess.Popen(cmd, stdin=subprocess.PIPE,
  24. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  25. self.data, serr = p.communicate(input=self.data)
  26. if p.returncode != 0:
  27. raise Exception('Failed to convert image: ' + ' '.join(cmd) + ' ' +
  28. serr.decode())
  29.  
  30. def add_exif_data(self, tags):
  31. cmd = ['exiftool', '-', '-Subject+=convert_tool_devmp.org',
  32. '-HierarchicalSubject+=convert_tool_devmp.org']
  33. for k, v in tags.items():
  34. cmd.append('-{:s}={:s}'.format(k, v))
  35. logging.info(' '.join(cmd))
  36. p = subprocess.Popen(cmd, stdin=subprocess.PIPE,
  37. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  38. self.data, serr = p.communicate(input=self.data)
  39. if p.returncode != 0:
  40. raise Exception('Failed to add tags: ' + ' '.join(cmd) + ': ' +
  41. serr.decode())
  42.  
  43. def get_data(self):
  44. return self.data
  45.  
  46.  
  47. def cgi_error(message):
  48. print('''Content-type:text/html\r\n\r\n
  49. <html>
  50. <head>
  51. <title>Resize and Upload on weeklypic.de</title>
  52. </head>
  53. <body>''')
  54. print('<h2>' + message + '</h2>')
  55. print('''
  56. <form method="post" enctype="multipart/form-data">
  57. <label>Picture (.jpg) to Upload.
  58. <input name="picture" type="file" size="50" accept="image/jpeg" />
  59. </label><br />
  60. <label>Slack Name
  61. <input type="text" name="name" />
  62. </label><br />
  63. <label>Picture Description
  64. <input type="text" name="description" />
  65. </label><br />
  66. <label>Week
  67. <input type="text" name="week" />
  68. </label><br />
  69. <button>Convert</button>
  70. </form>
  71. </body>
  72. </html>''')
  73. exit(0)
  74.  
  75.  
  76. if __name__ == '__main__':
  77. form = cgi.FieldStorage()
  78. pic = Picture(form.getvalue('picture'))
  79. if 'picture' not in form:
  80. logging.info('No picture given')
  81. cgi_error('You need to upload a picture')
  82. if 'name' not in form or form.getvalue('name').strip() == '':
  83. logging.info('No name given')
  84. cgi_error('You need to set your name')
  85. if 'description' not in form or form.getvalue('description').strip() == '':
  86. logging.info('No description given')
  87. cgi_error('You need to set your picture description')
  88. if 'week' not in form or form.getvalue('week').strip() == '':
  89. logging.info('No week given')
  90. cgi_error('You need to set the week number')
  91. logging.info(
  92. 'Upload of picture for "{:s}" picture "{:s}" week "{:s}"'.format(
  93. form.getvalue('name'), form.getvalue('description'),
  94. form.getvalue('week')))
  95.  
  96. pic.convert_to_max_resolution(2000)
  97. pic.add_exif_data(
  98. {'ImageDescription':
  99. form.getvalue('name') + ' / ' + form.getvalue('description')})
  100.  
  101. print("Content-type:image/jpeg;")
  102. print("Content-Disposition: attachment; filename=w_" +
  103. form.getvalue('week') + "_" + form.getvalue('name') + ".jpg;")
  104. print("Content-Transfer-Encoding: binary;")
  105. print("Content-Length: " + str(len(pic.get_data())) + ";\r\n")
  106. logging.info(
  107. 'Returning picture of size {:d} for "{:s}" picture "{:s}" week "{:s}"'.format(
  108. len(pic.get_data()), form.getvalue('name'),
  109. form.getvalue('description'), form.getvalue('week')))
  110. sys.stdout.flush()
  111. sys.stdout.buffer.write(pic.get_data())
  112. sys.stdout.flush()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement