import numpy as np import matplotlib.pyplot as plt from PIL import Image, ImageDraw, ImageFont import librosa import librosa.display import gradio as gr import soundfile as sf import os # Function for creating a spectrogram image with text def text_to_spectrogram_image(text, base_width=512, height=256, max_font_size=80, margin=10, letter_spacing=5): # Font and text size font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" if os.path.exists(font_path): font = ImageFont.truetype(font_path, max_font_size) else: font = ImageFont.load_default() # Determine the width of the text, taking into account the distance between letters image = Image.new('L', (base_width, height), 'black') draw = ImageDraw.Draw(image) text_width = 0 for char in text: text_bbox = draw.textbbox((0, 0), char, font=font) text_width += text_bbox[2] - text_bbox[0] + letter_spacing text_width -= letter_spacing # Remove the extra spacing after the last letter # Increase the width of the image if the text does not fit if text_width + margin * 2 > base_width: width = text_width + margin * 2 else: width = base_width # Create an image with a new width image = Image.new('L', (width, height), 'black') draw = ImageDraw.Draw(image) # Writing text in the center of the image text_x = (width - text_width) // 2 text_y = (height - (text_bbox[3] - text_bbox[1])) // 2 for char in text: draw.text((text_x, text_y), char, font=font, fill='white') char_bbox = draw.textbbox((0, 0), char, font=font) text_x += char_bbox[2] - char_bbox[0] + letter_spacing # Increase text contrast image = np.array(image) image = np.where(image > 0, 255, image) # Setting the text to "maximum white" return image # Converting an image to audio def spectrogram_image_to_audio(image, sr=22050): # Rotate the image vertically flipped_image = np.flipud(image) # Преобразуем изображение в амплитуды спектрограммы S = flipped_image.astype(np.float32) / 255.0 * 100.0 # Converting the spectrogram to an audio signal y = librosa.griffinlim(S) return y # Function for creating an audio file and spectrogram from text def create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing): # Create spectrogram image with normal text spec_image = text_to_spectrogram_image(text, base_width, height, max_font_size, margin, letter_spacing) # Generate audio signal with inverted text y = spectrogram_image_to_audio(spec_image) # Save audio signal and spectrogram image audio_path = 'output.wav' sf.write(audio_path, y, 22050) image_path = 'spectrogram.png' plt.imsave(image_path, spec_image, cmap='gray') return audio_path, image_path # Gradio interface with gr.Blocks(title='Audio Steganography', theme=gr.themes.Soft(primary_hue="green", secondary_hue="green", spacing_size="sm", radius_size="lg")) as iface: with gr.Group(): with gr.Row(variant='panel'): with gr.Column(): gr.HTML("