Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import argparse
- import subprocess
- import logging
- import sys
- import cgi
- logging.basicConfig(
- level=logging.DEBUG, filename='/tmp/image_converter.log', filemode='a',
- format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
- datefmt='%H:%M:%S')
- class Picture:
- def __init__(self, data):
- self.data = data
- def convert_to_max_resolution(self, max_side):
- cmd = ['convert', '-resize',
- '{:d}x{:d}>'.format(max_side, max_side), '-', '-']
- logging.info(' '.join(cmd))
- p = subprocess.Popen(cmd, stdin=subprocess.PIPE,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- self.data, serr = p.communicate(input=self.data)
- if p.returncode != 0:
- raise Exception('Failed to convert image: ' + ' '.join(cmd) + ' ' +
- serr.decode())
- def add_exif_data(self, tags):
- cmd = ['exiftool', '-', '-Subject+=convert_tool_devmp.org',
- '-HierarchicalSubject+=convert_tool_devmp.org']
- for k, v in tags.items():
- cmd.append('-{:s}={:s}'.format(k, v))
- logging.info(' '.join(cmd))
- p = subprocess.Popen(cmd, stdin=subprocess.PIPE,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- self.data, serr = p.communicate(input=self.data)
- if p.returncode != 0:
- raise Exception('Failed to add tags: ' + ' '.join(cmd) + ': ' +
- serr.decode())
- def get_data(self):
- return self.data
- def cgi_error(message):
- print('''Content-type:text/html\r\n\r\n
- <html>
- <head>
- <title>Resize and Upload on weeklypic.de</title>
- </head>
- <body>''')
- print('<h2>' + message + '</h2>')
- print('''
- <form method="post" enctype="multipart/form-data">
- <label>Picture (.jpg) to Upload.
- <input name="picture" type="file" size="50" accept="image/jpeg" />
- </label><br />
- <label>Slack Name
- <input type="text" name="name" />
- </label><br />
- <label>Picture Description
- <input type="text" name="description" />
- </label><br />
- <label>Week
- <input type="text" name="week" />
- </label><br />
- <button>Convert</button>
- </form>
- </body>
- </html>''')
- exit(0)
- if __name__ == '__main__':
- form = cgi.FieldStorage()
- pic = Picture(form.getvalue('picture'))
- if 'picture' not in form:
- logging.info('No picture given')
- cgi_error('You need to upload a picture')
- if 'name' not in form or form.getvalue('name').strip() == '':
- logging.info('No name given')
- cgi_error('You need to set your name')
- if 'description' not in form or form.getvalue('description').strip() == '':
- logging.info('No description given')
- cgi_error('You need to set your picture description')
- if 'week' not in form or form.getvalue('week').strip() == '':
- logging.info('No week given')
- cgi_error('You need to set the week number')
- logging.info(
- 'Upload of picture for "{:s}" picture "{:s}" week "{:s}"'.format(
- form.getvalue('name'), form.getvalue('description'),
- form.getvalue('week')))
- pic.convert_to_max_resolution(2000)
- pic.add_exif_data(
- {'ImageDescription':
- form.getvalue('name') + ' / ' + form.getvalue('description')})
- print("Content-type:image/jpeg;")
- print("Content-Disposition: attachment; filename=w_" +
- form.getvalue('week') + "_" + form.getvalue('name') + ".jpg;")
- print("Content-Transfer-Encoding: binary;")
- print("Content-Length: " + str(len(pic.get_data())) + ";\r\n")
- logging.info(
- 'Returning picture of size {:d} for "{:s}" picture "{:s}" week "{:s}"'.format(
- len(pic.get_data()), form.getvalue('name'),
- form.getvalue('description'), form.getvalue('week')))
- sys.stdout.flush()
- sys.stdout.buffer.write(pic.get_data())
- sys.stdout.flush()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement