HedronCreeper commited on
Commit
aefd9d9
·
verified ·
1 Parent(s): c300287

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -14
app.py CHANGED
@@ -11,29 +11,43 @@ MASK = (1 << BITS_PER_CHANNEL) - 1
11
  MAX_BYTES = 5 * 1024 * 1024
12
  MIN_SIZE = 200
13
 
14
- def calc_size(payload_bytes):
15
- total_bits = (len(payload_bytes) + 4) * 8
16
- pixels_needed = math.ceil(total_bits / 6)
17
- side = max(MIN_SIZE, math.ceil(math.sqrt(pixels_needed * 1.6)))
18
- return side, side
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  def make_base_image(w, h):
21
  img = Image.new("RGB", (w, h), BG_COLOR)
22
  draw = ImageDraw.Draw(img)
23
- font_size = max(10, int(min(w, h) * 0.40))
24
- try:
25
- font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", font_size)
26
- except:
27
- font = ImageFont.load_default()
28
- text = "CC"
29
- bbox = draw.textbbox((0, 0), text, font=font)
30
  tw = bbox[2] - bbox[0]
31
  th = bbox[3] - bbox[1]
32
  x = (w - tw) // 2 - bbox[0]
33
  y = (h - th) // 2 - bbox[1]
34
- draw.text((x, y), text, fill=TEXT_COLOR, font=font)
35
  return img
36
 
 
 
 
 
 
 
37
  def get_carriers(w, h):
38
  base = make_base_image(w, h)
39
  pixels = base.load()
@@ -70,7 +84,7 @@ def embed(payload):
70
  data = header + payload
71
  bits = bits_from_bytes(data)
72
  if len(bits) > len(carriers) * BITS_PER_CHANNEL * 3:
73
- raise ValueError("Data too large even after sizing. Try compressing your file.")
74
  pixels = img.load()
75
  bit_idx = 0
76
  for (x, y) in carriers:
 
11
  MAX_BYTES = 5 * 1024 * 1024
12
  MIN_SIZE = 200
13
 
14
+ def get_font_for_size(target_px):
15
+ font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
16
+ size = target_px
17
+ for _ in range(30):
18
+ try:
19
+ font = ImageFont.truetype(font_path, size)
20
+ except:
21
+ font = ImageFont.load_default()
22
+ return font
23
+ dummy = Image.new("RGB", (1, 1))
24
+ draw = ImageDraw.Draw(dummy)
25
+ bbox = draw.textbbox((0, 0), "CC", font=font)
26
+ h = bbox[3] - bbox[1]
27
+ if abs(h - target_px) <= 2:
28
+ break
29
+ size = max(1, size + (target_px - h))
30
+ return font
31
 
32
  def make_base_image(w, h):
33
  img = Image.new("RGB", (w, h), BG_COLOR)
34
  draw = ImageDraw.Draw(img)
35
+ target_h = int(min(w, h) * 0.40)
36
+ font = get_font_for_size(target_h)
37
+ bbox = draw.textbbox((0, 0), "CC", font=font)
 
 
 
 
38
  tw = bbox[2] - bbox[0]
39
  th = bbox[3] - bbox[1]
40
  x = (w - tw) // 2 - bbox[0]
41
  y = (h - th) // 2 - bbox[1]
42
+ draw.text((x, y), "CC", fill=TEXT_COLOR, font=font)
43
  return img
44
 
45
+ def calc_size(payload_bytes):
46
+ total_bits = (len(payload_bytes) + 4) * 8
47
+ pixels_needed = math.ceil(total_bits / 6)
48
+ side = max(MIN_SIZE, math.ceil(math.sqrt(pixels_needed * 2.2)))
49
+ return side, side
50
+
51
  def get_carriers(w, h):
52
  base = make_base_image(w, h)
53
  pixels = base.load()
 
84
  data = header + payload
85
  bits = bits_from_bytes(data)
86
  if len(bits) > len(carriers) * BITS_PER_CHANNEL * 3:
87
+ raise ValueError("Data too large even after sizing.")
88
  pixels = img.load()
89
  bit_idx = 0
90
  for (x, y) in carriers: