def extract_image(url, width, height): data = download_from_url(url) path, ext = os.path.splitext(url) docsplit = Docsplit() tmp = NamedTemporaryFile() pdf_name = u'%s%s' % (tmp.name, ext) with open(pdf_name, 'wb') as f: f.write(data.getvalue()) if width: if height: size = '%dx%d' % (width, height) else: size = '%dx' % width else: size = 'x%d' % height try: docsplit.extract_images(str(pdf_name), output='/tmp', sizes=[size], formats=['png'], pages=[1]) except RunError: abort(400, 'Please enter a pdf file') filename, ext = os.path.splitext(pdf_name) io = StringIO() png = '/tmp/%s/%s_1.png' % (size, os.path.basename(filename)) with open(png) as f: io.write(f.read()) for remove_file in list(glob('/tmp/%s/%s*' % (size, filename))) + list(glob('/tmp/%s*' % filename)): print remove_file os.remove(remove_file) tmp.close() return io.getvalue()
def __wrapped_view(url, width, height): im = Image.open(download_from_url(url)) version = view_func(im, width, height) thumb = StringIO() version.save(thumb, 'PNG') return thumb.getvalue()