Spaces:
Runtime error
Runtime error
update v1
Browse filesFiles changed (4)
- app.pyAdded+102
- requirement.txtAdded+4
- steganography.pyAdded+127
- utils.pyAdded+25
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102import gradio as gr from steganography import Steganographyfrom utils import draw_multiple_line_text, generate_qr_code
TITLE = """<h2 align="center"> ✍️ Invisible Watermark </h2>"""
def apply_watermark(radio_button, input_image, watermark_image, watermark_text, watermark_url): input_image = input_image.convert('RGB')
if radio_button == "Image": watermark_image = watermark_image.resize((input_image.width, input_image.height)).convert('L').convert('RGB') return Steganography().merge(input_image, watermark_image, digit=7) elif radio_button == "Text": watermark_image = draw_multiple_line_text(input_image.size, watermark_text) return Steganography().merge(input_image, watermark_image, digit=7) else: size = min(input_image.width, input_image.height) watermark_image = generate_qr_code(watermark_url).resize((size, size)).convert('RGB') return Steganography().merge(input_image, watermark_image, digit=7)
def extract_watermark(input_image_to_extract): return Steganography().unmerge(input_image_to_extract.convert('RGB'), digit=7).convert('RGBA')
with gr.Blocks() as demo: gr.HTML(TITLE) with gr.Tab("Add watermark"): with gr.Row(): with gr.Column(): gr.Markdown("### Image to apply watermark") input_image = gr.Image(type='pil') with gr.Blocks(): gr.Markdown("### Which type of watermark you want to apply?") radio_button = gr.Radio( choices=["QRCode", "Text", "Image"], label="Watermark type", value="QRCode", # info="Which type of watermark you want to apply?" ) watermark_url = gr.Textbox( placeholder="URL to generate QR code", visible=True ) watermark_text = gr.Textbox( placeholder="What text you want to use as watermark?", visible=False ) watermark_image = gr.Image( type='pil', visible=False ) def update_visability(radio_value): return { watermark_image: { "visible":radio_value == "Image", "__type__": "update" }, watermark_text: { "visible":radio_value == "Text", "__type__": "update" }, watermark_url: { "visible":radio_value == "QRCode", "__type__": "update" } }
with gr.Column(): gr.Markdown("### Appied watermark image") output_image = gr.Image(show_label=False) with gr.Row(): apply_button =gr.Button("Apply") with gr.Tab("Extract watermark"): with gr.Row(): with gr.Column(): gr.Markdown("### Image to extract watermark") input_image_to_extract = gr.Image(type='pil') with gr.Column(): gr.Markdown("### Extracted watermark") extracted_watermark = gr.Image(type='pil') extract_button = gr.Button("Extract") radio_button.change( fn=update_visability, inputs=radio_button, outputs=[watermark_image, watermark_text, watermark_url] ) apply_button.click( fn=apply_watermark, inputs=[radio_button, input_image, watermark_image, watermark_text, watermark_url], outputs=[output_image] ) extract_button.click(fn=extract_watermark, inputs=[input_image_to_extract], outputs=[extracted_watermark])
demo.launch()1234Pillowclickgradioqrcode123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127import argparse
from PIL import Image
class Steganography:
BLACK_PIXEL = (0, 0, 0)
def _int_to_bin(self, rgb): """Convert an integer tuple to a binary (string) tuple.
:param rgb: An integer tuple like (220, 110, 96) :return: A string tuple like ("00101010", "11101011", "00010110") """ r, g, b = rgb return f'{r:08b}', f'{g:08b}', f'{b:08b}'
def _bin_to_int(self, rgb): """Convert a binary (string) tuple to an integer tuple.
:param rgb: A string tuple like ("00101010", "11101011", "00010110") :return: Return an int tuple like (220, 110, 96) """ r, g, b = rgb return int(r, 2), int(g, 2), int(b, 2)
def _merge_rgb(self, rgb1, rgb2, digit): """Merge two RGB tuples.
:param rgb1: An integer tuple like (220, 110, 96) :param rgb2: An integer tuple like (240, 95, 105) :return: An integer tuple with the two RGB values merged. """ r1, g1, b1 = self._int_to_bin(rgb1) r2, g2, b2 = self._int_to_bin(rgb2) rgb = r1[:digit] + r2[:8-digit], g1[:digit] + g2[:8-digit], b1[:digit] + b2[:8-digit] return self._bin_to_int(rgb)
def _unmerge_rgb(self, rgb, digit): """Unmerge RGB.
:param rgb: An integer tuple like (220, 110, 96) :return: An integer tuple with the two RGB values merged. """ r, g, b = self._int_to_bin(rgb) # Extract the last 4 bits (corresponding to the hidden image) # Concatenate 4 zero bits because we are working with 8 bit new_rgb = r[digit:] + '0'*digit, g[digit:] + '0'*digit, b[digit:] + '0'*digit return self._bin_to_int(new_rgb)
def merge(self, image1, image2, digit=4): """Merge image2 into image1.
:param image1: First image :param image2: Second image :return: A new merged image. """ # Check the images dimensions if image2.size[0] > image1.size[0] or image2.size[1] > image1.size[1]: raise ValueError('Image 2 should be smaller than Image 1!')
# Get the pixel map of the two images map1 = image1.load() map2 = image2.load()
new_image = Image.new(image1.mode, image1.size) new_map = new_image.load()
for i in range(image1.size[0]): for j in range(image1.size[1]): is_valid = lambda: i < image2.size[0] and j < image2.size[1] rgb1 = map1[i ,j] rgb2 = map2[i, j] if is_valid() else self.BLACK_PIXEL new_map[i, j] = self._merge_rgb(rgb1, rgb2, digit)
return new_image
def unmerge(self, image, digit=4, binarization=True): """Unmerge an image.
:param image: The input image. :return: The unmerged/extracted image. """ pixel_map = image.load()
# Create the new image and load the pixel map new_image = Image.new(image.mode, image.size) new_map = new_image.load()
for i in range(image.size[0]): for j in range(image.size[1]): r, g, b = self._unmerge_rgb(pixel_map[i, j], digit) r = 255 if r >= 128 else 0 g = 255 if g >= 128 else 0 b = 255 if b >= 128 else 0 new_map[i, j] = r, g, b
return new_image
def main(): parser = argparse.ArgumentParser(description='Steganography') subparser = parser.add_subparsers(dest='command')
merge = subparser.add_parser('merge') merge.add_argument('--image1', required=True, help='Image1 path') merge.add_argument('--image2', required=True, help='Image2 path') merge.add_argument('--output', required=True, help='Output path')
unmerge = subparser.add_parser('unmerge') unmerge.add_argument('--image', required=True, help='Image path') unmerge.add_argument('--output', required=True, help='Output path')
args = parser.parse_args()
if args.command == 'merge': image1 = Image.open(args.image1) image2 = Image.open(args.image2) Steganography().merge(image1, image2).save(args.output) elif args.command == 'unmerge': image = Image.open(args.image) Steganography().unmerge(image).save(args.output)
if __name__ == '__main__': main()12345678910111213141516171819202122232425from PIL import Image, ImageDraw, ImageFontimport qrcode
def generate_qr_code(url): return qrcode.make(url)
def draw_multiple_line_text(input_image_size, text, font=None, text_color=(255, 255, 255)): if font is None: font = ImageFont.load_default() watermark_image = Image.new("RGB", input_image_size, (0, 0, 0)) output_image = watermark_image.copy() draw = ImageDraw.Draw(watermark_image) image_width, image_height = input_image_size line_width, line_height = font.getsize(text) draw.text( ((image_width - line_width)/2, (image_height - line_height)/2), text, font=font, fill=text_color )
scale = min(image_width / line_width, image_height / line_height) watermark_image = watermark_image.resize((int(watermark_image.width * scale), int(watermark_image.height*scale))) output_image.paste(watermark_image, (int((image_width-watermark_image.width)/2), int((image_height-watermark_image.height)/2))) return output_imageapp.pyAdded+102
requirement.txtAdded+4
steganography.pyAdded+127
utils.pyAdded+25