Comments (3)
The palette order and size matters, and I think the colour order is pretty arbitrary in an image you convert with PIL.
A "P" image could contain anywhere up to 256 colours, but Inky only cares about index 0 (white), 1 (black) and 2 (red or yellow).
When you convert your image- which contains anti-aliased text- you're probably inadvetently creating a palette of maybe 16 colours, which may not necessarily align with how Inky expects them.
One method of avoiding this is to create your image as "P" in the first place, and use explicit palette indexes in lieu of colours- ie instead of using a colour triplet in PIL drawing functions (0, 0, 0)
just specify a single number. You don't really need an attached palette, because Inky wont use it.
Another method, if you're dead set on using an RGB source image, is to use PIL's quantize
method. This may not give results you're happy with, as it'll smash all your text anti-aliasing to the nearest available colour (black or white). But bear in mind Inky can't display greyscale colour values, so the output will be representative of what you'll see on the display.
Here's a quick example of that process in action for a White/Black/Red display. This will also work for White/Black:
from PIL import Image, ImageDraw, ImageFont
from fonts.ttf import Roboto
image = Image.new('RGB', (212, 104))
font = ImageFont.truetype(Roboto)
draw = ImageDraw.Draw(image)
draw.text((0, 0), "Hello World", font=font, fill=(200, 200, 200))
draw.text((0, 20), "Oh boy, this looks bad!", font=font, fill=(200, 0, 0))
image.save("before.png")
palette = Image.new('P', (1, 1))
palette.putpalette(
[
255, 255, 255, # 0 = White
0, 0, 0, # 1 = Black
255, 0, 0, # 2 = Red (255, 255, 0 for yellow)
] + [0, 0, 0] * 253 # Zero fill the rest of the 256 colour palette
)
# Quantize our image using Inky's 3-colour palette
image = image.quantize(colors=3, palette=palette)
# Save the image
image.save("after.png")
Before quantization
After quantization
I should probably put this example and guide in the docs somewhere, but for now I hope this helps.
from inky.
Hello Philip, many thanks for the detailed help! That solved my issue.
The reason for using 'RGB' in the first place was that 'P' was also not behaving as expected for me.
In the end I think it was related to using a (0,0,0) tuple instead of a 0 value, which I now fixed.
Whish you a great week!
p.s. Also handling everything in 'RGB', with only the last step going to 'P', was nice as it allows to write modular code that can be used for every kind of display, instead of only Inky.
from inky.
Hey Philip, small follow up with a quick question. Hopefully it can also be useful to others folks who search for such a similar issue.
It all works nicely with the 'P' mode, but for logging purposes I'd like to save the displayed screenshot in a log folder (so I can check them out later).
Saving the 'P' mode images is giving me a black screen on my image viewer software (as they probably get picked up as 0/1 out of 255).
What would you suggest as a way to overcome this?
Is using https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.convert
to convert it to RGB, with a conversion matrix that map 0 -> (255, 255, 255) and 1 -> (0, 0, 0) the best way to go about this?
from inky.
Related Issues (20)
- Feature request: faster monochrome updates on Inky Impression HOT 2
- eeprom.py SMBus needs to be closed HOT 1
- SSD1608 Custom Lookup table HOT 2
- How to map palette to impression 7.3 colours and avoid dithering
- Make inky work with NVIDIA Jetson Nano HOT 1
- Magic number and checksum for eeprom HOT 1
- Pi Zero W - Not Enough Ram to pip install seaborn HOT 1
- `InkyMockImpression.set_image` fails with `AttributeError`
- `InkyMockImpression` class doesn't easily support multiple resolutions
- Use on any non-Pi machines? HOT 3
- Treating Inky pHAT-Red as Black/White doesn't work
- Busy Wait error - Line 323/333 HOT 1
- AttributeError: 'FreeTypeFont' object has no attribute 'getsize'
- Odd permissions issue on new Inky Impressions 7.3" HOT 1
- error: externally-managed-environment HOT 1
- Forcing black and white only on 7 color. HOT 2
- Partial Update or Single Pixel Update for all Inky Impression HOT 1
- Incompatible with Raspberry Pi 5B? HOT 8
- Not installable on MacOS for development
- Can't get Inky to work on Raspberry Pi Zero 2 W HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from inky.