Animation added
This commit is contained in:
@@ -32,7 +32,8 @@ class ImageRenderer:
|
||||
filename: str,
|
||||
directory: Optional[str] = None,
|
||||
solution_path: Optional[List[Tuple[int, int]]] = None,
|
||||
visited_cells: Optional[List[Tuple[int, int]]] = None
|
||||
visited_cells: Optional[List[Tuple[int, int]]] = None,
|
||||
format: str = 'png'
|
||||
) -> str:
|
||||
"""Render maze as an image.
|
||||
|
||||
@@ -42,6 +43,7 @@ class ImageRenderer:
|
||||
directory: Output directory (uses default if None)
|
||||
solution_path: Optional list of (row, col) tuples for solution
|
||||
visited_cells: Optional list of (row, col) tuples for visited cells
|
||||
format: Image format ('png' or 'jpg')
|
||||
|
||||
Returns:
|
||||
Path to the saved image file
|
||||
@@ -50,9 +52,21 @@ class ImageRenderer:
|
||||
output_dir = Path(directory) if directory else Path(self.DEFAULT_OUTPUT_DIR)
|
||||
output_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Ensure .png extension
|
||||
if not filename.endswith('.png'):
|
||||
filename += '.png'
|
||||
# Ensure correct extension
|
||||
format = format.lower()
|
||||
if format not in ['png', 'jpg', 'jpeg']:
|
||||
format = 'png'
|
||||
|
||||
# Normalize jpg/jpeg
|
||||
if format == 'jpeg':
|
||||
format = 'jpg'
|
||||
|
||||
# Add extension if not present
|
||||
if not filename.endswith(f'.{format}'):
|
||||
# Remove any existing extension
|
||||
if filename.endswith('.png') or filename.endswith('.jpg') or filename.endswith('.jpeg'):
|
||||
filename = filename.rsplit('.', 1)[0]
|
||||
filename += f'.{format}'
|
||||
|
||||
# Calculate image dimensions
|
||||
width = maze.cols * self.cell_size + self.wall_thickness
|
||||
@@ -83,7 +97,19 @@ class ImageRenderer:
|
||||
|
||||
# Save image
|
||||
file_path = output_dir / filename
|
||||
img.save(file_path)
|
||||
|
||||
# Save with appropriate format
|
||||
if format == 'jpg':
|
||||
# Convert RGBA to RGB for JPG (JPG doesn't support transparency)
|
||||
if img.mode == 'RGBA':
|
||||
rgb_img = Image.new('RGB', img.size, (255, 255, 255))
|
||||
rgb_img.paste(img, mask=img.split()[3] if len(img.split()) == 4 else None)
|
||||
rgb_img.save(file_path, 'JPEG', quality=95)
|
||||
else:
|
||||
img.save(file_path, 'JPEG', quality=95)
|
||||
else:
|
||||
img.save(file_path, 'PNG')
|
||||
|
||||
return str(file_path)
|
||||
|
||||
def _draw_cell_background(self, draw: ImageDraw, row: int, col: int, color: str) -> None:
|
||||
|
||||
Reference in New Issue
Block a user