python

#base64形式のデータ('base64.txt')をHTML('png-base64.html')
#に読み込み画像(png)を表示する
import base64

input_file = 'base64.txt'
output_file = 'png-base64.html'

# ファイルをUTF-8で読み込み、BOMを無視してUTF-8で書き出し
with open(input_file, 'r', encoding='utf-8-sig') as f_input:
    content = f_input.read()
    with open(output_file, 'w', encoding='utf-8') as f_output:
        f_output.write(content)

# Base64形式の画像データを読み込む
with open(output_file, 'r') as file:
    base64_data = file.read()

# HTMLコードを生成する関数
def generate_html(base64_data):
    html_code = f'''
    <!DOCTYPE html>
    <html>
    <head>
        <title>Base64 Image Display</title>
    </head>
    <body>
        <h3>Base64 Image Display</h3>
        <img src="data:image/png;base64,{base64_data}" alt="Base64 Image">
    </body>
    </html>
    '''
    return html_code

# HTMLコードを生成
html_content = generate_html(base64_data)

# HTMLファイルに保存
with open(output_file, 'w') as file:
    file.write(html_content)

print(f"HTML code has been generated and saved to {output_file}")