Surn commited on
Commit
ae12d34
·
1 Parent(s): 4f15013

Hura update

Browse files
Files changed (4) hide show
  1. README.md +1 -1
  2. style_20250314.css +1 -1
  3. utils/file_utils.py +18 -1
  4. utils/hex_hura.py +127 -0
README.md CHANGED
@@ -5,7 +5,7 @@ colorFrom: yellow
5
  colorTo: purple
6
  sdk: gradio
7
  python_version: 3.10.13
8
- sdk_version: 5.23.1
9
  app_file: app.py
10
  pinned: true
11
  short_description: Transform Your Images into Mesmerizing Hexagon Grids
 
5
  colorTo: purple
6
  sdk: gradio
7
  python_version: 3.10.13
8
+ sdk_version: 5.23.3
9
  app_file: app.py
10
  pinned: true
11
  short_description: Transform Your Images into Mesmerizing Hexagon Grids
style_20250314.css CHANGED
@@ -21,7 +21,7 @@
21
  background-color: rgba(242, 218, 163, 0.62);
22
  }
23
 
24
- .dark .gradio-container.gradio-container-5-23-1 .contain .intro .prose {
25
  background-color: rgba(41, 18, 5, 0.38) !important;
26
  }
27
  .toast-body.info {
 
21
  background-color: rgba(242, 218, 163, 0.62);
22
  }
23
 
24
+ .dark .gradio-container.gradio-container-5-23-3 .contain .intro .prose {
25
  background-color: rgba(41, 18, 5, 0.38) !important;
26
  }
27
  .toast-body.info {
utils/file_utils.py CHANGED
@@ -60,4 +60,21 @@ def rename_file_to_lowercase_extension(file_path: str) -> str:
60
  raise inner_e
61
  return new_file_path
62
  else:
63
- return file_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  raise inner_e
61
  return new_file_path
62
  else:
63
+ return file_path
64
+
65
+ def get_filename(file):
66
+ # extract filename from file object
67
+ filename = None
68
+ if file is not None:
69
+ filename = file.name
70
+ return filename
71
+
72
+ def convert_title_to_filename(title):
73
+ # convert title to filename
74
+ filename = title.lower().replace(" ", "_").replace("/", "_")
75
+ return filename
76
+
77
+ def get_filename_from_filepath(filepath):
78
+ file_name = os.path.basename(filepath)
79
+ file_base, file_extension = os.path.splitext(file_name)
80
+ return file_base, file_extension
utils/hex_hura.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import math
3
+
4
+ v = 139
5
+ r = 5
6
+
7
+ # Define the smoothstep function for vignette effect
8
+ def smoothstep(edge0, edge1, x):
9
+ """Smoothly interpolate between edge0 and edge1 based on x."""
10
+ if edge0 == edge1:
11
+ return 0.0 if x < edge0 else 1.0
12
+ t = min(max((x - edge0) / (edge1 - edge0), 0.0), 1.0)
13
+ return t * t * (3 - 2 * t)
14
+
15
+ # Define the hexagon function to compute coordinates
16
+ def hexagon(p):
17
+ """
18
+ Compute hexagon coordinates and distances for a given point p.
19
+ Returns (h_x, h_y, e, f) where h_x and h_y are integer coordinates.
20
+ """
21
+ # Transform to hexagonal coordinate system
22
+ q = (p[0] * 2.0 * 0.5773503, p[1] + p[0] * 0.5773503)
23
+ pi = (math.floor(q[0]), math.floor(q[1]))
24
+ pf = (q[0] - pi[0], q[1] - pi[1])
25
+ v = (pi[0] + pi[1]) % 3.0
26
+ ca = 1.0 if v >= 1.0 else 0.0
27
+ cb = 1.0 if v >= 2.0 else 0.0
28
+ ma = (1.0 if pf[1] >= pf[0] else 0.0, 1.0 if pf[0] >= pf[1] else 0.0)
29
+ temp = (
30
+ 1.0 - pf[1] + ca * (pf[0] + pf[1] - 1.0) + cb * (pf[1] - 2.0 * pf[0]),
31
+ 1.0 - pf[0] + ca * (pf[0] + pf[1] - 1.0) + cb * (pf[0] - 2.0 * pf[1])
32
+ )
33
+ e = ma[0] * temp[0] + ma[1] * temp[1]
34
+ p2_x = (q[0] + math.floor(0.5 + p[1] / 1.5)) * 0.5 + 0.5
35
+ p2_y = (4.0 * p[1] / 3.0) * 0.5 + 0.5
36
+ fract_p2 = (p2_x - math.floor(p2_x), p2_y - math.floor(p2_y))
37
+ f = math.sqrt((fract_p2[0] - 0.5)**2 + ((fract_p2[1] - 0.5) * 0.85)**2)
38
+ h_xy = (pi[0] + ca - cb * ma[0], pi[1] + ca - cb * ma[1])
39
+ return (h_xy[0], h_xy[1], e, f)
40
+
41
+ def ura(p, r=1.0, v=10.0):
42
+ """
43
+ Generate a grayscale value based on hexagonal coordinates, emulating the HLSL URA function.
44
+
45
+ Parameters:
46
+ p (tuple): A 2-tuple (x, y) of coordinates.
47
+ r (float): Multiplier for p[0]. Default is 1.0.
48
+ v (float): Modulus value controlling the pattern frequency. Default is 10.0.
49
+
50
+ Returns:
51
+ float: 1.0 if no pattern match is found, otherwise 0.0.
52
+ """
53
+ import math
54
+ l = math.fmod(p[1] + r * p[0], v)
55
+ rz = 1.0
56
+ for i in range(1, int(v/2)):
57
+ if math.isclose(math.fmod(i * i, v), l, abs_tol=1e-6):
58
+ rz = 0.0
59
+ break
60
+ return rz
61
+
62
+ # Define the color palette
63
+ default_colors = [
64
+ (255, 0, 0), # Red
65
+ (0, 255, 0), # Green
66
+ (0, 0, 255) # Blue
67
+ ]
68
+
69
+ # Generate the image with colorful_hexagonal pattern
70
+ def generate_image_color(width, height, colors=default_colors):
71
+ """Generate an RGB image with a colorful hexagonal pattern."""
72
+ img = Image.new('RGB', (width, height))
73
+ aspect = width / height
74
+ for j in range(height):
75
+ for i in range(width):
76
+ # Normalize pixel coordinates to [0, 1]
77
+ q_x = i / width
78
+ q_y = j / height
79
+ # Transform to centered coordinates with aspect ratio
80
+ p_x = (q_x * 2.0 - 1.0) * aspect
81
+ p_y = q_y * 2.0 - 1.0
82
+ p = (p_x, p_y)
83
+ # Scale coordinates for pattern frequency
84
+ h = hexagon((p[0] * 21.0, p[1] * 21.0))
85
+ h_xy = (int(h[0]), int(h[1]))
86
+ # Assign color based on hexagon coordinates
87
+ color_index = (h_xy[0] + h_xy[1]) % len(colors)
88
+ col = colors[color_index]
89
+ # Apply vignette effect
90
+ q = (q_x * 2.0 - 1.0, q_y * 2.0 - 1.0)
91
+ vignette = smoothstep(1.01, 0.97, max(abs(q[0]), abs(q[1])))
92
+ col = tuple(int(c * vignette) for c in col)
93
+ # Set the pixel color
94
+ img.putpixel((i, j), col)
95
+ return img
96
+
97
+ def generate_image_grayscale(width, height):
98
+ img = Image.new('RGB', (width, height))
99
+ aspect = width / height
100
+ for j in range(height):
101
+ for i in range(width):
102
+ q_x = i / width
103
+ q_y = j / height
104
+ p_x = (q_x * 2.0 - 1.0) * aspect
105
+ p_y = q_y * 2.0 - 1.0
106
+ p = (p_x, p_y)
107
+ h = hexagon((p[0] * 21.0, p[1] * 21.0))
108
+ rz = ura(h[:2])
109
+ smooth = smoothstep(-0.2, 0.13, h[2])
110
+ if rz > 0.5:
111
+ col = smooth
112
+ else:
113
+ col = 1.0 - smooth
114
+ q = (q_x * 2.0 - 1.0, q_y * 2.0 - 1.0)
115
+ vignette = smoothstep(1.01, 0.97, max(abs(q[0]), abs(q[1])))
116
+ col *= vignette
117
+ color = int(col * 255)
118
+ img.putpixel((i, j), (color, color, color))
119
+ return img
120
+
121
+ # Example usage
122
+ #if __name__ == "__main__":
123
+ # # Set image dimensions
124
+ # width, height = 800, 600
125
+ # img = generate_image(width, height)
126
+ # img.save('colorful_hexagon_pattern.png')
127
+ # print("Image saved as 'colorful_hexagon_pattern.png'")