如何将png图像打印到html?
我有:
print("Content-Type: image/png\n")
print(open('image.png', 'rb').read())上面印着:
Content-Type: image/png
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x0 ...那的回答对我没有帮助。我有这个:
Content-Type: image/png �PNG IHDR�X��%sBIT|d� pHYsaa�?�i IDAT...HTTP服务器:
from http.server import HTTPServer, CGIHTTPRequestHandler
server_address = ("", 8000)
httpd = HTTPServer(server_address, CGIHTTPRequestHandler)
httpd.serve_forever()发布于 2016-11-06 16:33:07
编辑:扩展的使用不同语言的CGI脚本的简单CGI服务器源代码。
我有结构:(所有代码都在末尾)
project
├── cgi-bin
│ └── image.py
├── image.png
├── index.html
└── server.py我运行./server.py (或python3 server.py)
CGI服务器不需要额外的代码就可以提供图像。你可以试试
http://localhost:8000/image.png或在HTML中添加标记(即。在index.html中)
< img src="/image.png" > 然后跑
http://localhost:8000/index.html如果您需要动态创建图像,那么使用脚本ie创建文件夹cgi-bin。image.py
(在Linux上,您必须设置执行属性chmod +x image.py)
然后您可以直接运行这个脚本。
http://localhost:8000/cgi-bin/image.py或在HTML中
< img src="/cgi-bin/image.py" >server.py
#!/usr/bin/env python3
from http.server import HTTPServer, CGIHTTPRequestHandler
server_address = ("", 8000)
httpd = HTTPServer(server_address, CGIHTTPRequestHandler)
httpd.serve_forever()cgi-bin/image.py
#!/usr/bin/env python3
import sys
import os
src = "image.png"
length = os.stat(src).st_size
sys.stdout.write("Content-Type: image/png\n")
sys.stdout.write("Content-Length: " + str(length) + "\n")
sys.stdout.write("\n")
sys.stdout.flush()
sys.stdout.buffer.write(open(src, "rb").read())index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Index</title>
</head>
<body>
<h1>image.png</h1>
<img src="/image.png">
<h1>cgi-bin/image.py</h1>
<img src="/cgi-bin/image.py">
</body>
</html>image.png

https://stackoverflow.com/questions/40450791
复制相似问题