def make_thumbnail_image(src_filepath, dest_filepath): """make thumbnail (jpeg 128x128)""" if isnewer(dest_filepath, src_filepath): return try: pic = PILImage.open(src_filepath) except IOError as exe: raise _BadFile("unknown_image_format") except _DecompressionBombError as exe: raise _BadFile("image_too_big") temp_jpg_file = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) try: temp_jpg_file.close() tmpjpg = temp_jpg_file.name if pic.mode == "CMYK" and ( src_filepath.endswith("jpg") or src_filepath.endswith("jpeg")) or pic.mode in ["P", "L"]: convert_image(src_filepath, tmpjpg, ["-quality", "100", "-draw", "rectangle 0,0 1,1"]) pic = PILImage.open(tmpjpg) pic.load() width = pic.size[0] height = pic.size[1] if width > height: newwidth = 128 newheight = height * newwidth / width else: newheight = 128 newwidth = width * newheight / height pic = pic.resize((newwidth, newheight), PILImage.ANTIALIAS) try: im = PILImage.new(pic.mode, (128, 128), (255, 255, 255)) except: im = PILImage.new("RGB", (128, 128), (255, 255, 255)) x = (128 - newwidth) / 2 y = (128 - newheight) / 2 im.paste(pic, (x, y, x + newwidth, y + newheight)) draw = ImageDraw.ImageDraw(im) draw.line([(0, 0), (127, 0), (127, 127), (0, 127), (0, 0)], (128, 128, 128)) im = im.convert("RGB") im.save(dest_filepath, "jpeg") finally: os.unlink(tmpjpg)
def makeThumbNail(image, thumb): if isnewer(thumb, image): return pic = PILImage.open(image) tmpjpg = config.get("paths.datadir") + "tmp/img" + str( random.random()) + ".jpg" if pic.mode == "CMYK" and (image.endswith("jpg") or image.endswith("jpeg")) or pic.mode in [ "P", "L" ]: os.system("convert -quality 100 -draw \"rectangle 0,0 1,1\" %s %s" % (image, tmpjpg)) # always get a rgb image pic = PILImage.open(tmpjpg) try: pic.load() except IOError as e: pic = None raise OperationException("error:" + str(e)) width = pic.size[0] height = pic.size[1] if width > height: newwidth = 128 newheight = height * newwidth / width else: newheight = 128 newwidth = width * newheight / height pic = pic.resize((newwidth, newheight), PILImage.ANTIALIAS) try: im = PILImage.new(pic.mode, (128, 128), (255, 255, 255)) except: im = PILImage.new("RGB", (128, 128), (255, 255, 255)) x = (128 - newwidth) / 2 y = (128 - newheight) / 2 im.paste(pic, (x, y, x + newwidth, y + newheight)) draw = ImageDraw.ImageDraw(im) draw.line([(0, 0), (127, 0), (127, 127), (0, 127), (0, 0)], (128, 128, 128)) im = im.convert("RGB") im.save(thumb, "jpeg") if os.path.exists(tmpjpg): os.unlink(tmpjpg)
def make_thumbnail_image(src_filepath, dest_filepath): """make thumbnail (jpeg 128x128)""" if isnewer(dest_filepath, src_filepath): return pic = PILImage.open(src_filepath) temp_jpg_file = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) try: temp_jpg_file.close() tmpjpg = temp_jpg_file.name if pic.mode == "CMYK" and (src_filepath.endswith("jpg") or src_filepath.endswith("jpeg")) or pic.mode in ["P", "L"]: convert_image(src_filepath, tmpjpg, ["-quality", "100", "-draw", "rectangle 0,0 1,1"]) pic = PILImage.open(tmpjpg) pic.load() width = pic.size[0] height = pic.size[1] if width > height: newwidth = 128 newheight = height * newwidth / width else: newheight = 128 newwidth = width * newheight / height pic = pic.resize((newwidth, newheight), PILImage.ANTIALIAS) try: im = PILImage.new(pic.mode, (128, 128), (255, 255, 255)) except: im = PILImage.new("RGB", (128, 128), (255, 255, 255)) x = (128 - newwidth) / 2 y = (128 - newheight) / 2 im.paste(pic, (x, y, x + newwidth, y + newheight)) draw = ImageDraw.ImageDraw(im) draw.line([(0, 0), (127, 0), (127, 127), (0, 127), (0, 0)], (128, 128, 128)) im = im.convert("RGB") im.save(dest_filepath, "jpeg") finally: os.unlink(tmpjpg)
def make_presentation_image(src_filepath, dest_filepath): if isnewer(dest_filepath, src_filepath): return pic = PILImage.open(src_filepath) temp_jpg_file = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) try: temp_jpg_file.close() tmpjpg = temp_jpg_file.name if pic.mode == "CMYK" and ( src_filepath.endswith("jpg") or src_filepath.endswith("jpeg")) or pic.mode in ["P", "L"]: convert_image(src_filepath, tmpjpg, ["-quality", "100", "-draw", "rectangle 0,0 1,1"]) pic = PILImage.open(tmpjpg) pic.load() width = pic.size[0] height = pic.size[1] resize = 1 if resize: # resize images only if they are actually too big if width > height: newwidth = 320 newheight = height * newwidth / width else: newheight = 320 newwidth = width * newheight / height pic = pic.resize((newwidth, newheight), PILImage.ANTIALIAS) try: pic.save(dest_filepath, "jpeg") except IOError: pic.convert('RGB').save(dest_filepath, "jpeg") finally: os.unlink(tmpjpg)
def makeThumbNail(image, thumb): if isnewer(thumb, image): return pic = PILImage.open(image) tmpjpg = config.get("paths.datadir") + "tmp/img" + str(random.random()) + ".jpg" if pic.mode == "CMYK" and (image.endswith("jpg") or image.endswith("jpeg")) or pic.mode in ["P", "L"]: os.system("convert -quality 100 -draw \"rectangle 0,0 1,1\" %s %s" % (image, tmpjpg)) # always get a rgb image pic = PILImage.open(tmpjpg) try: pic.load() except IOError as e: pic = None raise OperationException("error:" + str(e)) width = pic.size[0] height = pic.size[1] if width > height: newwidth = 128 newheight = height * newwidth / width else: newheight = 128 newwidth = width * newheight / height pic = pic.resize((newwidth, newheight), PILImage.ANTIALIAS) try: im = PILImage.new(pic.mode, (128, 128), (255, 255, 255)) except: im = PILImage.new("RGB", (128, 128), (255, 255, 255)) x = (128 - newwidth) / 2 y = (128 - newheight) / 2 im.paste(pic, (x, y, x + newwidth, y + newheight)) draw = ImageDraw.ImageDraw(im) draw.line([(0, 0), (127, 0), (127, 127), (0, 127), (0, 0)], (128, 128, 128)) im = im.convert("RGB") im.save(thumb, "jpeg") if os.path.exists(tmpjpg): os.unlink(tmpjpg)
def makePresentationFormat(image, thumb): if isnewer(thumb, image): return pic = PILImage.open(image) tmpjpg = config.get("paths.datadir") + "tmp/img" + str( random.random()) + ".jpg" if pic.mode == "CMYK" and (image.endswith("jpg") or image.endswith("jpeg")) or pic.mode in [ "P", "L" ]: os.system("convert -quality 100 -draw \"rectangle 0,0 1,1\" %s %s" % (image, tmpjpg)) pic = PILImage.open(tmpjpg) try: pic.load() except IOError as e: pic = None raise OperationException("error:" + str(e)) width = pic.size[0] height = pic.size[1] resize = 1 if resize: # resize images only if they are actually too big if width > height: newwidth = 320 newheight = height * newwidth / width else: newheight = 320 newwidth = width * newheight / height pic = pic.resize((newwidth, newheight), PILImage.ANTIALIAS) try: pic.save(thumb, "jpeg") except IOError: pic.convert('RGB').save(thumb, "jpeg") if os.path.exists(tmpjpg): os.unlink(tmpjpg)
def make_presentation_image(src_filepath, dest_filepath): if isnewer(dest_filepath, src_filepath): return pic = PILImage.open(src_filepath) temp_jpg_file = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) try: temp_jpg_file.close() tmpjpg = temp_jpg_file.name if pic.mode == "CMYK" and (src_filepath.endswith("jpg") or src_filepath.endswith("jpeg")) or pic.mode in ["P", "L"]: convert_image(src_filepath, tmpjpg, ["-quality", "100", "-draw", "rectangle 0,0 1,1"]) pic = PILImage.open(tmpjpg) pic.load() width = pic.size[0] height = pic.size[1] resize = 1 if resize: # resize images only if they are actually too big if width > height: newwidth = 320 newheight = height * newwidth / width else: newheight = 320 newwidth = width * newheight / height pic = pic.resize((newwidth, newheight), PILImage.ANTIALIAS) try: pic.save(dest_filepath, "jpeg") except IOError: pic.convert('RGB').save(dest_filepath, "jpeg") finally: os.unlink(tmpjpg)
def makePresentationFormat(image, thumb): if isnewer(thumb, image): return pic = PILImage.open(image) tmpjpg = config.get("paths.datadir") + "tmp/img" + str(random.random()) + ".jpg" if pic.mode == "CMYK" and (image.endswith("jpg") or image.endswith("jpeg")) or pic.mode in ["P", "L"]: os.system("convert -quality 100 -draw \"rectangle 0,0 1,1\" %s %s" % (image, tmpjpg)) pic = PILImage.open(tmpjpg) try: pic.load() except IOError as e: pic = None raise OperationException("error:" + str(e)) width = pic.size[0] height = pic.size[1] resize = 1 if resize: # resize images only if they are actually too big if width > height: newwidth = 320 newheight = height * newwidth / width else: newheight = 320 newwidth = width * newheight / height pic = pic.resize((newwidth, newheight), PILImage.ANTIALIAS) try: pic.save(thumb, "jpeg") except IOError: pic.convert('RGB').save(thumb, "jpeg") if os.path.exists(tmpjpg): os.unlink(tmpjpg)