Prefixes and Suffixes-DIV3

探讨了一种解决Prefixes和Suffixes问题的算法,该问题要求从一组混乱的前缀和后缀中识别出哪些是前缀,哪些是后缀。通过找到最长的两个字符串并尝试构建原始字符串,然后验证构建的字符串是否正确包含所有提供的前缀和后缀。
  • Prefixes and Suffixes
  • 题意:一个长度为n的字符串,给出 2n-2个前缀和后缀。打乱顺序的,然后让你输出这2n-2的分别是前缀还是后缀。
  • 思路:找到两个最长的,两种构造方式选一个放在前面另一个的最后一个字母加上即可。然后进行这两种构造原串的
  • 方式哪一种成立即可,输出任意一个成立的,注意验证时只要调用FIND函数查找,子串位置时候是0即可,有可能有些前后缀
  • 相同的情况,加一个标记。 如果一个字符被标记了前缀,那么下次再标记它一定是后缀,如果在查找子串过程中出现了-1是非法状态,并且计数S,P都为n-1才可以。
  • #include<bits/stdc++.h>
    using namespace std;
    #define maxn 1223
    map<string,bool>po;
    vector<char>skt;
    string ans1,ans2,son[maxn],tp;
    string s1,s2;
    int n,len;
    int main()
    {
        cin>>n;
        len=0;
        for(int i=1; i<=n*2-2; i++)
        {
            cin>>son[i];
            if(son[i].size()>len)
            {
                len=son[i].size();
                s1=son[i];
            }
            else if(son[i].size()==len)
                s2=son[i];
        }
        ans1=s1;
        ans1+=s2[len-1];
        ans2=s2;
        ans2+=s1[len-1];
        int lhk=0,flag=0;
        for(int i=1; i<=2*n-2; i++)
        {
            if(ans1.find(son[i])==-1)
            {
                flag=1;
                break;
            }
            if(ans1.find(son[i])==0)
            {
                if(po[son[i]]==0)
                {
                    po[son[i]]=1;
                    skt.push_back('P');
                    lhk++;
                }
                else skt.push_back('S');
            }
            else
                skt.push_back('S');
        }
        if(lhk==n-1&&flag==0)
        {
            for(int i=0; i<2*n-2; i++)
                cout<<skt[i];
            cout<<endl;
        }
        else
        {
            po.clear();
            skt.clear();
            for(int i=1; i<=2*n-2; i++)
                if(ans2.find(son[i])==0)
                {
                    if(po[son[i]]==0)
                    {
                        po[son[i]]=1;
                        skt.push_back('P');
                    }
                    else skt.push_back('S');
                }
                else
                    skt.push_back('S');
            for(int i=0; i<2*n-2; i++)
                cout<<skt[i];
            cout<<endl;
        }
        return 0;
    }

     

import base64 import os import shutil from bs4 import BeautifulSoup import tqdm import webbrowser # 配置参数 root_folder = r"C:\Users\admin\Desktop\out1\out1" # 替换为你的文件夹路径 output_html = os.path.join(root_folder, "output.html") # 将 HTML 保存到 root_folder 下 prefixes = [&#39;C2&#39;, &#39;C3&#39;, &#39;E&#39;, &#39;G&#39;] suffixes = [&#39;MP1.png&#39;, &#39;MP2.png&#39;, &#39;MP3.png&#39;, &#39;MPC.png&#39;] # 创建用于存放图片的子文件夹 image_output_dir = os.path.join(root_folder, "images") os.makedirs(image_output_dir, exist_ok=True) # 提取所有“标识符-序号” identifiers_and_indices = [] for folder in os.listdir(root_folder): if not os.path.isdir(os.path.join(root_folder, folder)): continue parts = folder.split(&#39;_&#39;) if len(parts) < 1: continue identifier_index_part = parts[0] if &#39;-&#39; not in identifier_index_part: continue identifier, index = identifier_index_part.split(&#39;-&#39;) identifiers_and_indices.append((identifier, int(index))) # 去重并排序 unique_identifier_index = sorted(set(identifiers_and_indices), key=lambda x: (x[1], x[0])) all_identifiers = sorted(set([id for id, _ in unique_identifier_index])) all_indices = sorted(set([idx for _, idx in unique_identifier_index])) txt = "".join([f&#39;<li><a href="#power-{index}" data-power="{index}">第{index}次测试</li></a>&#39; for index in all_indices]) # 构建 HTML 表格和 JavaScript 图片放大功能 html_content = """ <html> <head> <title>图像表格</title> <style> body { font-family: Arial, sans-serif; } table { border-collapse: collapse; margin-bottom: 40px; } td, th { border: 1px solid #ccc; padding: 5px; text-align: center; } img { width: 90%; height: auto; cursor: pointer; transition: transform 0.2s ease-in-out; loading: lazy; } img:hover { transform: scale(1.1); } /* 图片弹窗样式 */ .modal { display: none; position: fixed; z-index: 1000; left: 0; top: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.9); animation: fadeIn 0.15s; } .modal-info { color: white; margin-top: 15px; font-size: 1.1rem; text-align: center; } .modal-content { position: relative; margin: auto; padding: 20px; max-width: 60%; max-height: 60%; display: flex; flex-direction: column; justify-content: center; align-items: center; overflow: auto; } .modal-image { max-width: 60%; max-height: 60%; border: 2px solid white; border-radius: 5px; box-shadow: 0 5px 20px rgba(0, 0, 0, 0.5); transform:center; } .close { position: absolute; top: 15px; right: 35px; color: #f1f1f1; font-size: 40px; font-weight: bold; transition: 0.3s; } .close:hover, .close:focus { color: #bbb; text-decoration: none; cursor: pointer; } .prev, .next { position: absolute; top: 50%; transform: translateY(-50%); font-size: 80px; color: white; cursor: pointer; } .prev { left: 200px; } .next { right: 200px; } .prev:hover, .next:hover { color: #ccc; } .sidebar { position: fixed; /* 固定定位 */ top: 0; /* 距离顶部 0 */ left: 0; /* 左边对齐 */ width: 100%; height:20px background-color: #333; color: white; padding: 15px 10px; z-index: 1000; /* 确保在其他内容之上 */ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2); } .sidebar h2 { margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px solid #ddd; font-size: 1.2rem; } .nav-links { list-style: none; display: flex; /* 使用 Flex 布局 */ flex-direction: row; /* 横向排列(可省略,默认就是 row) */ gap: 10px; /* 可选:设置导航项之间的间距 */ } .nav-links li { margin-bottom: 8px; } .nav-links a { display: inline-block; padding: 8px 12px; color: var(--dark-color); text-decoration: none; border-radius: 4px; transition: all 0.3s; font-size: 0.9rem; } .nav-links a:hover { background: var(--primary-color); color: white; } .nav-links a.active { background: var(--primary-color); color: white; } .content { margin-top: 50px; /* 根据导航栏高度调整 */ padding: 20px; } </style> </head> <body> <div class=&#39;content&#39;> <div class="sidebar"> <ul class="nav-links"> """ + txt + """ </ul> </div> </div> <div id="imageModal" class="modal"> <div> <span class="close" onclick="document.getElementById(&#39;imageModal&#39;).style.display=&#39;none&#39;">×</span> <span class="prev" onclick="prevImage()">❮</span> <span class="next" onclick="nextImage()">❯</span> </div> <div class="modal-content"> <img class="modal-image" id="img01"> <div class="modal-info" id="modalInfo"></div> </div> </div> """ # 存储图片路径和文件名 image_infos = [] for index in tqdm.tqdm(all_indices, desc="处理序号"): html_content += f"<h2 id=power-{index}>第{index}次测试</h2>\n<table border=&#39;1&#39;>\n" html_content += "<tr><th>图片名</th>" + "".join([f"<th>{id}</th>" for id in all_identifiers]) + "</tr>\n" for image_name in prefixes: for suffix in suffixes: full_image_name = f"{image_name}_{suffix}" row = f"<tr><td>{full_image_name.split(&#39;.&#39;)[0]}</td>" for identifier in all_identifiers: folder_path = os.path.join(root_folder, f"{identifier}-{index}_20251118_obsqc") image_path = os.path.join(folder_path, full_image_name) if os.path.exists(image_path): # 复制图片到 images 文件夹,并记录相对路径 new_filename = f"{identifier}-{index}_{full_image_name}" new_image_path = os.path.join(image_output_dir, new_filename) shutil.copy2(image_path, new_image_path) relative_path = os.path.join("images", new_filename).replace("\\", "/") image_infos.append({ &#39;filename&#39;: new_filename, &#39;path&#39;: relative_path }) row += f&#39;<td><img src="{relative_path}" onclick="openModal(\&#39;{relative_path}\&#39;)" width="400" height="400"><p class="image-info">{new_filename}</p></td>&#39; else: row += "<td></td>" row += "</tr>\n" html_content += row html_content += "</table>\n<br/>\n" html_content += """ <script> // 存储所有图片的路径和文件名 var imageList = []; window.onload = function() { // 将 Python 提供的 image_infos 转换为 JS 可用的格式 imageList = """ + str(image_infos) + """; } function openModal(imgSrc) { var modal = document.getElementById("imageModal"); var modalImg = document.getElementById("img01"); var modalInfo = document.getElementById("modalInfo"); if (!modal || !modalImg || !modalInfo) { console.error("找不到模态框或相关信息元素,请检查 HTML 结构!"); return; } modal.style.display = "block"; modalImg.src = imgSrc; // 找到当前图片在数组中的位置 window.currentImageIndex = imageList.findIndex(item => item.path === imgSrc); console.log(imageList[window.currentImageIndex]) modalInfo.textContent = imageList[window.currentImageIndex].filename; } // 上一张图片 function prevImage() { if (window.currentImageIndex > 0) { window.currentImageIndex--; var currentImage = imageList[window.currentImageIndex]; document.getElementById("img01").src = currentImage.path; document.getElementById("modalInfo").textContent = currentImage.filename; } } // 下一张图片 function nextImage() { if (window.currentImageIndex < imageList.length - 1) { window.currentImageIndex++; var currentImage = imageList[window.currentImageIndex]; document.getElementById("img01").src = currentImage.path; document.getElementById("modalInfo").textContent = currentImage.filename; } } // 关闭模态框 window.onclick = function(event) { var modal = document.getElementById("imageModal"); if (event.target == modal) { modal.style.display = "none"; } } document.onkeydown = function(e) { if (e.key === "Escape") { document.getElementById("imageModal").style.display = "none"; } else if (e.key === "ArrowLeft") { prevImage(); } else if (e.key === "ArrowRight") { nextImage(); } } </script> </body> </html> """ # 写入 HTML 文件 with open(output_html, "w", encoding="utf-8") as f: f.write(html_content) print(f"HTML 文件已生成:{output_html}") webbrowser.open(output_html) 帮我优化修改导航栏
最新发布
11-20
import base64 import os from bs4 import BeautifulSoup import tqdm import webbrowser # 配置参数 root_folder = r"C:\Users\admin\Desktop\out1\out1" # 替换为你的文件夹路径 output_html = "output.html" prefixes = [&#39;C2&#39;, &#39;C3&#39;, &#39;E&#39;, &#39;G&#39;] suffixes = [&#39;MP1.png&#39;, &#39;MP2.png&#39;, &#39;MP3.png&#39;, &#39;MPC.png&#39;] image_infos = [] # 提取所有“标识符-序号” identifiers_and_indices = [] for folder in os.listdir(root_folder): if not os.path.isdir(os.path.join(root_folder, folder)): continue parts = folder.split(&#39;_&#39;) if len(parts) < 1: continue identifier_index_part = parts[0] if &#39;-&#39; not in identifier_index_part: continue identifier, index = identifier_index_part.split(&#39;-&#39;) identifiers_and_indices.append((identifier, index)) # 去重并排序 unique_identifier_index = sorted(set(identifiers_and_indices), key=lambda x: (x[1], x[0])) all_identifiers = sorted(set([id for id, _ in unique_identifier_index])) all_indices = sorted(set([idx for _, idx in unique_identifier_index])) # 构建 HTML 表格和 JavaScript 图片放大功能 # 构建 HTML 表格和 JavaScript 图片放大功能 html_content = """ <html> <head> <title>图像表格</title> <style> body { font-family: Arial, sans-serif; } table { border-collapse: collapse; margin-bottom: 40px; } td, th { border: 1px solid #ccc; padding: 5px; text-align: center; } img { width: 360px; height: auto; cursor: pointer; transition: transform 0.2s ease-in-out; } img:hover { transform: scale(1.1); } /* 图片弹窗样式 */ .modal { display: none; position: fixed; z-index: 1000; left: 0; top: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.9); animation: fadeIn 0.15s; } .modal-info { color: white; margin-top: 15px; font-size: 1.1rem; text-align: center; } .modal-content { position: relative; margin: auto; padding: 20px; max-width: 900px; max-height: 100%; display: flex; flex-direction: column; justify-content: center; align-items: center; overflow: auto; } .modal-image { max-width: 100%; max-height: 100%; border: 2px solid white; border-radius: 5px; box-shadow: 0 5px 20px rgba(0, 0, 0, 0.5); transform:center; } .image-item .image-info { margin-top: 10px; font-style: italic; color: var(--gray-color); font-size: 0.9rem; } .close { position: absolute; top: 15px; right: 35px; color: #f1f1f1; font-size: 40px; font-weight: bold; transition: 0.3s; } .close:hover, .close:focus { color: #bbb; text-decoration: none; cursor: pointer; } .prev, .next { position: absolute; top: 50%; transform: translateY(-50%); font-size: 40px; color: white; cursor: pointer; } .prev { left: 20px; } .next { right: 20px; } .prev:hover, .next:hover { color: #ccc; } </style> </head> <div id="imageModal" class="modal"> <span class="close" onclick="document.getElementById(&#39;imageModal&#39;).style.display=&#39;none&#39;">×</span> <span class="prev" onclick="prevImage()">❮</span> <span class="next" onclick="nextImage()">❯</span> <img class="modal-content" id="img01"> <div class="modal-info" id="modalInfo"></div> </div> <body> """ for index in tqdm.tqdm(all_indices, desc="处理序号"): html_content += f"<h2>序号 {index}</h2>\n<table border=&#39;1&#39;>\n" html_content += "<tr><th>图片名</th>" + "".join([f"<th>{id}</th>" for id in all_identifiers]) + "</tr>\n" for image_name in prefixes: for suffix in suffixes: full_image_name = f"{image_name}_{suffix}" row = f"<tr><td>{full_image_name}</td>" for identifier in all_identifiers: folder_path = os.path.join(root_folder, f"{identifier}-{index}_20251118_obsqc") image_path = os.path.join(folder_path, full_image_name) if os.path.exists(image_path): with open(image_path, "rb") as img_file: encoded_str = base64.b64encode(img_file.read()).decode("utf-8") base64_img = f"data:image/png;base64,{encoded_str}" image_infos.append({ &#39;filename&#39;: f&#39;{identifier}-{index}_{full_image_name}&#39;, &#39;img&#39;: base64_img }) row += f&#39;<td><img src="{base64_img}" onclick="openModal(this.src)" width="400" height="400"><p class="image-info">{identifier}-{index}_{full_image_name}</p></td>&#39; else: row += "<td></td>" row += "</tr>\n" html_content += row html_content += "</table>\n<br/>\n" html_content += "</body></html>" html_content += """ <script> // 存储所有图片的 base64 数据 var images = []; var images_name = []; const modalInfo = document.getElementById(&#39;modalInfo&#39;); console.log(modalInfo) window.onload = function() { // 将 Python 提供的 image_infos 转换为 JS 可用的格式 images = """ + str([info[&#39;img&#39;] for info in image_infos]) + """; images_name = """ + str([info[&#39;filename&#39;] for info in image_infos]) + """; } function openModal(imgSrc) { var modal = document.getElementById("imageModal"); var modalImg = document.getElementById("img01"); modal.style.display = "block"; modalImg.src = imgSrc; // 找到当前图片在数组中的位置 var currentIndex = images.indexOf(imgSrc); if (currentIndex === -1) { images.push(imgSrc); currentIndex = images.length - 1; } // 更新当前索引 window.currentImageIndex = currentIndex; modalInfo.textContent = images_name[window.currentImageIndex] } // 上一张图片 function prevImage() { if (window.currentImageIndex > 0) { window.currentImageIndex--; document.getElementById("img01").src = images[window.currentImageIndex]; modalInfo.textContent = images_name[window.currentImageIndex] } } // 下一张图片 function nextImage() { if (window.currentImageIndex < images.length - 1) { window.currentImageIndex++; document.getElementById("img01").src = images[window.currentImageIndex]; modalInfo.textContent = images_name[window.currentImageIndex] } } // 关闭模态框 window.onclick = function(event) { var modal = document.getElementById("imageModal"); if (event.target == modal) { modal.style.display = "none"; } } document.onkeydown = function(e) { if (e.key === "Escape") { document.getElementById("imageModal").style.display = "none"; } else if (e.key === "ArrowLeft") { prevImage(); } else if (e.key === "ArrowRight") { nextImage(); } } </script> """ # 写入 HTML 文件 with open(output_html, "w", encoding="utf-8") as f: f.write(html_content) print(f"HTML 文件已生成:{output_html}") webbrowser.open(output_html) 将图片保存方式修改为懒加载,记录相对文件夹路径,html保存到root_folder 路径下
11-20
import base64 import os from bs4 import BeautifulSoup # 配置参数 root_folder = r"C:\Users\admin\Desktop\out1\out1" # 替换为你的文件夹路径 output_html = "output.html" prefixes = [&#39;C2&#39;, &#39;C3&#39;, &#39;E&#39;, &#39;G&#39;] suffixes = [&#39;MP1.png&#39;, &#39;MP2.png&#39;, &#39;MP3.png&#39;, &#39;MPC.png&#39;] # 提取所有“标识符-序号” identifiers_and_indices = [] for folder in os.listdir(root_folder): if not os.path.isdir(os.path.join(root_folder, folder)): continue parts = folder.split(&#39;_&#39;) if len(parts) < 1: continue identifier_index_part = parts[0] if &#39;-&#39; not in identifier_index_part: continue identifier, index = identifier_index_part.split(&#39;-&#39;) identifiers_and_indices.append((identifier, index)) # 去重并排序 unique_identifier_index = sorted(set(identifiers_and_indices), key=lambda x: (x[1], x[0])) all_identifiers = sorted(set([id for id, _ in unique_identifier_index])) all_indices = sorted(set([idx for _, idx in unique_identifier_index])) # 构建 HTML 表格和 JavaScript 图片放大功能 html_content = """ <html> <head> <title>图像表格</title> <style> body { font-family: Arial, sans-serif; } table { border-collapse: collapse; margin-bottom: 40px; } td, th { border: 1px solid #ccc; padding: 5px; text-align: center; } img { width: 100px; height: auto; cursor: pointer; transition: transform 0.2s ease-in-out; } img:hover { transform: scale(1.1); } /* 图片弹窗样式 */ .modal { display: none; position: fixed; z-index: 1000; left: 0; top: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.9); animation: fadeIn 0.15s; } .modal-content { position: relative; margin: auto; padding: 20px; width: 90%; max-width: 900px; /* 调整最大宽度 */ max-height: 100%; /* 调整最大高度 */ display: flex; flex-direction: column; justify-content: center; align-items: center; overflow: auto; /* 允许滚动 */ } .close { position: absolute; top: 15px; right: 35px; color: #f1f1f1; font-size: 40px; font-weight: bold; transition: 0.3s; } .close:hover, .close:focus { color: #bbb; text-decoration: none; cursor: pointer; } </style> </head> <body> <script> function openModal(imgSrc) { var modal = document.getElementById("imageModal"); var modalImg = document.getElementById("img01"); modal.style.display = "block"; modalImg.src = imgSrc; } window.onclick = function(event) { var modal = document.getElementById("imageModal"); if (event.target == modal) { modal.style.display = "none"; } } document.onkeydown = function(e) { if (e.key === "Escape") { document.getElementById("imageModal").style.display = "none"; } } </script> <div id="imageModal" class="modal"> <span class="close" onclick="document.getElementById(&#39;imageModal&#39;).style.display=&#39;none&#39;">×</span> <img class="modal-content" id="img01"> </div> """ for index in all_indices: html_content += f"<h2>序号 {index}</h2>\n<table border=&#39;1&#39;>\n" html_content += "<tr><th>图片名</th>" + "".join([f"<th>{id}</th>" for id in all_identifiers]) + "</tr>\n" for image_name in prefixes: for suffix in suffixes: full_image_name = f"{image_name}_{suffix}" row = f"<tr><td>{full_image_name}</td>" for identifier in all_identifiers: full_image_name = f"{image_name}_{suffixes[0]}" folder_path = os.path.join(root_folder, f"{identifier}-{index}_20251118_obsqc") image_path = os.path.join(folder_path, full_image_name) if os.path.exists(image_path): with open(image_path, "rb") as img_file: encoded_str = base64.b64encode(img_file.read()).decode("utf-8") base64_img = f"data:image/png;base64,{encoded_str}" row += f&#39;<td><img src="{base64_img}" onclick="openModal(this.src)" width="400" height="400"></td>&#39; else: row += "<td></td>" row += "</tr>\n" html_content += row html_content += "</table>\n<br/>\n" html_content += "</body></html>" # 写入 HTML 文件 with open(output_html, "w", encoding="utf-8") as f: f.write(html_content) print(f"HTML 文件已生成:{output_html}") 添加打开大图时,有切换上一张和下一张的功能
11-20
import base64 import os from bs4 import BeautifulSoup import tqdm import webbrowser # 配置参数 root_folder = r"C:\Users\admin\Desktop\out1\out1" # 替换为你的文件夹路径 output_html = "output.html" prefixes = [&#39;C2&#39;, &#39;C3&#39;, &#39;E&#39;, &#39;G&#39;] suffixes = [&#39;MP1.png&#39;, &#39;MP2.png&#39;, &#39;MP3.png&#39;, &#39;MPC.png&#39;] image_infos = [] # 提取所有“标识符-序号” identifiers_and_indices = [] for folder in os.listdir(root_folder): if not os.path.isdir(os.path.join(root_folder, folder)): continue parts = folder.split(&#39;_&#39;) if len(parts) < 1: continue identifier_index_part = parts[0] if &#39;-&#39; not in identifier_index_part: continue identifier, index = identifier_index_part.split(&#39;-&#39;) identifiers_and_indices.append((identifier, index)) # 去重并排序 unique_identifier_index = sorted(set(identifiers_and_indices), key=lambda x: (x[1], x[0])) all_identifiers = sorted(set([id for id, _ in unique_identifier_index])) all_indices = sorted(set([idx for _, idx in unique_identifier_index])) # 构建 HTML 表格和 JavaScript 图片放大功能 # 构建 HTML 表格和 JavaScript 图片放大功能 html_content = """ <html> <head> <title>图像表格</title> <style> body { font-family: Arial, sans-serif; } table { border-collapse: collapse; margin-bottom: 40px; } td, th { border: 1px solid #ccc; padding: 5px; text-align: center; } img { width: 360px; height: auto; cursor: pointer; transition: transform 0.2s ease-in-out; } img:hover { transform: scale(1.1); } /* 图片弹窗样式 */ .modal { display: none; position: fixed; z-index: 1000; left: 0; top: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.9); animation: fadeIn 0.15s; } .modal-info { color: white; margin-top: 15px; font-size: 1.1rem; text-align: center; } .modal-content { position: relative; margin: auto; padding: 20px; width: 90%; max-width: 900px; max-height: 80%; display: flex; flex-direction: column; justify-content: center; align-items: center; overflow: auto; } .modal-image { max-width: 100%; max-height: 100%; border: 2px solid white; border-radius: 5px; box-shadow: 0 5px 20px rgba(0, 0, 0, 0.5); transform:center; } .image-item .image-info { margin-top: 10px; font-style: italic; color: var(--gray-color); font-size: 0.9rem; } .close { position: absolute; top: 15px; right: 35px; color: #f1f1f1; font-size: 40px; font-weight: bold; transition: 0.3s; } .close:hover, .close:focus { color: #bbb; text-decoration: none; cursor: pointer; } .prev, .next { position: absolute; top: 50%; transform: translateY(-50%); font-size: 40px; color: white; cursor: pointer; } .prev { left: 20px; } .next { right: 20px; } .prev:hover, .next:hover { color: #ccc; } </style> </head> <body> """ for index in tqdm.tqdm(all_indices, desc="处理序号"): html_content += f"<h2>序号 {index}</h2>\n<table border=&#39;1&#39;>\n" html_content += "<tr><th>图片名</th>" + "".join([f"<th>{id}</th>" for id in all_identifiers]) + "</tr>\n" for image_name in prefixes: for suffix in suffixes: full_image_name = f"{image_name}_{suffix}" row = f"<tr><td>{full_image_name}</td>" for identifier in all_identifiers: folder_path = os.path.join(root_folder, f"{identifier}-{index}_20251118_obsqc") image_path = os.path.join(folder_path, full_image_name) if os.path.exists(image_path): with open(image_path, "rb") as img_file: encoded_str = base64.b64encode(img_file.read()).decode("utf-8") base64_img = f"data:image/png;base64,{encoded_str}" image_infos.append({ &#39;filename&#39;: f&#39;{identifier}-{index}_{full_image_name}&#39;, &#39;img&#39;: base64_img }) row += f&#39;<td><img src="{base64_img}" onclick="openModal(this.src)" width="400" height="400"><p class="image-info">{identifier}-{index}_{full_image_name}</p></td>&#39; else: row += "<td></td>" row += "</tr>\n" html_content += row html_content += "</table>\n<br/>\n" html_content += "</body></html>" html_content += """ <script> // 存储所有图片的 base64 数据 var images = []; const modalInfo = document.getElementById(&#39;modalInfo&#39;); console.log(modalInfo) window.onload = function() { // 将 Python 提供的 image_infos 转换为 JS 可用的格式 images = """ + str([info[&#39;img&#39;] for info in image_infos]) + """; } function openModal(imgSrc) { var modal = document.getElementById("imageModal"); var modalImg = document.getElementById("img01"); modal.style.display = "block"; modalImg.src = imgSrc; // 找到当前图片在数组中的位置 var currentIndex = images.indexOf(imgSrc); if (currentIndex === -1) { images.push(imgSrc); currentIndex = images.length - 1; } // 更新当前索引 window.currentImageIndex = currentIndex; modalInfo.textContent = images[currentIndex].filename } // 上一张图片 function prevImage() { if (window.currentImageIndex > 0) { window.currentImageIndex--; document.getElementById("img01").src = images[window.currentImageIndex]; modalInfo.textContent = images[window.currentImageIndex].filename } } // 下一张图片 function nextImage() { if (window.currentImageIndex < images.length - 1) { window.currentImageIndex++; document.getElementById("img01").src = images[window.currentImageIndex]; modalInfo.textContent = images[window.currentImageIndex].filename } } // 关闭模态框 window.onclick = function(event) { var modal = document.getElementById("imageModal"); if (event.target == modal) { modal.style.display = "none"; } } document.onkeydown = function(e) { if (e.key === "Escape") { document.getElementById("imageModal").style.display = "none"; } else if (e.key === "ArrowLeft") { prevImage(); } else if (e.key === "ArrowRight") { nextImage(); } } </script> <div id="imageModal" class="modal"> <span class="close" onclick="document.getElementById(&#39;imageModal&#39;).style.display=&#39;none&#39;">×</span> <span class="prev" onclick="prevImage()">❮</span> <span class="next" onclick="nextImage()">❯</span> <div class="modal-content"> <img class="modal-image" id="img01"> <div class="modal-info" id="modalInfo"></div> </div> </div> """ # 写入 HTML 文件 with open(output_html, "w", encoding="utf-8") as f: f.write(html_content) print(f"HTML 文件已生成:{output_html}") webbrowser.open(output_html) 修改完善代码中大图显示,并且在下方显示图片名的逻辑
11-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值