Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Center smaller artwork in the middle of the screen #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions server/artwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from random import randint

from content import ImageContent
from epd import edge_color

# The directory containing static artwork images.
IMAGES_DIR = 'assets/artwork'
Expand All @@ -27,9 +28,17 @@ def image(self, _, width, height):
image = Image.open(filename)
image = image.convert('RGB')

# Crop the image to a random display-sized area.
x = randint(0, max(0, image.width - width))
y = randint(0, max(0, image.height - height))
image = image.crop((x, y, x + width, y + height))

return image
# Larger images are cropped to a random display-sized area.
# Smaller images are centered in the middle of the display.
if width > image.width:
x = int((width - image.width) / 2)
else:
x = -randint(0, image.width - width)
if height > image.height:
y = int((height - image.height) / 2)
else:
y = -randint(0, image.height - height)

output = Image.new('RGB', (width, height), color=edge_color(image))
output.paste(image, (x, y))
return output
19 changes: 19 additions & 0 deletions server/epd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from numpy import argmax
from numpy import array
from numpy import packbits
from numpy import uint8
from numpy import unique
from PIL import Image
from scipy.cluster.vq import vq

Expand Down Expand Up @@ -49,3 +51,20 @@ def adjust_xy(x, y, width, height):
y += (height - DEFAULT_DISPLAY_HEIGHT) // 2

return x, y


def edge_color(image):
"""Returns the most common color (r, g, b) of the image edge.

Looks at the pixels at the edge of the image and returns the most
common color. This is then used as a background color in case display
size is larger than the image size.
"""
i = array(bwr_image(image))
edge = array(
list(i[0]) + # top
list(i[-1]) + # bottom
[r[0] for r in i] + # left
[r[-1] for r in i]) # right
values, counts = unique(edge, axis=0, return_counts=True)
return tuple(values[argmax(counts)])