added filled shapes (black lines, white fill), still need to adjust aspect ratio

This commit is contained in:
admin 2026-01-29 11:06:09 -08:00
parent bee9e37e6e
commit a6859f30a5

View File

@ -7,7 +7,7 @@ import os
from os.path import exists
import requests
import shutil
from PIL import Image
from PIL import Image, ImageOps
from io import BytesIO
import ee
@ -232,9 +232,9 @@ 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)):
def get_image_grid_max(image_path, grid_size=(15,15)):
# Open image and convert to grayscale for pixel intensity
img = Image.open(image_path).convert('L')
img = ImageOps.expand(Image.open(image_path).convert('L'), border=200,fill=0)
width, height = img.size
grid_w, grid_h = grid_size
@ -249,13 +249,13 @@ def get_image_grid_max(image_path, grid_size=(5, 7)):
# 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))
max_values[r, c] = np.median(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)):
def create_3d_line_chart_svg(data, output_path, svg_size=(800, 800)):
surface = cairo.SVGSurface(output_path, svg_size[0], svg_size[1])
cr = cairo.Context(surface)
@ -265,8 +265,9 @@ def create_3d_line_chart_svg(data, output_path, svg_size=(800, 600)):
# 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)
def project(x, y, z, scale=20, offset=(0, 0)):
return (offset[0] + x * scale, offset[1] + y * scale + z * 1)
#return (offset[0] + x * scale + z * 2, offset[1] + y * scale + z * 2)
rows, cols = data.shape
max_val = data.max()
@ -281,31 +282,40 @@ def create_3d_line_chart_svg(data, output_path, svg_size=(800, 600)):
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
x, y = project(c, r, -z, scale=800/cols) # -z to make higher values go "up" in 2D
if c == 0:
cr.move_to(x, y)
cr.move_to(x, y+50)
cr.line_to(x, y)
else:
cr.line_to(x, y)
cr.move_to(x, y + 50)
cr.close_path()
cr.set_source_rgb(1,1,1)
cr.fill_preserve()
cr.set_source_rgb(0,0,0)
cr.set_line_width(1.0)
cr.stroke()
# 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)
if False:
# 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()
#cr.stroke()
surface.finish()
# Process
grid_max = get_image_grid_max(target_filename, grid_size=(25, 27))
grid_max = get_image_grid_max(target_filename, grid_size=(50,30))
print("Grid Max Values:\n", grid_max)
# Plot
create_3d_line_chart_svg(grid_max, target_region_name+".svg")
create_3d_line_chart_svg(grid_max, target_region_name+".svg", svg_size=Image.open(target_filename).size)
print("SVG file created: "+ target_region_name+".svg")