add cairosvg and pandas/numpy to draw rudimentary svg file
This commit is contained in:
parent
28c00e18a5
commit
bee9e37e6e
@ -224,5 +224,88 @@ else:
|
|||||||
create_elevation_file_from_region_name(target_region_name, target_filename)
|
create_elevation_file_from_region_name(target_region_name, target_filename)
|
||||||
|
|
||||||
|
|
||||||
pil_image = Image.open(target_filename)
|
#pil_image = Image.open(target_filename)
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
import cairocffi as cairo
|
||||||
|
|
||||||
|
|
||||||
|
# 1. Load Image and get Max Values in a 5x7 Grid
|
||||||
|
def get_image_grid_max(image_path, grid_size=(5, 7)):
|
||||||
|
# Open image and convert to grayscale for pixel intensity
|
||||||
|
img = Image.open(image_path).convert('L')
|
||||||
|
width, height = img.size
|
||||||
|
grid_w, grid_h = grid_size
|
||||||
|
|
||||||
|
# Calculate cell dimensions
|
||||||
|
cell_w = width // grid_w
|
||||||
|
cell_h = height // grid_h
|
||||||
|
|
||||||
|
# Calculate max values for each cell
|
||||||
|
max_values = np.zeros((grid_h, grid_w))
|
||||||
|
for r in range(grid_h):
|
||||||
|
for c in range(grid_w):
|
||||||
|
# Define box (left, upper, right, lower)
|
||||||
|
box = (c * cell_w, r * cell_h, (c + 1) * cell_w, (r + 1) * cell_h)
|
||||||
|
cell = img.crop(box)
|
||||||
|
max_values[r, c] = np.max(np.array(cell))
|
||||||
|
|
||||||
|
return max_values
|
||||||
|
|
||||||
|
|
||||||
|
# 2. Create 3D Line Chart using Cairo (No Matplotlib)
|
||||||
|
def create_3d_line_chart_svg(data, output_path, svg_size=(800, 600)):
|
||||||
|
surface = cairo.SVGSurface(output_path, svg_size[0], svg_size[1])
|
||||||
|
cr = cairo.Context(surface)
|
||||||
|
|
||||||
|
# Background
|
||||||
|
cr.set_source_rgb(1, 1, 1)
|
||||||
|
cr.paint()
|
||||||
|
|
||||||
|
# Simple Orthographic Projection Matrix (pseudo-3D)
|
||||||
|
# Projects (x,y,z) -> (x+z/2, y+z/2)
|
||||||
|
def project(x, y, z, scale=20, offset=(100, 100)):
|
||||||
|
return (offset[0] + x * scale + z * 2, offset[1] + y * scale + z * 2)
|
||||||
|
|
||||||
|
rows, cols = data.shape
|
||||||
|
max_val = data.max()
|
||||||
|
|
||||||
|
# Normalize data for visualization height (0-100)
|
||||||
|
norm_data = (data / max_val) * 100
|
||||||
|
|
||||||
|
# Draw Lines along rows
|
||||||
|
cr.set_source_rgb(0, 0, 0) # Black lines
|
||||||
|
cr.set_line_width(1.0)
|
||||||
|
|
||||||
|
for r in range(rows):
|
||||||
|
for c in range(cols):
|
||||||
|
z = norm_data[r, c]
|
||||||
|
x, y = project(c, r, -z) # -z to make higher values go "up" in 2D
|
||||||
|
if c == 0:
|
||||||
|
cr.move_to(x, y)
|
||||||
|
else:
|
||||||
|
cr.line_to(x, y)
|
||||||
|
|
||||||
|
# Draw Lines along columns
|
||||||
|
for c in range(cols):
|
||||||
|
for r in range(rows):
|
||||||
|
z = norm_data[r, c]
|
||||||
|
x, y = project(c, r, -z)
|
||||||
|
if r == 0:
|
||||||
|
cr.move_to(x, y)
|
||||||
|
else:
|
||||||
|
cr.line_to(x, y)
|
||||||
|
|
||||||
|
cr.stroke()
|
||||||
|
surface.finish()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Process
|
||||||
|
grid_max = get_image_grid_max(target_filename, grid_size=(25, 27))
|
||||||
|
print("Grid Max Values:\n", grid_max)
|
||||||
|
|
||||||
|
# Plot
|
||||||
|
create_3d_line_chart_svg(grid_max, target_region_name+".svg")
|
||||||
|
print("SVG file created: "+ target_region_name+".svg")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user