Skip to content

Commit

Permalink
optional dithering
Browse files Browse the repository at this point in the history
  • Loading branch information
adriankeenan committed Dec 30, 2024
1 parent 53d08dc commit dbd204e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Data should be sent form-encoded.
|--------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|----------|
| `image` | Image file to display. Any image type supported by the Python PIL library should work. | N/A | Yes |
| `mode` | Display mode, corresponding to the `display_XXX` method on the `epd` object.<br>`FAST` - uses `display_Fast` for a complete screen refresh.<br>`PARTIAL` - uses `display_Partial` for incremental updates (which may cause ghosting). | `FAST` | No |
| `dither` | Whether to enable ditering when converting image to mono chromatic. `true` or `1` to enable. | `true` | No |
| `resize` | `FIT` Resize keeping aspect ratio, without cropping<br>`CROP` Resize keeping aspect ratio, with cropping<br>`STRETCH` fill display, ignoring aspect ratio<br>`NONE` Display without any scaling, pixels drawn 1:1. | `FIT` | No |
| `rotate` | Number of degrees to rotate counter-clockwise, supports increments of 90 degrees. | 0 | No |
| `background` | Background colour to use if the image doesn't fill the display. Either `WHITE` or `BLACK`. | `WHITE` | No |
Expand Down
7 changes: 5 additions & 2 deletions src/img_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Tuple
from PIL import Image

from models import Resolution, Rotation, Resize, BackgroundColour, Mode
from models import Resolution, Rotation, Resize, BackgroundColour


def resize_img(img: Image, mode: Mode, rotation: Rotation, resize: Resize, background: BackgroundColour,
def resize_img(img: Image, dither: bool, rotation: Rotation, resize: Resize, background: BackgroundColour,
display_res: Resolution) -> Image:
# Rotate
out_img = img.rotate(angle=rotation, expand=True)
Expand All @@ -21,6 +21,9 @@ def resize_img(img: Image, mode: Mode, rotation: Rotation, resize: Resize, backg

scaled_image = out_img.resize(scaled_resolution)

dither_setting = Image.Dither.FLOYDSTEINBERG if dither else Image.Dither.NONE
scaled_image = scaled_image.convert('1', dither=dither_setting)

# Add scaled image to full size canvas
bg_colour = 255 if background == BackgroundColour.WHITE else 0
x = int((display_res.width - scaled_image.width) / 2)
Expand Down
4 changes: 3 additions & 1 deletion src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ def show_image() -> tuple[Response, int]:
except ValueError:
return jsonify(message=f'"mode" invalid, must be one of {", ".join([x for x in Mode])}'), 422

image_to_display = resize_img(image, mode, rotate, resize, background, DISPLAY_RESOLUTION)
dither = request.form.get('dither', 'true') in ['true', '1']

image_to_display = resize_img(image, dither, rotate, resize, background, DISPLAY_RESOLUTION)

try:
update_image = image_changed(Image.open(IMG_PATH), image_to_display)
Expand Down

0 comments on commit dbd204e

Please sign in to comment.