将 WSL2 (Ubuntu 20.04) 目录中的图像加载到 Windows 中加载的网页中时出现问题

问题描述 投票:0回答:1

TL/DR: 基于 Windows 的 Chrome 浏览器将毫无问题地加载基于 WSL2 的 html 文件,但不会加载嵌入的基于 WSL2 的图像,因为未知的解析问题不会在文件字符串中添加“Ubuntu”。通过网页检查手动更改图像文件字符串是一个临时修复,在生成 html 页面时将“Ubuntu”添加到文件字符串并不能解决问题。

情况如下: 处理数据后,我想使用 Jupyter Notebook 在网页中显示结果。 因此,我运行以下代码:

import pandas as pd
from IPython.core.display import HTML


# For showing images in Pandas DF
def path_to_image_html(path, windows = True):
    return '<img src="'+ path + '" width="250" >'

df = pd.DataFrame(columns = ['img_guid', 'img_text', 'img_original', 'img_processed', 'img_filestring'])

for image_name in images.items():
    img_processed = process(img_original)
    data_output_dict = {
        'img_guid': [img_guid], 
        'img_text': [text.rstrip()], 
        'img_original': [path_to_image_html(img_original)],
        'img_processed': [path_to_image_html(img_processed)],
        'img_filestring': [img_filestring]
    }
    df_temp = pd.DataFrame(data_output_dict)
    df = pd.concat([df, df_temp], ignore_index=True)

html_output = HTML(df.to_html(escape=False))

with open('html_file.html', 'w') as f: f.write(html_output.data)

然而,这就是结果:(参见 Imgur 上的图片 --> Example of the webpage

所以网页看起来就像我想要的那样,只是图像没有加载。它们的文件路径是

file://wsl.localhost/home/Oaknut/ProjectDir/img_dir/example_image.png
,而 html 的文件位置是
file://wsl.localhost/Ubuntu/home/Oaknut/ProjectDir/html_file.html
- 在浏览器中手动添加图像文件字符串
中缺少的 
Ubuntu 位,使用 html 检查器 会使所述图像出现,但这当然是不是解决问题的方法。将行
if windows: path = 'Ubuntu' + path
添加到上面显示的
path_to_image_html
函数并不能解决问题,因为这将使图像(在重新编译的 html 文件中)具有文件位置
file://wsl.localhost/Ubuntu/home/Oaknut/ProjectDir/Ubuntu/home/Oaknut/ProjectDir/img_dir/example_image.png

  • 系统设置:使用 Windows 11 和 WSL2,通过 Windows 上安装的 VScode 在 WSL2 中运行 Python 3.8(我需要 CUDA,因此我使用 WSL2),并且在 Windows 上使用 Chrome。
python pandas windows-subsystem-for-linux wsl-2
1个回答
0
投票

img_processed
中存储的值是多少?您是否确认它是您所期望的?

© www.soinside.com 2019 - 2024. All rights reserved.