improving rendering, added timers

This commit is contained in:
admin 2026-01-29 13:38:35 -08:00
parent a6859f30a5
commit 0ca8f00d1b

View File

@ -234,7 +234,16 @@ 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=(15,15)): 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 = ImageOps.expand(Image.open(image_path).convert('L'), border=200,fill=0) #img = ImageOps.expand(Image.open(image_path).convert('L'), border=200,fill=0)
orig=Image.open(image_path).convert('L')
logging.debug('orig image size is [{}]'.format(orig.size))
img= Image.new(mode='L', size=(1224,1224), color=0)
logging.debug('background image size is [{}]'.format(img.size))
img.paste(orig,
( (1224-orig.size[0])//2,
(1224-orig.size[1])//2) )
img.save("frame.png")
width, height = img.size width, height = img.size
grid_w, grid_h = grid_size grid_w, grid_h = grid_size
@ -249,13 +258,13 @@ def get_image_grid_max(image_path, grid_size=(15,15)):
# 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.median(np.array(cell)) max_values[r, c] = np.mean(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, 800)): def create_3d_line_chart_svg(data, output_path, svg_size=(1200, 1200)):
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 +274,10 @@ def create_3d_line_chart_svg(data, output_path, svg_size=(800, 800)):
# 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=(0, 0)): def project(x, y, z, xscale=20, yscale=10, offset=(0, 0)):
return (offset[0] + x * scale, offset[1] + y * scale + z * 1) shelf = 0 if z == 0 else z+2
#shelf = z
return (offset[0] + x * xscale, offset[1] + y * yscale + shelf/2)
#return (offset[0] + x * scale + z * 2, offset[1] + y * scale + z * 2) #return (offset[0] + x * scale + z * 2, offset[1] + y * scale + z * 2)
rows, cols = data.shape rows, cols = data.shape
@ -278,16 +289,23 @@ def create_3d_line_chart_svg(data, output_path, svg_size=(800, 800)):
# Draw Lines along rows # Draw Lines along rows
cr.set_source_rgb(0, 0, 0) # Black lines cr.set_source_rgb(0, 0, 0) # Black lines
cr.set_line_width(1.0) cr.set_line_width(1.0)
lastz = 0
lasty = 0
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, scale=800/cols) # -z to make higher values go "up" in 2D x, y = project(c, r, -z, xscale=800/cols, yscale=800/rows) # -z to make higher values go "up" in 2D
if c == 0: if c == 0:
cr.move_to(x, y+50) cr.move_to(x, y+50)
cr.line_to(x, y) cr.line_to(x, y)
else: else:
if lastz == 0:
cr.line_to(x, lasty)
if z == 0 and lastz != 0:
cr.line_to(x, lasty)
cr.line_to(x, y) cr.line_to(x, y)
lastz=z
lasty=y
cr.move_to(x, y + 50) cr.move_to(x, y + 50)
cr.close_path() cr.close_path()
cr.set_source_rgb(1,1,1) cr.set_source_rgb(1,1,1)
@ -313,9 +331,14 @@ def create_3d_line_chart_svg(data, output_path, svg_size=(800, 800)):
# Process # Process
grid_max = get_image_grid_max(target_filename, grid_size=(50,30)) t = Timerlog("Gridify png file").start()
grid_max = get_image_grid_max(target_filename, grid_size=(80,80))
print("Grid Max Values:\n", grid_max) print("Grid Max Values:\n", grid_max)
logging.debug(t.end().report())
# Plot # Plot
create_3d_line_chart_svg(grid_max, target_region_name+".svg", svg_size=Image.open(target_filename).size) t = Timerlog("Convert to SVG").start()
create_3d_line_chart_svg(grid_max, target_region_name+".svg", svg_size=(1200,1200))
print("SVG file created: "+ target_region_name+".svg") print("SVG file created: "+ target_region_name+".svg")
logging.debug(t.end().report())