donkirkby / live-py-plugin

Live coding in Python with PyCharm, Emacs, Sublime Text, or even a browser
https://donkirkby.github.io/live-py-plugin
MIT License
291 stars 57 forks source link

Live image without alpha channel #360

Closed donkirkby closed 1 year ago

donkirkby commented 2 years ago

What I did

Tried to compare two images without alpha channels.

from PIL import Image
from space_tracer import LivePillowImage, LiveImageDiffer

a = LivePillowImage(Image.new('RGB', (100, 100)))
b = LivePillowImage(Image.new('RGB', (100, 100)))
differ = LiveImageDiffer()
differ.compare(a, b)

What happened

It complained about the missing alpha channel.

/home/don/.local/share/virtualenvs/svg-turtle-7kVEgXjr/bin/python /home/don/.config/JetBrains/PyCharm2021.3/scratches/scratch.py
Traceback (most recent call last):
  File "/home/don/.config/JetBrains/PyCharm2021.3/scratches/scratch.py", line 7, in <module>
    differ.compare(a, b)
  File "/home/don/.local/share/virtualenvs/svg-turtle-7kVEgXjr/lib/python3.9/site-packages/space_tracer/live_image.py", line 262, in compare
    diff_fill = self.compare_pixel(fill1, fill2, is_missing)
  File "/home/don/.local/share/virtualenvs/svg-turtle-7kVEgXjr/lib/python3.9/site-packages/space_tracer/live_image.py", line 334, in compare_pixel
    ar, ag, ab, aa = actual_pixel
ValueError: not enough values to unpack (expected 4, got 3)

What I wanted to happen

Make the alpha channel default to 255, the same as changing the code sample above to use mode 'RGBA'.

My environment

Describe the versions of everything you were using:

Analysis

Add a helper function to pad pixels to four channels, and call it in compare_pixel(). Think about grayscale images, as well.

donkirkby commented 1 year ago

In progress as PR #530.