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

大邓和他的Python

共 3008字,需浏览 7分钟

 · 2022-02-26

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

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

网页左侧的工具栏部分

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

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

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

st.sidebar.markdown('My First Photo Converter App

'
, 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方法来生成一个下拉框,在通过点击“+”键来打开,

然后则是5个单选框,代码如下
filter = st.sidebar.radio('Covert your photo to:', ['Original''Gray Image''Black and White''Pencil Sketch''Blur Effect'])
单选框是通过sidebar方法来实现的,当中添加上分别是哪些的单选项,

紧接着是最后的“感谢反馈”的部分,代码如下
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:
      .......

网页正文的标题部分

标题部分的代码如下

with col1:
    st.markdown("" """, unsafe_allow_html=True)
    st.markdown('Upload your photo here...

'
, 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)

源码链接:https://pan.baidu.com/s/1jih-53VfRAWUUM5fbp1OFw 提取码:krst

近期文章

视频专栏课 | Python网络爬虫与文本分析

读完本文你就了解什么是文本分析

综述:文本分析在市场营销研究中的应用

Pandas库 | 高速读取csv文件的方法

如何在DataFrame中使用If-Else条件语句创建新列

BERTopic 主题建模库 | 建议收藏

Top2Vec | 主题建模和语义搜索库

案例实战 | 企业信息数据采集

使用文本相似度可以识别变化的时间点

PNAS | 文本网络分析&文化桥梁Python代码实现

tomotopy | 速度最快的LDA主题模型

dvt | 视觉文化分析的Python工具包

Stargazer库 | 创建漂亮可发表的多元回归表

人文社科类Python免费教程列表

量化历史语言学-贝叶斯语言谱系分析

Python与文化分析入门

Backtrader库 | 均线买入卖出策略实现

在会计研究中使用Python进行文本分析

文本分析方法在《管理世界》(2021.5)中的应用

hiResearch 定义自己的科研首页

SciencePlots | 科研样式绘图库

Wow~70G上市公司定期报告数据集

漂亮~pandas可以无缝衔接Bokeh  

YelpDaset: 酒店管理类数据集10+

浏览 66
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报