image mode from raw bytes

This commit is contained in:
klemek
2020-04-29 12:23:49 +02:00
parent 08c938719a
commit b6d063e1e5
+10 -1
View File
@@ -1,5 +1,6 @@
from typing import List, Optional, Tuple from typing import List, Optional, Tuple
from PIL import Image, ImageFont, ImageDraw from PIL import Image, ImageFont, ImageDraw, UnidentifiedImageError
import io
import os import os
import os.path as path import os.path as path
import logging import logging
@@ -72,6 +73,14 @@ def build_text_only(texts: List[Text], debug: bool = False) -> Image.Image:
return apply_texts(txt_img, texts, debug=debug) return apply_texts(txt_img, texts, debug=debug)
def build_image_only(image_data: bytes) -> Optional[Image.Image]:
try:
image = Image.open(io.BytesIO(image_data))
except UnidentifiedImageError:
return None
return image.convert(mode='RGB')
def apply_texts(img: Image.Image, texts: List[Text], debug: bool = False) -> Image.Image: def apply_texts(img: Image.Image, texts: List[Text], debug: bool = False) -> Image.Image:
if img.mode != 'RGBA': if img.mode != 'RGBA':
img = img.convert(mode='RGBA') img = img.convert(mode='RGBA')