python 以html的形式显示测试结果图片

# coding:utf-8

import webbrowser
import sys
import os

# 准备相关变量

txt_file = "./results"
txt_file_list = os.listdir(txt_file)
data = [i for i in txt_file_list]

res_list = []
for j, i in enumerate(data):
    new_name = os.path.join("./results", i)
    if j % 3 == 0:
        res_list.append("<img src=" + new_name + "><br>")
    else:
        res_list.append("<img src=" + new_name + ">")

img_str = ""
for i in res_list:
    img_str += i

# 命名生成的html
GEN_HTML = "test_result.html"
# 打开文件,准备写入
f = open(GEN_HTML, 'w')

message = """
<!DOCTYPE html>
<html>
<head>    
    <link rel="stylesheet" type="text/css" href="task001.css">
    <meta charset="utf-8">
    <title>test</title>
    <style type="text/css">
    *{margin: 0; padding: 0;}
    .clearfix:after {
          clear: both;
          content: ".";
          display: block;
          height: 0;
          visibility: hidden;
        }
      .wrap {
        /* 可无需设置高度,靠图片高度 或者 文字的上下padding撑开高度 */
            border: 1px solid;
            width: 600px;
            text-align: center; margin: 0 auto;
        }
        .wrap span{
            display: inline-block;
            vertical-align: middle;
            padding: 20px 0; /* 撑开高度 */
        }
        .wrap img{
            width: auto;
            height: auto;
            vertical-align: middle;
        }
    </style>
</head>
<body>    
    <div class="wrap">
        %s
    </div>
</body>
</html>""" % (img_str)

# 写入文件
f.write(message)
# 关闭文件
f.close()

# 运行完自动在网页中显示
webbrowser.open(GEN_HTML, new=1)
'''
webbrowser.open(url, new=0, autoraise=True) 
Display url using the default browser. If new is 0, the url is opened in the same browser window if possible. If new is 1, a new browser window is opened if possible. If new is 2, a new browser page (“tab”) is opened if possible. If autoraise is True, the window is raised if possible (note that under many window managers this will occur regardless of the setting of this variable).
'''

其中 txt_file 是图像所在的文件夹
GEN_HTML 是生成的html文件名
这里是设置每4张图像换行一次

生成结果示意图:
在这里插入图片描述

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值