pimoroni / inky

Combined library for V2/V3 Inky pHAT and Inky wHAT.
https://shop.pimoroni.com/?q=inky
MIT License
578 stars 121 forks source link

Ellipse not drawing solid outline #111

Closed PeaceDealer closed 3 years ago

PeaceDealer commented 3 years ago

I'm using the inky wHAT, and I'm trying to draw an analog clock face, however, the outline of the PIL Ellipse tool doesn't seem to be solid. Got lots of white dots sprinkled around the outline.

This is the code which I'm using

inky_display = InkyWHAT('red')
inky_display.h_flip = True
inky_display.v_flip = True

colourBackground = inky_display.WHITE
colourForeground = inky_display.BLACK
colourSpecial = inky_display.RED

inky_display.set_border(colourForeground)

img = Image.new("P", (inky_display.WIDTH, inky_display.HEIGHT), colourBackground)

draw = ImageDraw.Draw(img)
draw.ellipse((20, 20, 280, 280), fill = None, outline = colourForeground, width=5)

inky_display.set_image(img)
inky_display.show()
jerbzz commented 3 years ago

Can you show us a picture of what you get?

PeaceDealer commented 3 years ago

Like this

20210421_141000

jerbzz commented 3 years ago

How interesting. It looks like it's maybe drawing 5 concentric ellipses and then aliasing is happening on the parts which aren't straight?

Do you get a solid line if you specify width = 1?

PeaceDealer commented 3 years ago

Its a continues line, however not very circular. There are places where it seems a little jittery, which could seem to line up with the white spots at width 5.

I guess this may be an issue with the PIL library then.

I was just trying to generate an RGB image, where its not an issue, but I cant get the display to process that directly.

Gadgetoid commented 3 years ago

Testing this in pure PIL on my desktop yields a solid - if someone ropey - line as you might expect:

tmpiifhh_o4

Code:

from PIL import Image, ImageDraw, __version__

print(__version__)

image = Image.new("P", (300, 300), 0)

draw = ImageDraw.Draw(image)

draw.ellipse((20, 20, 280, 280), fill=None, outline=1, width=5)

image.putpalette([255, 255, 255, 0, 0, 0])

image = image.resize((600, 600), Image.NEAREST)

image.show()

Assuming there wasn't some weird version of PIL where circles weren't rendered solid, I'm guessing set_image is failing to detect the image as "P" (palette) type and trying to quantize it.

But in the interest of science, what does the above code do for you?

Edit: This is the code path you should not be hitting: https://github.com/pimoroni/inky/blob/75f12b415ad1ae42ac3be92b33f83f51519f5da3/library/inky/inky_uc8159.py#L381-L391

PeaceDealer commented 3 years ago

This is the exact code i ran on the raspberry pi:

from PIL import Image, ImageDraw, __version__
print(__version__)
image = Image.new("P", (300, 300), 0)
draw = ImageDraw.Draw(image)
draw.ellipse((20, 20, 280, 280), fill=None, outline=1, width=5)
image.putpalette([255, 255, 255, 0, 0, 0])
image = image.resize((600, 600), Image.NEAREST)
image.save("test.png")

and the image that it generates looks like this: test

So it seems the PIL libary for some reason is to blame. It returns as version 5.4.1 on the pi.

jerbzz commented 3 years ago

image

Same on my Inky pHAT with PIL 5.4.1.

Gadgetoid commented 3 years ago

Well since PIL is intent on being.... weird... (even my circles leave much to be desired) ... perhaps this will work better:

from PIL import Image, ImageDraw, __version__

print(__version__)

image = Image.new("P", (300, 300), 0)

draw = ImageDraw.Draw(image)

width = 5

draw.ellipse((20, 20, 280, 280), fill=1, outline=None)
draw.ellipse((20 + width, 20 + width, 280 - width, 280 - width), fill=0, outline=None)

image.putpalette([255, 255, 255, 0, 0, 0])

image = image.resize((600, 600), Image.NEAREST)

image.show()
jerbzz commented 3 years ago

I was on the stock inky library install using the bash script from the product page.

I just:

sudo apt remove python3-pil

sudo pip3 install -Iv pillow==6.2.2

(latest version that supports Python 2.7)

sudo apt install libopenjp2-7

and now:

image

Looks good! (Well, as good as a badly rasterised circle with no antialiasing will ever look)

PeaceDealer commented 3 years ago

Can confirm, after changing to the pillow Libary everything works fine.