background transparency solved
2021-03-03 22:34:52,004:ERROR:An unknown error occurred while creating a notification image: Traceback (most recent call last): File "trade_worker.py", line 92, in send_trade File "notification_builder.py", line 81, in build_image File "notification_builder.py", line 132, in stitch_text File "PIL\ImageDraw.py", line 373, in text File "PIL\ImageDraw.py", line 324, in draw_text File "PIL\ImageFont.py", line 667, in getmask2 OSError: invalid face handle
import io, os from PIL import Image def resize(im, factor): return im.resize((int(im.width * factor), int(im.height * factor)), Image.ANTIALIAS) def getsize(im, factor): im_resized = resize(im, factor) # Save to memory, rather than disk, and check size with io.BytesIO() as fp: im_resized.save(fp, "PNG") return fp.getbuffer().nbytes def scale_down_to_size(im, destination_size): size = getsize(im, 1) if size < destination_size: print("Already small enough: "+str(size)) return im # Binary search factor = 0.5 gap = 0.5 while gap > 0.0005: gap *= 0.5 size = getsize(im, factor) if size > destination_size: factor -= gap else: factor += gap if size > destination_size: factor -= gap size = getsize(im, factor) print("Final size: "+str(size)) return resize(im, factor) with Image.open("Tests/images/hopper.png") as im: im = scale_down_to_size(im, 500 * 1024) # 500 kb im.save("out.png")
You might running into https://github.com/python-pillow/Pillow/issues/5461#issuecomment-831114416 - error code 6 from webp, PARTITION0_OVERFLOW - "partition is bigger than 512k"
Without a copy of your image, I can't say for sure. The next version of Pillow due out in a month will help this a bit, by adding in an error code alongside 'encoding error'.
Here are the options as far as handling GIF frames with LCTs, transparency, and disposal 0/1:
All three would mean currently broken GIF files would display properly, though 3 would need extra work when encoding to put back in transparency and squish the color tables and extra work as far as decoupling GIF frames from the GIF's data.