ESN-Trondheim / ESNbot

A Slack bot for ESN Trondheim
MIT License
3 stars 1 forks source link

Coverphoto fails because "color must be int or single-element tuple" #57

Closed LaiAlexander closed 10 months ago

LaiAlexander commented 10 months ago
2023-10-31 20:02:59,303 - Stack trace - ERROR - color must be int or single-element tuple
2023-10-31 20:02:59,303 - Stack trace - ERROR - Traceback (most recent call last):
  File "/opt/ESNbot/esnbot/main.py", line 135, in handle_command
    self.choose_command(command, arguments, channel, user, output)
  File "/opt/ESNbot/esnbot/main.py", line 163, in choose_command
    return func(self, channel, user, arguments, output)
  File "/opt/ESNbot/esnbot/commands.py", line 229, in coverphoto
    cp.create_coverphoto(background_img, filename, argument)
  File "/opt/ESNbot/esnbot/graphics/coverphoto.py", line 155, in create_coverphoto
    cover = overlay_images(blend_color(background, color, dimensions), LOGOS[logos])
  File "/opt/ESNbot/esnbot/graphics/coverphoto.py", line 64, in blend_color
    overlay = create_color_overlay(background, color, dimensions)
  File "/opt/ESNbot/esnbot/graphics/coverphoto.py", line 59, in create_color_overlay
    img = Image.new(background.mode, dimensions, color["rgb"])
  File "/usr/local/lib/python3.10/dist-packages/PIL/Image.py", line 2933, in new
    return im._new(core.fill(mode, size, color))
TypeError: color must be int or single-element tuple

User supplied tried this command: coverphoto facebook blue “Figure Drawing”“8th of November”

The issue is the supplied background picture, and can be easily reproduced. It seems to work as intended for other background pictures, can only reproduce with this specific image.

Use this image, and just coverphoto, no arguments. fg

image

This function fails: https://github.com/ESN-Trondheim/ESNbot/blob/a61b463a083d6a47716524bb994258680b1f42ec/esnbot/graphics/coverphoto.py#L58-L60

Edit: This also leaves the file coverphoto.jpg in the directory the bot is started from, as the function fails before the files is deleted. Fails here: https://github.com/ESN-Trondheim/ESNbot/blob/a61b463a083d6a47716524bb994258680b1f42ec/esnbot/commands.py#L229 Should try to delete here: https://github.com/ESN-Trondheim/ESNbot/blob/a61b463a083d6a47716524bb994258680b1f42ec/esnbot/commands.py#L246-L247

Edit 2: This is key image

LaiAlexander commented 10 months ago

It turns out this specific image has mode L, which means it is a grayscale image.

https://github.com/ESN-Trondheim/ESNbot/blob/a61b463a083d6a47716524bb994258680b1f42ec/esnbot/graphics/coverphoto.py#L59

This then fails, because you can't create a color in a greyscale image.

Fix is to convert mode earlier, instead if in overlay_images(): https://github.com/ESN-Trondheim/ESNbot/blob/a61b463a083d6a47716524bb994258680b1f42ec/esnbot/graphics/coverphoto.py#L69-L75

As a side note, watermark also sort of fails because if this. It will watermark the image, but the result will also be grayscale. watermarked

Created separate issue #58 for this problem, will resolve both in the same branch/PR.