-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (55 loc) · 2.21 KB
/
main.py
File metadata and controls
70 lines (55 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import math
import typer
from rich import print
from pathlib import Path
from FileReader import Reader
from Drawer import ImageCreator
from HashToColor import HashToColor
from Decoder import Decoder
app = typer.Typer(help="🎵🔵 Generate or decode a color grid image from a WAV file hash.")
# Hardcoded byte length for now
BYTE_SIZE = 450 # Change this if needed
CELL_SIZE = 20 # pixels per cell
@app.command()
def generate(
filename: Path = typer.Argument(..., exists=True, readable=True, help="Path to the WAV file."),
output: Path = typer.Option("hash_grid.png", help="Output image file name.")
):
"""
Generate a color grid image from a WAV file's hash.
"""
try:
print(f"[green]📂 Reading file:[/green] {filename}")
fr = Reader(str(filename))
hashed_string = fr.hash_file(BYTE_SIZE)
print(f"[blue]🔢 Hashed string length:[/blue] {len(hashed_string)} hex chars")
stc = HashToColor()
color_list = stc.get_colors_list(hashed_string)
grid_size = int(math.isqrt(len(hashed_string))) # Square grid
print(f"[green]🎨 Generating image ({grid_size}x{grid_size})...[/green]")
image = ImageCreator().draw_image(color_list=color_list, cell_size=CELL_SIZE, grid_size=grid_size)
image.save(output)
image.show()
print(f"[bold green]✅ Image saved to:[/bold green] {output}")
except Exception as e:
print(f"[red]❌ Error:[/red] {e}")
raise typer.Exit(code=1)
@app.command()
def decode(
image_path: Path = typer.Argument(..., exists=True, readable=True, help="Path to the hash image.")
):
"""
Decode a color grid image back to its hash string.
"""
try:
print(f"[cyan]🖼️ Decoding image:[/cyan] {image_path}")
stc = HashToColor()
decoder = Decoder(str(image_path), CELL_SIZE, grid_size=math.isqrt(2*BYTE_SIZE))
hashed = decoder.decode_image_to_hash(stc.rgb_to_hex_map)
print(f"[bold blue]🔓 Decoded hash:[/bold blue] {hashed}")
print(f"[bold blue]🧮 Length:[/bold blue] {len(hashed)} hex chars")
except Exception as e:
print(f"[red]❌ Error:[/red] {e}")
raise typer.Exit(code=1)
if __name__ == "__main__":
app()