Skip to content

Commit

Permalink
Apply monkeypatch from: flopp/py-staticmaps#39 (comment) & update req…
Browse files Browse the repository at this point in the history
…uirements.txt
  • Loading branch information
jeremiah-k committed Dec 25, 2024
1 parent 54036b1 commit 48d8482
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
16 changes: 10 additions & 6 deletions plugins/map_plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
--- START OF FILE map_plugin.py ---

import io
import math
import random
Expand All @@ -10,7 +12,6 @@

from plugins.base_plugin import BasePlugin


class TextLabel(staticmaps.Object):
def __init__(self, latlng: s2sphere.LatLng, text: str, fontSize: int = 12) -> None:
staticmaps.Object.__init__(self)
Expand Down Expand Up @@ -144,7 +145,6 @@ def render_svg(self, renderer: staticmaps.SvgRenderer) -> None:
)
)


def anonymize_location(lat, lon, radius=1000):
# Generate random offsets for latitude and longitude
lat_offset = random.uniform(-radius / 111320, radius / 111320)
Expand All @@ -158,7 +158,6 @@ def anonymize_location(lat, lon, radius=1000):

return new_lat, new_lon


def get_map(locations, zoom=None, image_size=None, anonymize=True, radius=10000):
"""
Anonymize a location to 10km by default
Expand Down Expand Up @@ -187,7 +186,6 @@ def get_map(locations, zoom=None, image_size=None, anonymize=True, radius=10000)
else:
return context.render_pillow(1000, 1000)


async def upload_image(client: AsyncClient, image: Image.Image) -> UploadResponse:
buffer = io.BytesIO()
image.save(buffer, format="PNG")
Expand All @@ -202,7 +200,6 @@ async def upload_image(client: AsyncClient, image: Image.Image) -> UploadRespons

return response


async def send_room_image(
client: AsyncClient, room_id: str, upload_response: UploadResponse
):
Expand All @@ -212,11 +209,18 @@ async def send_room_image(
content={"msgtype": "m.image", "url": upload_response.content_uri, "body": ""},
)


async def send_image(client: AsyncClient, room_id: str, image: Image.Image):
response = await upload_image(client=client, image=image)
await send_room_image(client, room_id, upload_response=response)

# Monkeypatch fix for https://github.com/flopp/py-staticmaps/issues/39
import PIL.ImageDraw

def textsize(self: PIL.ImageDraw.ImageDraw, *args, **kwargs):
x, y, w, h = self.textbbox((0, 0), *args, **kwargs)
return w, h

PIL.ImageDraw.ImageDraw.textsize = textsize

class Plugin(BasePlugin):
plugin_name = "map"
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
meshtastic
Pillow==11
py-staticmaps==0.4.0
git+https://github.com/flopp/py-staticmaps.git@e0266dc40163e87ce42a0ea5d8836a9a4bd92208
matrix-nio==0.25.2
matplotlib==3.9.0
requests==2.32.3
markdown==3.4.3
haversine==2.8.0
schedule==1.2.0
schedule==1.2.0

0 comments on commit 48d8482

Please sign in to comment.