def general_image(self, image_format='PNG'):
fm_width = self.cleaned_data['width']
fm_height = self.cleaned_data['height']
key = '{}.{}.{}'.format(fm_width, fm_height, image_format)
content = cache.get(key)
if content is None:
image = Image.new('RGB', (fm_width, fm_height), color=122)
draw = ImageDraw.Draw(image)
text = '{}x{}'.format(fm_width, fm_height)
text_width, text_height = draw.textsize(text)
if text_width < fm_width and text_height < fm_height:
text_top = (fm_height - text_height) // 2
text_left = (fm_width - text_width) // 2
draw.text((text_top, text_left), text, fill=(255, 255, 255))
content = BytesIO()
image.save(content, image_format)
content.seek(0)
cache.set(key, content, 60 * 60)
return content
评论列表
文章目录