sayile firahs
sayile firahs

Reputation: 125

How to change the background

I am new to streamlit and wanted to know that Is there any way to change the background of the streamlit app ?

i have tried uisng this in my code its not showing any error but it also not working

Let me know how to change the background any idea would be appreciated

page_bg_img = '''
    <style>
    body {
    background-image: url("https://images.unsplash.com/photo-1542281286-9e0a16bb7366");
    background-size: cover;
    }
    </style>
    '''
    
    st.markdown(page_bg_img, unsafe_allow_html=True)

Orignial code:

import cv2
import streamlit as st
import numpy as np 
from PIL import Image


def cartoonization (img):

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)      
    gray = cv2.medianBlur(gray, 9) 
    edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9) 
    color = cv2.detailEnhance(img, sigma_s=10, sigma_r=0.15)
    cartoon = cv2.bitwise_and(color, color, mask=edges)
    return cartoon

###############################################################################
    
st.write("""
          # Cartoonize Your Image!

          """
          )

st.write("This is an app to turn your photos into cartoon")

file_image = st.camera_input(label = "Take a pic of you to be sketched out")

if file_image:
    input_img = Image.open(file_image)
    final_sketch = cartoonization(np.array(input_img))
    st.write("**Output Pencil Sketch**")
    one = st.columns(1,gap="small")
    st.image(final_sketch, use_column_width=True)
else:
     st.write("You haven't uploaded any image file")

Upvotes: 1

Views: 993

Answers (1)

Fariya
Fariya

Reputation: 284

Now you can use your local image to the background

import cv2
import streamlit as st
import numpy as np 
from PIL import Image
import base64

def image_local(image_file):
    with open(image_file, "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read())
    st.markdown(
    f"""
    <style>
    .stApp {{
        background-image: url(data:image/{"png"};base64,{encoded_string.decode()});
        background-size: cover
    }}
    </style>
    """,
    unsafe_allow_html=True
    )
image_local('photo.png')    


def cartoonization (img):

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)      
    gray = cv2.medianBlur(gray, 9) 
    edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9) 
    color = cv2.detailEnhance(img, sigma_s=10, sigma_r=0.15)
    cartoon = cv2.bitwise_and(color, color, mask=edges)
    return cartoon

###############################################################################
    
st.write("""
          # Cartoonize Your Image!

          """
          )

st.write("This is an app to turn your photos into cartoon")

file_image = st.camera_input(label = "Take a pic of you to be sketched out")

if file_image:
    input_img = Image.open(file_image)
    final_sketch = cartoonization(np.array(input_img))
    st.write("**Output Pencil Sketch**")
    one = st.columns(1,gap="small")
    st.image(final_sketch, use_column_width=True)
else:
     st.write("You haven't uploaded any image file")

Upvotes: 1

Related Questions