Skip to content
Merged
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
38 changes: 20 additions & 18 deletions SCR/valetudo_map_parser/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@
from .drawable import Drawable
from .drawable_elements import DrawableElement, DrawingConfig
from .enhanced_drawable import EnhancedDrawable
from .types import LOGGER, ChargerPosition, ImageSize, NumpyArray, PilPNG, RobotPosition, WebPBytes
from .types import (
LOGGER,
ChargerPosition,
ImageSize,
NumpyArray,
PilPNG,
RobotPosition,
WebPBytes,
)


@dataclass
Expand Down Expand Up @@ -843,10 +851,8 @@ def calculate_angle(point):


async def numpy_to_webp_bytes(
img_np_array: np.ndarray,
quality: int = 85,
lossless: bool = False
) -> bytes:
img_np_array: np.ndarray, quality: int = 85, lossless: bool = False
) -> WebPBytes:
"""
Convert NumPy array directly to WebP bytes.

Expand All @@ -864,13 +870,12 @@ async def numpy_to_webp_bytes(
# Create bytes buffer
webp_buffer = io.BytesIO()

# Save as WebP
# Save as WebP - PIL images should use lossless=True for best results
pil_img.save(
webp_buffer,
format='WEBP',
quality=quality,
lossless=lossless,
method=6 # Best compression method
format="WEBP",
lossless=True, # Always lossless for PIL images
method=1, # Fastest method for lossless
)

# Get bytes and cleanup
Expand All @@ -881,9 +886,7 @@ async def numpy_to_webp_bytes(


async def pil_to_webp_bytes(
pil_img: Image.Image,
quality: int = 85,
lossless: bool = False
pil_img: Image.Image, quality: int = 85, lossless: bool = False
) -> bytes:
"""
Convert PIL Image to WebP bytes.
Expand All @@ -899,13 +902,12 @@ async def pil_to_webp_bytes(
# Create bytes buffer
webp_buffer = io.BytesIO()

# Save as WebP
# Save as WebP - PIL images should use lossless=True for best results
pil_img.save(
webp_buffer,
format='WEBP',
quality=quality,
lossless=lossless,
method=6 # Best compression method
format="WEBP",
lossless=True, # Always lossless for PIL images
method=1, # Fastest method for lossless
)

# Get bytes and cleanup
Expand Down
13 changes: 3 additions & 10 deletions SCR/valetudo_map_parser/hypfer_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,23 +379,16 @@ async def async_get_image_from_json(
# Return WebP bytes or PIL Image based on parameter
if return_webp:
from .config.utils import pil_to_webp_bytes
webp_bytes = await pil_to_webp_bytes(
resized_image,
quality=90,
lossless=False
)

webp_bytes = await pil_to_webp_bytes(resized_image)
return webp_bytes
else:
return resized_image
else:
# Return WebP bytes or PIL Image based on parameter
if return_webp:
# Convert directly from NumPy to WebP for better performance
webp_bytes = await numpy_to_webp_bytes(
img_np_array,
quality=90,
lossless=False
)
webp_bytes = await numpy_to_webp_bytes(img_np_array)
del img_np_array
LOGGER.debug("%s: Frame Completed.", self.file_name)
return webp_bytes
Expand Down
6 changes: 1 addition & 5 deletions SCR/valetudo_map_parser/rand256_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,7 @@ async def get_image_from_rrm(
# Return WebP bytes or PIL Image based on parameter
if return_webp:
# Convert directly to WebP bytes for better performance
webp_bytes = await numpy_to_webp_bytes(
img_np_array,
quality=90, # High quality for vacuum maps
lossless=False, # Use lossy compression for smaller size
)
webp_bytes = await numpy_to_webp_bytes(img_np_array)
del img_np_array # free memory
return webp_bytes
else:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "valetudo-map-parser"
version = "0.1.9b58"
version = "0.1.9b59"
description = "A Python library to parse Valetudo map data returning a PIL Image object."
authors = ["Sandro Cantarella <gsca075@gmail.com>"]
license = "Apache-2.0"
Expand Down