def binarize_image(image):
from PIL import Image
binary = None
try:
im = Image.open(image)
thumb = im.copy()
thumb.thumbnail((260, 260))
image_buffer = StringIO()
thumb.save(image_buffer, "JPEG")
binary = Binary(image_buffer.getvalue(), BINARY_SUBTYPE)
except IOError as e:
logging.error("failed to binarize image: "+str(e))
return None
# if image is a file object, rewind it
finally:
try:
image.seek(0)
except AttributeError:
pass
return binary
评论列表
文章目录