|
""" |
|
@author: idoia lerchundi |
|
""" |
|
import urllib.request |
|
from PIL import Image,ImageFile |
|
import streamlit as st |
|
import numpy as np |
|
import requests |
|
from io import BytesIO |
|
|
|
def local_css(file_name): |
|
with open(file_name) as f: |
|
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True) |
|
|
|
st.set_page_config( |
|
page_title="Streamlit iCodeIdoia", |
|
page_icon="images/ilpicon1.png",layout="wide",initial_sidebar_state="expanded" |
|
) |
|
|
|
st.image("images/banner.jpg") |
|
|
|
|
|
local_css("styles/style.css") |
|
|
|
def fill_square_cropper(img): |
|
imgsz = [img.height, img.width] |
|
|
|
original_size = imgsz |
|
|
|
smallestsize = min(imgsz) |
|
biggestsize = max(imgsz) |
|
|
|
|
|
avg_color_per_row = np.average(img, axis=0) |
|
avg_color = np.average(avg_color_per_row, axis=0) |
|
|
|
if img.height > img.width: |
|
|
|
area = (0, 0, img.width + (img.height - img.width),img.height) |
|
cropped_img = img.crop(area) |
|
newimgsz = [cropped_img.height, cropped_img.width] |
|
|
|
newimg = Image.new('RGB', ([newimgsz[1],newimgsz[0]]), (round(avg_color[0]), round(avg_color[1]), round(avg_color[2]))) |
|
|
|
newpos = (img.height-img.width) |
|
newpos = newpos/2 |
|
newimg.paste(img,(int(newpos),0)) |
|
|
|
return newimg |
|
|
|
|
|
if img.width > img.height: |
|
area = (0, 0, img.width, img.height+ (img.width - img.height)) |
|
cropped_img = img.crop(area) |
|
newimgsz = [cropped_img.height, cropped_img.width] |
|
|
|
newimg = Image.new('RGB', ([newimgsz[1],newimgsz[0]]), (round(avg_color[0]), round(avg_color[1]), round(avg_color[2]))) |
|
|
|
newpos = (img.width-img.height) |
|
newpos = newpos/2 |
|
newimg.paste(img,(0,(int(newpos)))) |
|
|
|
return newimg |
|
|
|
|
|
tab1, tab2 = st.tabs(["Demo","Application"]) |
|
|
|
with tab1: |
|
|
|
url = "https://raw.githubusercontent.com/webdevserv/images_video/main/cowportrait.jpg" |
|
|
|
url2 = "https://raw.githubusercontent.com/webdevserv/images_video/main/cowlandscape.jpg" |
|
|
|
st.subheader("Square an image demo") |
|
img_description = st.text('Image will be squared with color filler where applicable.') |
|
|
|
if st.button('Square and Fill Demo'): |
|
response = requests.get(url) |
|
img = Image.open(BytesIO(response.content)) |
|
img.load() |
|
|
|
generated_img = fill_square_cropper(img) |
|
st.image(generated_img) |
|
|
|
response = requests.get(url2) |
|
img = Image.open(BytesIO(response.content)) |
|
img.load() |
|
|
|
generated_img = fill_square_cropper(img) |
|
st.image(generated_img) |
|
|
|
with tab2: |
|
st.subheader("Square an image app") |
|
img_description = st.text('Image will be squared with color filler where applicable. Suitable for instagram posts.') |
|
uploaded_file = st.file_uploader("Upload a JPG image to square and fill with color.", type=['jpg']) |
|
|
|
if uploaded_file is not None: |
|
img = Image.open(uploaded_file) |
|
img.load() |
|
generated_img = fill_square_cropper(img) |
|
st.image(generated_img) |
|
st.balloons() |
|
st.markdown("""<div style="margin: 0.75em 0;"><a href="https://www.buymeacoffee.com/Artgen" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a></div> |
|
<div style="margin: 0.75em 0;">But what would really help me is a <strong>PRO subscription</strong> to Google Colab, Kaggle or Hugging Face. Many thanks.</div>""", unsafe_allow_html=True) |