用Python写了一个网页版的美图秀秀,惊呆了

大家新年快乐,这篇文章可以算作是虎年的第一篇干货技术类文章了,今天小编用Python做了一个网页版的“P图软件”,大致的流程在于我们可以将上传的照片进行黑白处理、铅笔素描处理、模糊化处理等一系列操作,具体如下

04f0716c049d82d657a274516dceee70.gif

下面我们来看一下该整个网页是怎么写的

网页左侧的工具栏部分

首先第一步在于导入我们所需要用到的模块,代码如下

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

我们先设计到网页当中的标题和左边的工具栏,左边的工具栏部分代码如下

st.sidebar.markdown('<p class="font">My First Photo Converter App</p>', unsafe_allow_html=True)
with st.sidebar.expander("About the App"):
     st.write("""
        Use this simple app to convert your favorite photo to a pencil sketch, a grayscale image or an image with blurring effect.  \n  \nThis app was created by Junxin as a side project to learn Streamlit and computer vision. Hope you enjoy!
     """)

首先在工具栏当中添加的是关于该应用的介绍,通过调用streamlit模块当中的sidebar方法来生成一个下拉框,在通过点击“+”键来打开,

41c78f605ae44a67dfd6a69a09f5a5cb.gif

然后则是5个单选框,代码如下

filter = st.sidebar.radio('Covert your photo to:', ['Original', 'Gray Image', 'Black and White', 'Pencil Sketch', 'Blur Effect'])

单选框是通过sidebar方法来实现的,当中添加上分别是哪些的单选项,

e5bdfa94ddc08157b4d898fd3f3ae5a3.png

紧接着是最后的“感谢反馈”的部分,代码如下

st.sidebar.title(' ')
st.sidebar.markdown(' ')
st.sidebar.subheader('Please help us improve!')
with st.sidebar.form(key='columns_in_form',clear_on_submit=True): 
    rating=st.slider("Please rate the app", min_value=1, max_value=5, value=3,help='Drag the slider to rate the app. This is a 1-5 rating scale where 5 is the highest rating')
    text=st.text_input(label='Please leave your feedback here')
    submitted = st.form_submit_button('Submit')
    if submitted:
      .......

169cc75681460c0c0bb2d4471ea13169.png

网页正文的标题部分

标题部分的代码如下

with col1:
    st.markdown(""" <style> .font {
    font-size:35px ; font-family: 'Cooper Black'; color: #FF9633;} 
    </style> """, unsafe_allow_html=True)
    st.markdown('<p class="font">Upload your photo here...</p>', unsafe_allow_html=True)

当然除此之外,我们需要的是上传图片的功能,代码如下

uploaded_file = st.file_uploader("", type=['jpg', 'png', 'jpeg'])

这里调用的是streamlit模块当中的file_uploader方法,其中允许上传的有三种文件格式,分别是jpgpng以及jpeg

接下去便是上传图片的处理逻辑部分,代码如下,

with col2:
    filter = st.sidebar.radio('Covert your photo to:', ['Original', 'Gray Image', 'Black and White', 'Pencil Sketch', 'Blur Effect'])
    if filter == 'Gray Image':
        converted_img = np.array(image.convert('RGB'))
        gray_scale = cv2.cvtColor(converted_img, cv2.COLOR_RGB2GRAY)
            ......
    elif filter == 'Black and White':
            ......
    elif filter == 'Pencil Sketch':
            ......
    elif filter == 'Blur Effect':
            ......
    else:
        st.image(image, width=300)

想要获取源码的童鞋,可以在公众号后台私信回复“美图秀秀”即可

NO.1

往期推荐

Historical articles

推荐10个好用到爆的Jupyter Notebook插件,让你效率飞起

推荐10个Github热门Python库,非常实用!

分享10个超实用的Python编程技巧

Python办公自动化 | 一键给PDF文件加密,超方便

分享、收藏、点赞、在看安排一下?

a1939ccd9dd6bb5f007c245eb0f02335.gif

d3eeccf384296b0dff15ea03c95f4306.gif

a0f896ca566fedf033b506cb4926ffd0.gif

e6d56c30b88a4cb978c7a376966f765b.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值