Spaces:
Sleeping
Sleeping
Update txt2spec.py
Browse files- txt2spec.py +29 -29
txt2spec.py
CHANGED
@@ -7,35 +7,35 @@ import gradio as gr
|
|
7 |
import soundfile as sf
|
8 |
import os
|
9 |
|
10 |
-
#
|
11 |
def text_to_spectrogram_image(text, base_width=512, height=256, max_font_size=80, margin=10, letter_spacing=5):
|
12 |
-
#
|
13 |
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
|
14 |
if os.path.exists(font_path):
|
15 |
font = ImageFont.truetype(font_path, max_font_size)
|
16 |
else:
|
17 |
font = ImageFont.load_default()
|
18 |
|
19 |
-
#
|
20 |
image = Image.new('L', (base_width, height), 'black')
|
21 |
draw = ImageDraw.Draw(image)
|
22 |
text_width = 0
|
23 |
for char in text:
|
24 |
text_bbox = draw.textbbox((0, 0), char, font=font)
|
25 |
text_width += text_bbox[2] - text_bbox[0] + letter_spacing
|
26 |
-
text_width -= letter_spacing #
|
27 |
|
28 |
-
#
|
29 |
if text_width + margin * 2 > base_width:
|
30 |
width = text_width + margin * 2
|
31 |
else:
|
32 |
width = base_width
|
33 |
|
34 |
-
#
|
35 |
image = Image.new('L', (width, height), 'black')
|
36 |
draw = ImageDraw.Draw(image)
|
37 |
|
38 |
-
#
|
39 |
text_x = (width - text_width) // 2
|
40 |
text_y = (height - (text_bbox[3] - text_bbox[1])) // 2
|
41 |
for char in text:
|
@@ -43,32 +43,32 @@ def text_to_spectrogram_image(text, base_width=512, height=256, max_font_size=80
|
|
43 |
char_bbox = draw.textbbox((0, 0), char, font=font)
|
44 |
text_x += char_bbox[2] - char_bbox[0] + letter_spacing
|
45 |
|
46 |
-
#
|
47 |
image = np.array(image)
|
48 |
-
image = np.where(image > 0, 255, image) #
|
49 |
return image
|
50 |
|
51 |
-
#
|
52 |
def spectrogram_image_to_audio(image, sr=22050):
|
53 |
-
#
|
54 |
flipped_image = np.flipud(image)
|
55 |
|
56 |
# Преобразуем изображение в амплитуды спектрограммы
|
57 |
S = flipped_image.astype(np.float32) / 255.0 * 100.0
|
58 |
|
59 |
-
#
|
60 |
y = librosa.griffinlim(S)
|
61 |
return y
|
62 |
|
63 |
-
#
|
64 |
def create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing):
|
65 |
-
#
|
66 |
spec_image = text_to_spectrogram_image(text, base_width, height, max_font_size, margin, letter_spacing)
|
67 |
|
68 |
-
#
|
69 |
y = spectrogram_image_to_audio(spec_image)
|
70 |
|
71 |
-
#
|
72 |
audio_path = 'output.wav'
|
73 |
sf.write(audio_path, y, 22050)
|
74 |
|
@@ -77,35 +77,35 @@ def create_audio_with_spectrogram(text, base_width, height, max_font_size, margi
|
|
77 |
|
78 |
return audio_path, image_path
|
79 |
|
80 |
-
#
|
81 |
-
with gr.Blocks(title='
|
82 |
|
83 |
with gr.Group():
|
84 |
with gr.Row(variant='panel'):
|
85 |
with gr.Column():
|
86 |
-
gr.HTML("<center><h2><a href='https://t.me/pol1trees'>Telegram
|
87 |
with gr.Column():
|
88 |
-
gr.HTML("<center><h2><a href='https://t.me/+GMTP7hZqY0E4OGRi'>Telegram
|
89 |
with gr.Column():
|
90 |
gr.HTML("<center><h2><a href='https://www.youtube.com/channel/UCHb3fZEVxUisnqLqCrEM8ZA'>YouTube</a></h2></center>")
|
91 |
with gr.Column():
|
92 |
gr.HTML("<center><h2><a href='https://github.com/Bebra777228/Audio-Steganography'>GitHub</a></h2></center>")
|
93 |
|
94 |
with gr.Group():
|
95 |
-
text = gr.Textbox(lines=2, placeholder="
|
96 |
|
97 |
with gr.Row(variant='panel'):
|
98 |
-
base_width = gr.Slider(value=512, label="
|
99 |
-
height = gr.Slider(value=256, label="
|
100 |
-
max_font_size = gr.Slider(minimum=10, maximum=130, step=5, value=80, label="
|
101 |
-
margin = gr.Slider(minimum=0, maximum=50, step=1, value=10, label="
|
102 |
-
letter_spacing = gr.Slider(minimum=0, maximum=50, step=1, value=5, label="
|
103 |
|
104 |
-
generate_button = gr.Button("
|
105 |
|
106 |
with gr.Column(variant='panel'):
|
107 |
-
output_audio = gr.Audio(type="filepath", label="
|
108 |
-
output_image = gr.Image(type="filepath", label="
|
109 |
|
110 |
def gradio_interface_fn(text, base_width, height, max_font_size, margin, letter_spacing):
|
111 |
print("\n", text)
|
|
|
7 |
import soundfile as sf
|
8 |
import os
|
9 |
|
10 |
+
# Function for creating a spectrogram image with text
|
11 |
def text_to_spectrogram_image(text, base_width=512, height=256, max_font_size=80, margin=10, letter_spacing=5):
|
12 |
+
# Font and text size
|
13 |
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
|
14 |
if os.path.exists(font_path):
|
15 |
font = ImageFont.truetype(font_path, max_font_size)
|
16 |
else:
|
17 |
font = ImageFont.load_default()
|
18 |
|
19 |
+
# Determine the width of the text, taking into account the distance between letters
|
20 |
image = Image.new('L', (base_width, height), 'black')
|
21 |
draw = ImageDraw.Draw(image)
|
22 |
text_width = 0
|
23 |
for char in text:
|
24 |
text_bbox = draw.textbbox((0, 0), char, font=font)
|
25 |
text_width += text_bbox[2] - text_bbox[0] + letter_spacing
|
26 |
+
text_width -= letter_spacing # Remove the extra spacing after the last letter
|
27 |
|
28 |
+
# Increase the width of the image if the text does not fit
|
29 |
if text_width + margin * 2 > base_width:
|
30 |
width = text_width + margin * 2
|
31 |
else:
|
32 |
width = base_width
|
33 |
|
34 |
+
# Create an image with a new width
|
35 |
image = Image.new('L', (width, height), 'black')
|
36 |
draw = ImageDraw.Draw(image)
|
37 |
|
38 |
+
# Writing text in the center of the image
|
39 |
text_x = (width - text_width) // 2
|
40 |
text_y = (height - (text_bbox[3] - text_bbox[1])) // 2
|
41 |
for char in text:
|
|
|
43 |
char_bbox = draw.textbbox((0, 0), char, font=font)
|
44 |
text_x += char_bbox[2] - char_bbox[0] + letter_spacing
|
45 |
|
46 |
+
# Increase text contrast
|
47 |
image = np.array(image)
|
48 |
+
image = np.where(image > 0, 255, image) # Setting the text to "maximum white"
|
49 |
return image
|
50 |
|
51 |
+
# Converting an image to audio
|
52 |
def spectrogram_image_to_audio(image, sr=22050):
|
53 |
+
# Rotate the image vertically
|
54 |
flipped_image = np.flipud(image)
|
55 |
|
56 |
# Преобразуем изображение в амплитуды спектрограммы
|
57 |
S = flipped_image.astype(np.float32) / 255.0 * 100.0
|
58 |
|
59 |
+
# Converting the spectrogram to an audio signal
|
60 |
y = librosa.griffinlim(S)
|
61 |
return y
|
62 |
|
63 |
+
# Function for creating an audio file and spectrogram from text
|
64 |
def create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing):
|
65 |
+
# Create spectrogram image with normal text
|
66 |
spec_image = text_to_spectrogram_image(text, base_width, height, max_font_size, margin, letter_spacing)
|
67 |
|
68 |
+
# Generate audio signal with inverted text
|
69 |
y = spectrogram_image_to_audio(spec_image)
|
70 |
|
71 |
+
# Save audio signal and spectrogram image
|
72 |
audio_path = 'output.wav'
|
73 |
sf.write(audio_path, y, 22050)
|
74 |
|
|
|
77 |
|
78 |
return audio_path, image_path
|
79 |
|
80 |
+
# Gradio interface
|
81 |
+
with gr.Blocks(title='Audio Steganography', theme=gr.themes.Soft(primary_hue="green", secondary_hue="green", spacing_size="sm", radius_size="lg")) as iface:
|
82 |
|
83 |
with gr.Group():
|
84 |
with gr.Row(variant='panel'):
|
85 |
with gr.Column():
|
86 |
+
gr.HTML("<center><h2><a href='https://t.me/pol1trees'>Telegram Channel</a></h2></center>")
|
87 |
with gr.Column():
|
88 |
+
gr.HTML("<center><h2><a href='https://t.me/+GMTP7hZqY0E4OGRi'>Telegram Chat</a></h2></center>")
|
89 |
with gr.Column():
|
90 |
gr.HTML("<center><h2><a href='https://www.youtube.com/channel/UCHb3fZEVxUisnqLqCrEM8ZA'>YouTube</a></h2></center>")
|
91 |
with gr.Column():
|
92 |
gr.HTML("<center><h2><a href='https://github.com/Bebra777228/Audio-Steganography'>GitHub</a></h2></center>")
|
93 |
|
94 |
with gr.Group():
|
95 |
+
text = gr.Textbox(lines=2, placeholder="Enter your text:", label="Текст")
|
96 |
|
97 |
with gr.Row(variant='panel'):
|
98 |
+
base_width = gr.Slider(value=512, label="Image Width", visible=False)
|
99 |
+
height = gr.Slider(value=256, label="Image Height", visible=False)
|
100 |
+
max_font_size = gr.Slider(minimum=10, maximum=130, step=5, value=80, label="Font Size")
|
101 |
+
margin = gr.Slider(minimum=0, maximum=50, step=1, value=10, label="Indent")
|
102 |
+
letter_spacing = gr.Slider(minimum=0, maximum=50, step=1, value=5, label="Letter spacing")
|
103 |
|
104 |
+
generate_button = gr.Button("Generate")
|
105 |
|
106 |
with gr.Column(variant='panel'):
|
107 |
+
output_audio = gr.Audio(type="filepath", label="Generated audio")
|
108 |
+
output_image = gr.Image(type="filepath", label="Spectrogram")
|
109 |
|
110 |
def gradio_interface_fn(text, base_width, height, max_font_size, margin, letter_spacing):
|
111 |
print("\n", text)
|