티스토리 뷰

To add a watermark to an image using Python, you can use libraries like Pillow (PIL). However, integrating this functionality with Streamlit for web deployment requires a different approach. Here's a basic outline of how you can achieve this:

  1. Pillow for Watermarking: Use the Pillow library to add a watermark to the image.
  2. Streamlit for Web Interface: Use Streamlit to create a web interface where users can upload images and download the watermarked version.

Here's a Python script that demonstrates how to add a watermark:

from PIL import Image, ImageDraw, ImageFont
import streamlit as st

def add_watermark(input_image):
    watermark_text = "Watermark_Text_Here!"

    # Load the image
    image = Image.open(input_image).convert("RGBA")

    # Make the image editable
    txt = Image.new('RGBA', image.size, (255,255,255,0))

    # Choose a font and size for the watermark
    fnt = ImageFont.load_default()
    d = ImageDraw.Draw(txt)

    # Position the text at the bottom right
    textwidth, textheight = d.textsize(watermark_text, font=fnt)
    width, height = image.size
    x = width - textwidth - 10
    y = height - textheight - 10

    # Add text to the image
    d.text((x,y), watermark_text, font=fnt, fill=(255,255,255,128))

    watermarked = Image.alpha_composite(image, txt)

    # Save the watermarked image
    watermarked.save("watermarked_image.png")

# Streamlit interface
st.title("Image Watermarking App")

uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
    # Add watermark
    add_watermark(uploaded_file)
    # Show the watermarked image
    st.image("watermarked_image.png", caption="Watermarked Image")
    # Provide a button to download the watermarked image
    with open("watermarked_image.png", "rb") as file:
        btn = st.download_button(
            label="Download Watermarked Image",
            data=file,
            file_name="watermarked_image.png",
            mime="image/png"
        )

This script creates a Streamlit app where users can upload an image, and the app adds a "Watermark_Text_Here!" watermark to the bottom right corner of the image. Users can then download the watermarked image. Note that you need to have Pillow installed (pip install Pillow) and Streamlit (pip install streamlit) to run this script.

Deploy this script using Streamlit sharing or a similar platform to make it available on the web.

 
반응형

'IT & IOT 이야기 > 어쩌다개발' 카테고리의 다른 글

Convert MOV to MP4  (0) 2024.02.06
[Streamlit] link html file  (0) 2024.01.25
avahi-daemon 이란?  (0) 2024.01.25
[웹개발]Windows server에 NginX 설치하기 (2020.12.08)  (0) 2020.12.08
댓글
최근에 올라온 글
최근에 달린 댓글
글 보관함
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Total
Today
Yesterday