Politrees commited on
Commit
bc96c8f
·
verified ·
1 Parent(s): a148ff8

Update steganography.py

Browse files
Files changed (1) hide show
  1. steganography.py +15 -49
steganography.py CHANGED
@@ -48,22 +48,6 @@ def text_to_spectrogram_image(text, base_width=512, height=256, max_font_size=80
48
  image = np.where(image > 0, 255, image)
49
  return image
50
 
51
- # Function for creating a spectrogram image with an image
52
- def image_to_spectrogram_image(image_path, base_width=512, height=256, margin=10):
53
- image = Image.open(image_path).convert('L')
54
- image_width, image_height = image.size
55
-
56
- # Adjust width and height based on image size
57
- width = max(base_width, image_width + margin * 2)
58
- height = max(height, image_height + margin * 2)
59
-
60
- new_image = Image.new('L', (width, height), 'black')
61
- new_image.paste(image, ((width - image_width) // 2, (height - image_height) // 2))
62
-
63
- new_image = np.array(new_image)
64
- new_image = np.where(new_image > 0, 255, new_image)
65
- return new_image
66
-
67
  # Converting an image to audio
68
  def spectrogram_image_to_audio(image, sr=DEFAULT_SAMPLE_RATE):
69
  flipped_image = np.flipud(image)
@@ -95,30 +79,6 @@ def create_audio_with_spectrogram(text, base_width, height, max_font_size, margi
95
 
96
  return audio_path, spectrogram_path
97
 
98
- # Function for creating an audio file and spectrogram from an image
99
- def create_audio_with_spectrogram_from_image(image_path, base_width, height, margin):
100
- spec_image = image_to_spectrogram_image(image_path, base_width, height, margin)
101
- y = spectrogram_image_to_audio(spec_image)
102
-
103
- with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_audio:
104
- audio_path = temp_audio.name
105
- sf.write(audio_path, y, DEFAULT_SAMPLE_RATE)
106
-
107
- # Create spectrogram from audio
108
- S = librosa.feature.melspectrogram(y=y, sr=DEFAULT_SAMPLE_RATE)
109
- S_dB = librosa.power_to_db(S, ref=np.max)
110
- plt.figure(figsize=(10, 4))
111
- librosa.display.specshow(S_dB, sr=DEFAULT_SAMPLE_RATE, x_axis='time', y_axis='mel')
112
- plt.axis('off')
113
- plt.tight_layout(pad=0)
114
-
115
- with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_spectrogram:
116
- spectrogram_path = temp_spectrogram.name
117
- plt.savefig(spectrogram_path, bbox_inches='tight', pad_inches=0, transparent=True)
118
- plt.close()
119
-
120
- return audio_path, spectrogram_path
121
-
122
  # Function for displaying the spectrogram of an audio file
123
  def display_audio_spectrogram(audio_path):
124
  y, sr = librosa.load(audio_path, sr=None)
@@ -136,16 +96,26 @@ def display_audio_spectrogram(audio_path):
136
  plt.close()
137
  return spectrogram_path
138
 
 
 
 
 
 
 
 
 
 
 
 
139
  # Gradio interface
140
  def gradio_interface_fn(text, base_width, height, max_font_size, margin, letter_spacing):
141
  logging.info(f"Generating audio and spectrogram for text: {text}")
142
  audio_path, spectrogram_path = create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing)
143
  return audio_path, spectrogram_path
144
 
145
- def gradio_image_to_audio_fn(upload_image, base_width, height, margin):
146
  logging.info(f"Converting image to audio: {upload_image}")
147
- audio_path, spectrogram_path = create_audio_with_spectrogram_from_image(upload_image, base_width, height, margin)
148
- return audio_path, spectrogram_path
149
 
150
  def gradio_decode_fn(upload_audio):
151
  logging.info(f"Generating spectrogram for audio: {upload_audio}")
@@ -174,16 +144,12 @@ with gr.Blocks(title='Audio Steganography', theme=gr.themes.Soft(primary_hue="gr
174
  with gr.Group():
175
  with gr.Row(variant='panel'):
176
  upload_image = gr.Image(type="filepath", label="Upload image")
177
- base_width = gr.Slider(value=512, label="Image Width", visible=False)
178
- height = gr.Slider(value=256, label="Image Height", visible=False)
179
- margin = gr.Slider(minimum=0, maximum=50, step=1, value=10, label="Indent")
180
  convert_button = gr.Button("Convert to audio")
181
 
182
  with gr.Column(variant='panel'):
183
  output_audio_from_image = gr.Audio(type="filepath", label="Generated audio")
184
- output_spectrogram_from_image = gr.Image(type="filepath", label="Spectrogram")
185
 
186
- convert_button.click(gradio_image_to_audio_fn, inputs=[upload_image, base_width, height, margin], outputs=[output_audio_from_image, output_spectrogram_from_image])
187
 
188
  with gr.Tab("Audio Spectrogram"):
189
  with gr.Group():
@@ -196,4 +162,4 @@ with gr.Blocks(title='Audio Steganography', theme=gr.themes.Soft(primary_hue="gr
196
 
197
  decode_button.click(gradio_decode_fn, inputs=[upload_audio], outputs=[decoded_image])
198
 
199
- txt2spec.launch(share=True)
 
48
  image = np.where(image > 0, 255, image)
49
  return image
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  # Converting an image to audio
52
  def spectrogram_image_to_audio(image, sr=DEFAULT_SAMPLE_RATE):
53
  flipped_image = np.flipud(image)
 
79
 
80
  return audio_path, spectrogram_path
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  # Function for displaying the spectrogram of an audio file
83
  def display_audio_spectrogram(audio_path):
84
  y, sr = librosa.load(audio_path, sr=None)
 
96
  plt.close()
97
  return spectrogram_path
98
 
99
+ # Converting a downloaded image to an audio spectrogram
100
+ def image_to_spectrogram_audio(image_path, sr=DEFAULT_SAMPLE_RATE):
101
+ image = Image.open(image_path).convert('L')
102
+ image = np.array(image)
103
+ y = spectrogram_image_to_audio(image, sr)
104
+
105
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_audio:
106
+ img2audio_path = temp_audio.name
107
+ sf.write(img2audio_path, y, sr)
108
+ return img2audio_path
109
+
110
  # Gradio interface
111
  def gradio_interface_fn(text, base_width, height, max_font_size, margin, letter_spacing):
112
  logging.info(f"Generating audio and spectrogram for text: {text}")
113
  audio_path, spectrogram_path = create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing)
114
  return audio_path, spectrogram_path
115
 
116
+ def gradio_image_to_audio_fn(upload_image):
117
  logging.info(f"Converting image to audio: {upload_image}")
118
+ return image_to_spectrogram_audio(upload_image)
 
119
 
120
  def gradio_decode_fn(upload_audio):
121
  logging.info(f"Generating spectrogram for audio: {upload_audio}")
 
144
  with gr.Group():
145
  with gr.Row(variant='panel'):
146
  upload_image = gr.Image(type="filepath", label="Upload image")
 
 
 
147
  convert_button = gr.Button("Convert to audio")
148
 
149
  with gr.Column(variant='panel'):
150
  output_audio_from_image = gr.Audio(type="filepath", label="Generated audio")
 
151
 
152
+ convert_button.click(gradio_image_to_audio_fn, inputs=[upload_image], outputs=[output_audio_from_image])
153
 
154
  with gr.Tab("Audio Spectrogram"):
155
  with gr.Group():
 
162
 
163
  decode_button.click(gradio_decode_fn, inputs=[upload_audio], outputs=[decoded_image])
164
 
165
+ txt2spec.launch(share=True)