Skip to content

Commit

Permalink
Adds darkmode
Browse files Browse the repository at this point in the history
  • Loading branch information
FL550 committed Jan 26, 2025
1 parent 0b50d8d commit 56f24ed
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class MarkerShape(Enum):
CIRCLE = "circle"
SQUARE = "square"
CROSS = "cross"

class Marker(
latitude: float,
longitude: float,
Expand All @@ -198,11 +198,11 @@ class Marker(
width: int = 0,
)

get_from_location(longitude, latitude, radius_km, map_type: WeatherMapType, background_type: WeatherBackgroundMapType, optional integer image_width, optional integer image_height, optional markers: list[Marker]) #Returns map as pillow image with given radius from coordinates
get_from_location(longitude, latitude, radius_km, map_type: WeatherMapType, background_type: WeatherBackgroundMapType, optional integer image_width, optional integer image_height, optional markers: list[Marker], optional bool dark_mode) #Returns map as pillow image with given radius from coordinates

get_germany(map_type: WeatherMapType, optional WeatherBackgroundMapType background_type, optional integer image_width, optional integer image_height, optional markers: list[Marker]) #Returns map as pillow image of whole germany
get_germany(map_type: WeatherMapType, optional WeatherBackgroundMapType background_type, optional integer image_width, optional integer image_height, optional markers: list[Marker], optional bool dark_mode) #Returns map as pillow image of whole germany

get_map(minx,miny,maxx,maxy, map_type: WeatherMapType, background_type: WeatherBackgroundMapType, optional integer image_width, optional integer image_height, optional markers: list[Marker]) #Returns map as pillow image
get_map(minx,miny,maxx,maxy, map_type: WeatherMapType, background_type: WeatherBackgroundMapType, optional integer image_width, optional integer image_height, optional markers: list[Marker], optional bool dark_mode) #Returns map as pillow image
```


Expand Down Expand Up @@ -234,7 +234,7 @@ for image in enumerate(maploop._images):

```python
ImageLoop(minx: float, miny: float, maxx: float, maxy: float, map_type: WeatherMapType, background_type: WeatherBackgroundMapType,
steps: int = 6, image_width: int = 520,image_height: int = 580, markers: list[Marker] = []) -> ImageLoop
steps: int = 6, image_width: int = 520,image_height: int = 580, markers: list[Marker] = [], optional bool dark_mode) -> ImageLoop

get_images() -> Iterable[ImageFile.ImageFile] # Returns the image loop

Expand Down
29 changes: 28 additions & 1 deletion simple_dwd_weatherforecast/dwdmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def get_from_location(
image_width=520,
image_height=580,
markers: list[Marker] = [],
dark_mode: bool = False,
):
if radius_km <= 0:
raise ValueError("Radius must be greater than 0")
Expand All @@ -107,6 +108,7 @@ def get_from_location(
image_width,
image_height,
markers,
dark_mode,
)


Expand All @@ -116,6 +118,7 @@ def get_germany(
image_width=520,
image_height=580,
markers: list[Marker] = [],
dark_mode: bool = False,
):
return get_map(
germany_boundaries.minx,
Expand All @@ -127,6 +130,7 @@ def get_germany(
image_width,
image_height,
markers,
dark_mode,
)


Expand All @@ -140,6 +144,7 @@ def get_map(
image_width=520,
image_height=580,
markers: list[Marker] = [],
dark_mode: bool = False,
):
if image_width > 1200 or image_height > 1400:
raise ValueError(
Expand All @@ -157,6 +162,16 @@ def get_map(
request = requests.get(url, stream=True)
if request.status_code == 200:
image = Image.open(BytesIO(request.content))
if dark_mode:
new_image_data = []
for item in image.getdata(): # type: ignore
if item[0] == 255 and item[1] == 255 and item[2] == 255:
new_image_data.append((0, 0, 0))
else:
new_image_data.append(item)

# update image data
image.putdata(new_image_data)
image = draw_marker(image, ImageBoundaries(minx, maxx, miny, maxy), markers)
return image

Expand Down Expand Up @@ -186,6 +201,7 @@ def __init__(
image_width: int = 520,
image_height: int = 580,
markers: list[Marker] = [],
dark_mode: bool = False,
):
if image_width > 1200 or image_height > 1400:
raise ValueError(
Expand All @@ -203,6 +219,7 @@ def __init__(
self._image_width = image_width
self._image_height = image_height
self.markers = markers
self.dark_mode = dark_mode
self._images = deque([], steps)

self._full_reload()
Expand Down Expand Up @@ -246,11 +263,21 @@ def _get_image(
layers = f"{self._background_type.value}, {self._map_type.value}"
else:
layers = f"{self._map_type.value}, {self._background_type.value}"
url = f"https://maps.dwd.de/geoserver/dwd/wms?service=WMS&version=1.1.0&request=GetMap&layers={layers}&bbox={self._minx},{self._miny},{self._maxx},{self._maxy}&width={self._image_width}&height={self._image_height}&srs=EPSG:4326&styles=&format=image/png&TIME={date.strftime("%Y-%m-%dT%H:%M:00.0Z")}"
url = f"https://maps.dwd.de/geoserver/dwd/wms?service=WMS&version=1.1.0&request=GetMap&layers={layers}&bbox={self._minx},{self._miny},{self._maxx},{self._maxy}&width={self._image_width}&height={self._image_height}&srs=EPSG:4326&styles=&format=image/png&TIME={date.strftime('%Y-%m-%dT%H:%M:00.0Z')}"
request = requests.get(url, stream=True)
if request.status_code != 200:
raise ConnectionError("Error during image request from DWD servers")
image = Image.open(BytesIO(request.content))
if self.dark_mode:
new_image_data = []
for item in image.getdata(): # type: ignore
if item[0] == 255 and item[1] == 255 and item[2] == 255:
new_image_data.append((0, 0, 0))
else:
new_image_data.append(item)

# update image data
image.putdata(new_image_data)
image = draw_marker(
image,
ImageBoundaries(self._minx, self._maxx, self._miny, self._maxy),
Expand Down

0 comments on commit 56f24ed

Please sign in to comment.