• 元宇宙:本站分享元宇宙相关资讯,资讯仅代表作者观点与平台立场无关,仅供参考.

Python 写了一个网页版的「P图软件」,惊呆了!

  • AI科技大本营
  • 2022年2月07日05时

作者 | 小欣

来源 |Python爱好者集中营

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

网页左侧的工具栏部分

首先第一步在于导入我们所需要用到的模块,代码如下
importstreamlitasst
importnumpyasnp
importcv2
fromPILimportImage,ImageEnhance
我们先设计到网页当中的标题和左边的工具栏,左边的工具栏部分代码如下
st.sidebar.markdown('<pclass="font">MyFirstPhotoConverterApp</p>',unsafe_allow_html=True)
withst.sidebar.expander("AbouttheApp"):
st.write("""
Usethissimpleapptoconvertyourfavoritephototoapencilsketch,agrayscaleimageoranimagewithblurringeffect.\n\nThisappwascreatedbyJunxinasasideprojecttolearnStreamlitandcomputervision.Hopeyouenjoy!
"""
)
首先在工具栏当中添加的是关于该应用的介绍,通过调用streamlit模块当中的sidebar方法来生成一个下拉框,在通过点击“+”键来打开,
然后则是5个单选框,代码如下
filter=st.sidebar.radio('Covertyourphototo:',['Original','GrayImage','BlackandWhite','PencilSketch','BlurEffect'])
单选框是通过sidebar方法来实现的,当中添加上分别是哪些的单选项,
紧接着是最后的“感谢反馈”的部分,代码如下
st.sidebar.title('')
st.sidebar.markdown('')
st.sidebar.subheader('Pleasehelpusimprove!')
withst.sidebar.form(key='columns_in_form',clear_on_submit=True):
rating=st.slider("Pleaseratetheapp",min_value=1,max_value=5,value=3,help='Dragtheslidertoratetheapp.Thisisa1-5ratingscalewhere5isthehighestrating')
text=st.text_input(label='Pleaseleaveyourfeedbackhere')
submitted=st.form_submit_button('Submit')
ifsubmitted:
.......

网页正文的标题部分

标题部分的代码如下
withcol1:
st.markdown("""<style>.font{
font-size:35px;font-family:'CooperBlack';color:#FF9633;}
</style>"""
,unsafe_allow_html=True)
st.markdown('<pclass="font">Uploadyourphotohere...</p>',unsafe_allow_html=True)
当然除此之外,我们需要的是上传图片的功能,代码如下
uploaded_file=st.file_uploader("",type=['jpg','png','jpeg'])
这里调用的是streamlit模块当中的file_uploader方法,其中允许上传的有三种文件格式,分别是jpgpng以及jpeg
接下去便是上传图片的处理逻辑部分,代码如下,
withcol2:
filter=st.sidebar.radio('Covertyourphototo:',['Original','GrayImage','BlackandWhite','PencilSketch','BlurEffect'])
iffilter=='GrayImage':
converted_img=np.array(image.convert('RGB'))
gray_scale=cv2.cvtColor(converted_img,cv2.COLOR_RGB2GRAY)
......
eliffilter=='BlackandWhite':
......
eliffilter=='PencilSketch':
......
eliffilter=='BlurEffect':
......
else:
st.image(image,width=300)

资讯

谷歌使出禁用2G大招

资讯

大型模型语言能够理解吗?

技术

创意十足的Python命令行工具

资讯

游戏圈地震级消息,微软收购动视暴雪


分享

点收藏

点点赞

点在看

Copyright © 2021.Company 元宇宙YITB.COM All rights reserved.元宇宙YITB.COM