@@ -243,12 +243,15 @@ class Generator(object):
# Draw the rectangle.
draw.rectangle((x1, y1, x2, y2), fill=foreground)
# Set-up a stream where image will be saved.
stream = BytesIO()
if image_format.upper() == "JPEG":
image = image.convert(mode="RGB")
# Save the image to stream.
try:
image.save(stream, format=image_format, optimize=True)
except KeyError:
raise ValueError("Pillow does not support requested image format: %s" % image_format)
image_raw = stream.getvalue()