Politrees commited on
Commit
8fb2a96
·
verified ·
1 Parent(s): 5e39517

Update steganography.py

Browse files
Files changed (1) hide show
  1. steganography.py +55 -45
steganography.py CHANGED
@@ -13,21 +13,27 @@ DEFAULT_SAMPLE_RATE = 22050
13
 
14
  logging.basicConfig(level=logging.INFO)
15
 
16
- def text_to_spectrogram_image(text, base_width=512, height=256, max_font_size=80, margin=10, letter_spacing=5):
17
  try:
18
- font = ImageFont.truetype(DEFAULT_FONT_PATH, max_font_size)
19
  except IOError:
20
- logging.warning(f"Font not found at {DEFAULT_FONT_PATH}. Using default font.")
21
- font = ImageFont.load_default()
22
  except Exception as e:
23
  logging.error(f"An error occurred while loading the font: {e}")
24
  raise
25
 
 
26
  draw = ImageDraw.Draw(Image.new("L", (1, 1)))
27
-
28
- text_widths = [draw.textsize(char, font=font)[0] for char in text]
 
 
29
  text_width = sum(text_widths) + letter_spacing * (len(text) - 1)
30
- text_height = draw.textsize(text[0], font=font)[1]
 
 
 
31
 
32
  width = max(base_width, text_width + margin * 2)
33
  height = max(height, text_height + margin * 2)
@@ -43,9 +49,7 @@ def text_to_spectrogram_image(text, base_width=512, height=256, max_font_size=80
43
  draw.text((current_x, text_start_y), char, font=font, fill="white")
44
  current_x += char_width + letter_spacing
45
 
46
- image = np.array(image)
47
- image = np.where(image > 0, 255, image)
48
- return image
49
 
50
  def spectrogram_image_to_audio(image, sr=DEFAULT_SAMPLE_RATE):
51
  flipped_image = np.flipud(image)
@@ -54,7 +58,8 @@ def spectrogram_image_to_audio(image, sr=DEFAULT_SAMPLE_RATE):
54
  return y
55
 
56
  def create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing):
57
- spec_image = text_to_spectrogram_image(text, base_width, height, max_font_size, margin, letter_spacing)
 
58
  y = spectrogram_image_to_audio(spec_image)
59
 
60
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
@@ -111,45 +116,50 @@ def gradio_image_to_audio_fn(upload_image):
111
  def gradio_decode_fn(upload_audio):
112
  return display_audio_spectrogram(upload_audio)
113
 
114
- with gr.Blocks(title="Audio Steganography", css="footer{display:none !important}", theme=gr.themes.Soft(primary_hue="green", secondary_hue="green", spacing_size="sm", radius_size="lg")) as txt2spec:
115
- with gr.Tab("Text to Spectrogram"):
116
- with gr.Group():
117
- text = gr.Textbox(lines=2, placeholder="Enter your text:", label="Text", info="Enter the text you want to convert to audio.")
118
- with gr.Row(variant="panel"):
119
- base_width = gr.Slider(value=512, label="Image Width", visible=False)
120
- height = gr.Slider(value=256, label="Image Height", visible=False)
121
- max_font_size = gr.Slider(minimum=10, maximum=130, step=5, value=80, label="Font size")
122
- margin = gr.Slider(minimum=0, maximum=50, step=1, value=10, label="Indent")
123
- letter_spacing = gr.Slider(minimum=0, maximum=50, step=1, value=5, label="Letter spacing")
124
- generate_button = gr.Button("Generate", variant="primary", size="lg")
125
-
126
- with gr.Column(variant="panel"):
127
  with gr.Group():
128
- output_audio = gr.Audio(type="filepath", label="Generated audio")
129
- output_spectrogram = gr.Image(type="filepath", label="Spectrogram")
130
-
131
- generate_button.click(gradio_interface_fn, inputs=[text, base_width, height, max_font_size, margin, letter_spacing], outputs=[output_audio, output_spectrogram])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
- with gr.Tab("Image to Spectrogram"):
134
- with gr.Group():
135
- with gr.Column():
136
- upload_image = gr.Image(type="filepath", label="Upload image")
137
- convert_button = gr.Button("Convert to audio", variant="primary", size="lg")
138
 
139
- with gr.Column(variant="panel"):
140
- output_audio_from_image = gr.Audio(type="filepath", label="Generated audio")
141
 
142
- convert_button.click(gradio_image_to_audio_fn, inputs=[upload_image], outputs=[output_audio_from_image])
 
 
 
 
143
 
144
- with gr.Tab("Audio Spectrogram"):
145
- with gr.Group():
146
- with gr.Column():
147
- upload_audio = gr.Audio(type="filepath", label="Upload audio", scale=3)
148
- decode_button = gr.Button("Show spectrogram", variant="primary", size="lg")
149
 
150
- with gr.Column(variant="panel"):
151
- decoded_image = gr.Image(type="filepath", label="Audio Spectrogram")
152
 
153
- decode_button.click(gradio_decode_fn, inputs=[upload_audio], outputs=[decoded_image])
154
 
155
- txt2spec.launch(share=True)
 
 
 
13
 
14
  logging.basicConfig(level=logging.INFO)
15
 
16
+ def load_font(font_path, max_font_size):
17
  try:
18
+ return ImageFont.truetype(font_path, max_font_size)
19
  except IOError:
20
+ logging.warning(f"Font not found at {font_path}. Using default font.")
21
+ return ImageFont.load_default()
22
  except Exception as e:
23
  logging.error(f"An error occurred while loading the font: {e}")
24
  raise
25
 
26
+ def create_text_image(text, font, base_width=512, height=256, margin=10, letter_spacing=5):
27
  draw = ImageDraw.Draw(Image.new("L", (1, 1)))
28
+ text_widths = [
29
+ draw.textbbox((0, 0), char, font=font)[2] - draw.textbbox((0, 0), char, font=font)[0]
30
+ for char in text
31
+ ]
32
  text_width = sum(text_widths) + letter_spacing * (len(text) - 1)
33
+ text_height = (
34
+ draw.textbbox((0, 0), text[0], font=font)[3]
35
+ - draw.textbbox((0, 0), text[0], font=font)[1]
36
+ )
37
 
38
  width = max(base_width, text_width + margin * 2)
39
  height = max(height, text_height + margin * 2)
 
49
  draw.text((current_x, text_start_y), char, font=font, fill="white")
50
  current_x += char_width + letter_spacing
51
 
52
+ return np.array(image)
 
 
53
 
54
  def spectrogram_image_to_audio(image, sr=DEFAULT_SAMPLE_RATE):
55
  flipped_image = np.flipud(image)
 
58
  return y
59
 
60
  def create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing):
61
+ font = load_font(DEFAULT_FONT_PATH, max_font_size)
62
+ spec_image = create_text_image(text, font, base_width, height, margin, letter_spacing)
63
  y = spectrogram_image_to_audio(spec_image)
64
 
65
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
 
116
  def gradio_decode_fn(upload_audio):
117
  return display_audio_spectrogram(upload_audio)
118
 
119
+ def create_gradio_interface():
120
+ with gr.Blocks(title="Audio Steganography", css="footer{display:none !important}", theme=gr.themes.Soft(primary_hue="green", secondary_hue="green", spacing_size="sm", radius_size="lg")) as txt2spec:
121
+ with gr.Tab("Text to Spectrogram"):
 
 
 
 
 
 
 
 
 
 
122
  with gr.Group():
123
+ text = gr.Textbox(lines=2, placeholder="Enter your text:", label="Text", info="Enter the text you want to convert to audio.")
124
+ with gr.Row(variant="panel"):
125
+ base_width = gr.Slider(value=512, label="Image Width", visible=False)
126
+ height = gr.Slider(value=256, label="Image Height", visible=False)
127
+ max_font_size = gr.Slider(minimum=10, maximum=130, step=5, value=80, label="Font size")
128
+ margin = gr.Slider(minimum=0, maximum=50, step=1, value=10, label="Indent")
129
+ letter_spacing = gr.Slider(minimum=0, maximum=50, step=1, value=5, label="Letter spacing")
130
+ generate_button = gr.Button("Generate", variant="primary", size="lg")
131
+
132
+ with gr.Column(variant="panel"):
133
+ with gr.Group():
134
+ output_audio = gr.Audio(type="filepath", label="Generated audio")
135
+ output_spectrogram = gr.Image(type="filepath", label="Spectrogram")
136
+
137
+ generate_button.click(gradio_interface_fn, inputs=[text, base_width, height, max_font_size, margin, letter_spacing], outputs=[output_audio, output_spectrogram])
138
+
139
+ with gr.Tab("Image to Spectrogram"):
140
+ with gr.Group():
141
+ with gr.Column():
142
+ upload_image = gr.Image(type="filepath", label="Upload image")
143
+ convert_button = gr.Button("Convert to audio", variant="primary", size="lg")
144
 
145
+ with gr.Column(variant="panel"):
146
+ output_audio_from_image = gr.Audio(type="filepath", label="Generated audio")
 
 
 
147
 
148
+ convert_button.click(gradio_image_to_audio_fn, inputs=[upload_image], outputs=[output_audio_from_image])
 
149
 
150
+ with gr.Tab("Audio Spectrogram"):
151
+ with gr.Group():
152
+ with gr.Column():
153
+ upload_audio = gr.Audio(type="filepath", label="Upload audio", scale=3)
154
+ decode_button = gr.Button("Show spectrogram", variant="primary", size="lg")
155
 
156
+ with gr.Column(variant="panel"):
157
+ decoded_image = gr.Image(type="filepath", label="Audio Spectrogram")
 
 
 
158
 
159
+ decode_button.click(gradio_decode_fn, inputs=[upload_audio], outputs=[decoded_image])
 
160
 
161
+ return txt2spec
162
 
163
+ if __name__ == "__main__":
164
+ txt2spec = create_gradio_interface()
165
+ txt2spec.launch(share=True)