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 from os.path import exists
import requests import requests
import shutil import shutil
from PIL import Image from PIL import Image, ImageOps
from io import BytesIO from io import BytesIO
import ee import ee
@ -232,9 +232,9 @@ import cairocffi as cairo
# 1. Load Image and get Max Values in a 5x7 Grid # 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 # 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 width, height = img.size
grid_w, grid_h = grid_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) # Define box (left, upper, right, lower)
box = (c * cell_w, r * cell_h, (c + 1) * cell_w, (r + 1) * cell_h) box = (c * cell_w, r * cell_h, (c + 1) * cell_w, (r + 1) * cell_h)
cell = img.crop(box) 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 return max_values
# 2. Create 3D Line Chart using Cairo (No Matplotlib) # 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]) surface = cairo.SVGSurface(output_path, svg_size[0], svg_size[1])
cr = cairo.Context(surface) 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) # Simple Orthographic Projection Matrix (pseudo-3D)
# Projects (x,y,z) -> (x+z/2, y+z/2) # Projects (x,y,z) -> (x+z/2, y+z/2)
def project(x, y, z, scale=20, offset=(100, 100)): def project(x, y, z, scale=20, offset=(0, 0)):
return (offset[0] + x * scale + z * 2, offset[1] + y * scale + z * 2) 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 rows, cols = data.shape
max_val = data.max() max_val = data.max()
@ -281,12 +282,21 @@ def create_3d_line_chart_svg(data, output_path, svg_size=(800, 600)):
for r in range(rows): for r in range(rows):
for c in range(cols): for c in range(cols):
z = norm_data[r, c] 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: if c == 0:
cr.move_to(x, y) cr.move_to(x, y+50)
cr.line_to(x, y)
else: else:
cr.line_to(x, y) 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()
if False:
# Draw Lines along columns # Draw Lines along columns
for c in range(cols): for c in range(cols):
for r in range(rows): for r in range(rows):
@ -297,15 +307,15 @@ def create_3d_line_chart_svg(data, output_path, svg_size=(800, 600)):
else: else:
cr.line_to(x, y) cr.line_to(x, y)
cr.stroke() #cr.stroke()
surface.finish() surface.finish()
# Process # 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) print("Grid Max Values:\n", grid_max)
# Plot # 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") print("SVG file created: "+ target_region_name+".svg")