提交 8e4f724a 编写于 作者: Y ylwdev

Mon Feb 19 17:06:00 CST 2024 inscode

上级 0520122c
import streamlit as st
from streamlit_option_menu import option_menu
# 设置Streamlit应用程序的标题
st.set_page_config(page_title="app name", layout="wide")
menu1="菜单1"
menu2="菜单2"
with st.sidebar:
menu = option_menu("菜单", [menu1, menu2],
icons=['house', "list-task"],
menu_icon="cast", default_index=0)
from PIL import Image
import os
def main():
if menu == menu1:
st.subheader(f"{menu1}")
if menu == menu2:
st.subheader(f"{menu2}")
if __name__ == '__main__':
main()
st.title("图片居中剪裁")
# 图片上传功能
uploaded_file = st.file_uploader("上传一张图片", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# 打印上传的图片信息,方便调试
st.write("原始图片信息:", uploaded_file)
# 读取上传的图片
image = Image.open(uploaded_file)
# 显示原始图片
st.image(image, caption="原始图片", use_column_width=True)
# 图片居中剪裁功能
# 获取图片尺寸
width, height = image.size
# 按宽高比例计算剪裁后的尺寸
if width > height:
new_width = int(height * (16 / 9))
new_height = height
else:
new_width = width
new_height = int(width * (9 / 16))
# 计算剪裁起始点坐标
left = int((width - new_width) / 2)
top = int((height - new_height) / 2)
right = int((width + new_width) / 2)
bottom = int((height + new_height) / 2)
# 进行剪裁
image_cropped = image.crop((left, top, right, bottom))
# 显示剪裁后的图片
st.image(image_cropped, caption="剪裁后的图片", use_column_width=True)
# 图片剪裁后保存功能
# 获取保存路径
save_path = st.text_input("请输入要保存的文件夹路径")
# 如果路径存在且不为空
if save_path and os.path.exists(save_path):
# 获取保存文件名
filename = st.text_input("请输入要保存的文件名")
# 如果文件名不为空
if filename:
# 拼接文件保存路径
save_file_path = os.path.join(save_path, filename)
# 保存剪裁后的图片
image_cropped.save(save_file_path)
st.write("图片已保存至:", save_file_path)
if __name__ == "__main__":
main()
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册