qqwjq1981 commited on
Commit
5ae52ca
·
verified ·
1 Parent(s): 8293611

Update utils/image_utils.py

Browse files
Files changed (1) hide show
  1. utils/image_utils.py +52 -17
utils/image_utils.py CHANGED
@@ -1,23 +1,58 @@
1
- from PIL import Image, ImageDraw, ImageFont
2
- import base64
3
- from io import BytesIO
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def split_image(image: Image.Image, num_chunks: int) -> list:
 
 
 
6
  width, height = image.size
7
- chunk_height = height // num_chunks
 
 
8
  chunks = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  for i in range(num_chunks):
10
- top = i * chunk_height
11
- bottom = (i + 1) * chunk_height if i < num_chunks - 1 else height
12
- chunks.append(image.crop((0, top, width, bottom)))
13
- return chunks
14
 
15
- def encode_image_to_html(image: Image.Image) -> str:
16
- buffered = BytesIO()
17
- image.save(buffered, format="PNG")
18
- encoded = base64.b64encode(buffered.getvalue()).decode()
19
- return f"""
20
- <div style="height:500px; overflow-y:auto; border:1px solid #ccc;">
21
- <img src="data:image/png;base64,{encoded}" style="width:100%;" />
22
- </div>
23
- """
 
1
+ import numpy as np
2
+ import cv2
3
+ from PIL import Image
4
+
5
+ def find_low_complexity_row(gray, target_row, search_pct=0.2):
6
+ """
7
+ Find a nearby row (within ±20%) that has low edge/text density.
8
+ """
9
+ h, w = gray.shape
10
+ search_radius = int(h * search_pct)
11
+
12
+ start = max(0, target_row - search_radius)
13
+ end = min(h - 1, target_row + search_radius)
14
+
15
+ # Compute edge map
16
+ edges = cv2.Canny(gray, 80, 160)
17
+
18
+ # Sum edges along each row → 1D profile
19
+ row_scores = edges[start:end].sum(axis=1)
20
+
21
+ # Find the row index with the *minimum* score
22
+ best_local_idx = np.argmin(row_scores)
23
+
24
+ # Convert local idx → global row idx
25
+ best_row = start + best_local_idx
26
+ return best_row
27
+
28
 
29
  def split_image(image: Image.Image, num_chunks: int) -> list:
30
+ """
31
+ Intelligent split: moves split boundaries up/down by ±20% to avoid text.
32
+ """
33
  width, height = image.size
34
+ img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
35
+ gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
36
+
37
  chunks = []
38
+ split_points = []
39
+
40
+ # First compute approximate "uniform" split points
41
+ approx_points = [int(i * height / num_chunks) for i in range(1, num_chunks)]
42
+
43
+ # Adjust each boundary to a low-density horizontal strip
44
+ for pt in approx_points:
45
+ best = find_low_complexity_row(gray, target_row=pt)
46
+ split_points.append(best)
47
+
48
+ # Add start and end
49
+ split_points = [0] + split_points + [height]
50
+
51
+ # Produce chunks
52
  for i in range(num_chunks):
53
+ top = split_points[i]
54
+ bottom = split_points[i+1]
55
+ chunk = image.crop((0, top, width, bottom))
56
+ chunks.append(chunk)
57
 
58
+ return chunks