+
+ # Limit PNG to 200K. If larger, convert to JPEG.
+ if th_format == 'PNG' and buffer.tell() > MAX_PNG_WEIGHT:
+ th_format, ext, media_type = 'JPEG', 'jpg', 'image/jpeg'
+ if th.mode != 'RGB':
+ buffer = io.BytesIO()
+ th = Image.alpha_composite(
+ Image.new('RGBA', th.size, '#fff'),
+ th.convert('RGBA')
+ )
+ th = th.convert('RGB')
+ th.save(buffer, format=th_format)
+