better color handling

This commit is contained in:
Remy Moll 2023-11-20 14:41:40 +01:00
parent 08bdfe970c
commit 3d27a0ee04
8 changed files with 35 additions and 30 deletions

1
README
View File

@ -2,6 +2,7 @@
## Deployment ## Deployment
Run the ansible playbooks (`setup` once, `deploy` on each code update): Run the ansible playbooks (`setup` once, `deploy` on each code update):
(Don't forget to set variables!)
``` ```
ansible-playbook deploy/setup.playbook.yml -i <ip of pi> ansible-playbook deploy/setup.playbook.yml -i <ip of pi>
ansible-playbook deploy/deploy.playbook.yml -i <ip of pi> ansible-playbook deploy/deploy.playbook.yml -i <ip of pi>

View File

@ -33,7 +33,7 @@
- name: Copy keys python file - name: Copy keys python file
copy: copy:
src: ../keys.py src: ../src/keys.py
dest: "{{ code_dest }}/keys.py" dest: "{{ code_dest }}/keys.py"
- name: Copy unit files - name: Copy unit files

View File

@ -58,14 +58,6 @@
delay: 5 delay: 5
timeout: 300 timeout: 300
# - name: Add wifi networks from template
# template:
# src: templates/wpa_supplicant.conf.j2
# dest: /etc/wpa_supplicant/wpa_supplicant.conf
# owner: root
# group: root
# mode: 0600
- name: Use raspi-config to set WIFI country - name: Use raspi-config to set WIFI country
shell: | shell: |
raspi-config nonint do_wifi_country CH raspi-config nonint do_wifi_country CH

View File

@ -17,7 +17,7 @@ class ImageGetCombined:
+ [ugettter] * int(other_image_percent/2) \ + [ugettter] * int(other_image_percent/2) \
+ [mgetter] * int(other_image_percent/2) + [mgetter] * int(other_image_percent/2)
# representing weighted probabilities # representing weighted probabilities
print(f"Using {len(self.getters)} image sources: ~{own_image_percent}% own, ~{other_image_percent}% foreign images.") print(f"Using 3 image sources: ~{own_image_percent}% own, ~{other_image_percent}% foreign images.")
def get_random_image(self) -> bytearray: def get_random_image(self) -> bytearray:

View File

@ -44,7 +44,7 @@ class ImageGetMuseum(ImageGet):
response = h.post(self.base_url + "search", json=post_data, headers=self.headers) response = h.post(self.base_url + "search", json=post_data, headers=self.headers)
if not response.status_code == 200: if not response.status_code == 200:
raise ImageGetException("Error in step get_image_file: " + str(response.status_code)) raise ImageGetException("Error in step get_random_image_ids: " + str(response.status_code))
response = json.loads(response.text) response = json.loads(response.text)
images = response["data"] images = response["data"]
@ -60,7 +60,9 @@ class ImageGetMuseum(ImageGet):
raise ImageGetException("Error in step get_image_file: " + str(response.status_code)) raise ImageGetException("Error in step get_image_file: " + str(response.status_code))
response = json.loads(response.text) response = json.loads(response.text)
image_url = response["config"]["iiif_url"] + "/" + response["data"]["image_id"] + "/full/843,/0/default.jpg" image_url = response["config"]["iiif_url"] + f"/{response['data']['image_id']}/full/843,/0/default.jpg"
image = h.get(image_url).content try:
image = h.get(image_url).content
except (h.ConnectError, h.NetworkError, h.RequestError, h.HTTPStatusError) as e:
raise ImageGetException(f"Error in step get_image_file: {e}")
return image return image

View File

@ -44,8 +44,11 @@ class ImageGet:
for i, id in enumerate(ids): for i, id in enumerate(ids):
print(f"Caching image {i + 1}") print(f"Caching image {i + 1}")
new_cache = self.cache_dir / f"{uuid.uuid4()}" new_cache = self.cache_dir / f"{uuid.uuid4()}"
new_cache.write_bytes(self.get_image_file(id)) try:
new_cache.write_bytes(self.get_image_file(id))
except ImageGetException:
print("Could not cache image, skipping")
continue
def load_cached_file(self) -> bytearray: def load_cached_file(self) -> bytearray:
"""Returns a random file from self.cache_dir""" """Returns a random file from self.cache_dir"""

View File

@ -32,9 +32,8 @@ class ImageShrink:
"""Shrinks a given image (bytearray) to a given resolution (width, height)""" """Shrinks a given image (bytearray) to a given resolution (width, height)"""
resolution = (480, 800) resolution = (480, 800)
def __init__(self, dither=True, colors=7) -> None: def __init__(self, reduce_colors=False) -> None:
self.dither = dither self.reduce_colors = reduce_colors
self.colors = colors
def convert(self, image: bytearray) -> Image: def convert(self, image: bytearray) -> Image:
@ -61,8 +60,13 @@ class ImageShrink:
if image.mode != "RGB": if image.mode != "RGB":
print("Converting image to RGB") print("Converting image to RGB")
image = image.convert("RGB") image = image.convert("RGB")
if self.colors == -1: if not self.reduce_colors:
print("Skipping color reduction") print("Not manually reducing colors")
return image return image
new_image = image.quantize(colors = len(palette), palette=ref_image, dither=self.dither)
new_image = image.quantize(
colors = len(palette),
palette = ref_image,
dither = True
)
return new_image return new_image

View File

@ -9,14 +9,17 @@ if len(sys.argv) == 2 and sys.argv[1] == "test":
shower.draw_sample_image() shower.draw_sample_image()
sys.exit() sys.exit()
shrink_kwargs = {} shrink_kwargs = {}
if "nodither" in sys.argv: # if "dither" in sys.argv:
print("Disabling dithering") # print("Enabling dithering")
shrink_kwargs["dither"] = False # shrink_kwargs["dither"] = True
if "noreduce" in sys.argv: # if "noreduce" in sys.argv:
print("Disabling color reduction") # print("Disabling color reduction")
shrink_kwargs["colors"] = -1 # shrink_kwargs["colors"] = -1
if "reduce" in sys.argv:
print("Enabling color reduction")
shrink_kwargs["reduce_colors"] = True
getter = ImageGetCombined() getter = ImageGetCombined()
converter = ImageShrink(**shrink_kwargs) converter = ImageShrink(**shrink_kwargs)