better color handling
This commit is contained in:
		
							
								
								
									
										1
									
								
								README
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								README
									
									
									
									
									
								
							| @@ -2,6 +2,7 @@ | ||||
|  | ||||
| ## Deployment | ||||
| 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/deploy.playbook.yml -i <ip of pi> | ||||
|   | ||||
| @@ -33,7 +33,7 @@ | ||||
|  | ||||
|     - name: Copy keys python file | ||||
|       copy: | ||||
|         src: ../keys.py | ||||
|         src: ../src/keys.py | ||||
|         dest: "{{ code_dest }}/keys.py" | ||||
|  | ||||
|     - name: Copy unit files | ||||
|   | ||||
| @@ -58,14 +58,6 @@ | ||||
|         delay: 5 | ||||
|         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 | ||||
|       shell: | | ||||
|         raspi-config nonint do_wifi_country CH | ||||
|   | ||||
| @@ -17,7 +17,7 @@ class ImageGetCombined: | ||||
|             + [ugettter] * int(other_image_percent/2) \ | ||||
|             + [mgetter] * int(other_image_percent/2) | ||||
|         # 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: | ||||
|   | ||||
| @@ -44,7 +44,7 @@ class ImageGetMuseum(ImageGet): | ||||
|  | ||||
|         response = h.post(self.base_url + "search", json=post_data, headers=self.headers) | ||||
|         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) | ||||
|         images = response["data"] | ||||
| @@ -60,7 +60,9 @@ class ImageGetMuseum(ImageGet): | ||||
|             raise ImageGetException("Error in step get_image_file: " + str(response.status_code)) | ||||
|          | ||||
|         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" | ||||
|         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 | ||||
|  | ||||
|   | ||||
| @@ -44,8 +44,11 @@ class ImageGet: | ||||
|         for i, id in enumerate(ids): | ||||
|             print(f"Caching image {i + 1}") | ||||
|             new_cache = self.cache_dir / f"{uuid.uuid4()}" | ||||
|             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: | ||||
|         """Returns a random file from self.cache_dir""" | ||||
|   | ||||
| @@ -32,9 +32,8 @@ class ImageShrink: | ||||
|     """Shrinks a given image (bytearray) to a given resolution (width, height)""" | ||||
|     resolution = (480, 800) | ||||
|  | ||||
|     def __init__(self, dither=True, colors=7) -> None: | ||||
|         self.dither = dither | ||||
|         self.colors = colors | ||||
|     def __init__(self, reduce_colors=False) -> None: | ||||
|         self.reduce_colors = reduce_colors | ||||
|  | ||||
|  | ||||
|     def convert(self, image: bytearray) -> Image: | ||||
| @@ -61,8 +60,13 @@ class ImageShrink: | ||||
|         if image.mode != "RGB": | ||||
|             print("Converting image to RGB") | ||||
|             image = image.convert("RGB") | ||||
|         if self.colors == -1: | ||||
|             print("Skipping color reduction") | ||||
|         if not self.reduce_colors: | ||||
|             print("Not manually reducing colors") | ||||
|             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 | ||||
|   | ||||
							
								
								
									
										17
									
								
								src/main.py
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								src/main.py
									
									
									
									
									
								
							| @@ -9,14 +9,17 @@ if len(sys.argv) == 2 and sys.argv[1] == "test": | ||||
|     shower.draw_sample_image() | ||||
|     sys.exit() | ||||
|  | ||||
|  | ||||
| shrink_kwargs = {} | ||||
| if "nodither" in sys.argv: | ||||
|     print("Disabling dithering") | ||||
|     shrink_kwargs["dither"] = False | ||||
| if "noreduce" in sys.argv: | ||||
|     print("Disabling color reduction") | ||||
|     shrink_kwargs["colors"] = -1 | ||||
| # if "dither" in sys.argv: | ||||
| #     print("Enabling dithering") | ||||
| #     shrink_kwargs["dither"] = True | ||||
| # if "noreduce" in sys.argv: | ||||
| #     print("Disabling color reduction") | ||||
| #     shrink_kwargs["colors"] = -1 | ||||
| if "reduce" in sys.argv: | ||||
|     print("Enabling color reduction") | ||||
|     shrink_kwargs["reduce_colors"] = True | ||||
|  | ||||
|  | ||||
| getter = ImageGetCombined() | ||||
| converter = ImageShrink(**shrink_kwargs) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user