当前位置: 首页 > news >正文

2023.11.20使用flask做一个简单图片浏览器

2023.11.20使用flask做一个简单图片浏览器

功能:
(1)输入指定路径,打开文件夹
(2)判断文件格式为图片
(3)在前端进行预览
(4)使用bootstrap进行简单美化
在这里插入图片描述
main.py

from flask import Flask, request, render_template
import osapp = Flask(__name__)@app.route('/')
def index():return render_template('index.html')@app.route('/preview_images', methods=['POST'])
def preview_images():folder_path = request.form['folder_path']images = []message = ''if os.path.isdir(folder_path):for filename in os.listdir(folder_path):if filename.endswith('.jpg') or filename.endswith('.jpeg') or filename.endswith('.PNG'):images.append(os.path.join(folder_path, filename))if len(images) == 0:message = '该文件夹中没有图片文件'else:message = f'共找到{len(images)}个图片文件'else:message = '该路径不是一个文件夹'return render_template('index.html', images=images, message=message)if __name__ == '__main__':app.run()

index.html

<!DOCTYPE html>
<html>
<head><title>图片预览</title><script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script><link href="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css" rel="stylesheet"><script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js"></script><style>.thumbnail {height: 150px;object-fit: contain;}</style>
</head>
<body><div class="container mt-3"><h2>图片预览</h2><form method="POST" action="/preview_images"><div class="input-group mb-3"><input type="text" id="folderPath" name="folder_path" class="form-control" placeholder="请输入文件夹路径"><button type="submit" class="btn btn-primary">预览</button></div></form>{% if images %}<p>{{ message }}</p><div class="row row-cols-1 row-cols-md-3 g-4">{% for image in images %}<div class="col"><div class="card"><img src="{{ image }}" class="card-img-top thumbnail" alt="..."><div class="card-body"><button type="button" class="btn btn-primary" onclick="previewImage('{{ image }}')">预览</button></div></div></div>{% endfor %}</div>{% else %}<p>{{ message }}</p>{% endif %}</div><script>function previewImage(imageUrl) {window.open(imageUrl, '_blank');}</script>
</body>
</html>
http://www.lryc.cn/news/238631.html

相关文章:

  • https和http的区别和优势
  • Docker 启动alpine镜像中可执行程序文件遇到 not found
  • .net对接阿里云CSB服务
  • Json数据格式
  • Kafka-Producer
  • Ubuntu20上离线安装samba
  • 【开源】基于Vue.js的教学过程管理系统
  • 【C++】泛型编程 ⑪ ( 类模板的运算符重载 - 函数实现 写在类外部的不同的 .h 头文件和 .cpp 代码中 )
  • 动手学深度学习——循环神经网络的简洁实现(代码详解)
  • 19.删除链表的倒数第 N 个节点
  • 机器人制作开源方案 | 莲花灯
  • 华为无线ac+fit三层组网,每个ap发射不同的业务vlan
  • 人工智能:科技之光,生活之美
  • mysql8.0英文OCP考试第61-70题
  • WaveletPool:抗混叠在微小目标检测中的重要性
  • 文章系列2:Unraveling the functional dark matter through global metagenomics
  • ubuntu 20.04 搭建crash dump问题分析环境
  • 算法训练营一刷 总结篇
  • Linux中的MFS分布式文件系统
  • 气相色谱质谱仪样品传输装置中电动针阀和微泄漏阀的解决方案
  • ArkTS基础知识
  • Kotlin学习(二)
  • LangChain 6根据图片生成推广文案HuggingFace中的image-caption模型
  • QFontDialog开发详解
  • 【C++进阶之路】第七篇:异常
  • shell 判断文件是否存在(csh bash)
  • 第六年到第十年是分水岭
  • 关于标准库中的string类 - c++
  • Chrome添加扩展程序
  • C++单调向量算法:132模式枚举1简洁版