HTML 使用 Python 进行图像处理
HTML to IMAGE using Python
这里有一个变量html_str,它是一个包含html个标签和body内容的字符串。
我使用 python.
中的以下代码从此字符串创建了一个 .html 文件
html_file = open("filename.html", "w")
html_file.write(html_str)
html_file.close()
现在我得到了 html 个名为文件“filename.html”的文件。现在 我想将那个 "filename.html" 转换为图像 ,命名为 filename.jpg,具有 html 文件的确切内容。
请帮助我。
您可以使用 imgkit
import imgkit
imgkit.from_file('test.html', 'out.jpg')
或者你也可以使用htmlcsstoimage Api
# pip3 install requests
import requests
HCTI_API_ENDPOINT = "https://hcti.io/v1/image"
HCTI_API_USER_ID = 'your-user-id'
HCTI_API_KEY = 'your-api-key'
data = { 'html': "<div class='box'>Hello, world!</div>",
'css': ".box { color: white; background-color: #0f79b9; padding: 10px; font-family: Roboto }",
'google_fonts': "Roboto" }
image = requests.post(url = HCTI_API_ENDPOINT, data = data, auth=(HCTI_API_USER_ID, HCTI_API_KEY))
print("Your image URL is: %s"%image.json()['url'])
# https://hcti.io/v1/image/7ed741b8-f012-431e-8282-7eedb9910b32
另一个非常有用的呈现 HTML 网站的工具是无头 Chromium 浏览器。
在 javascript 中,您可以使用 puppeteer api 与其交互,但有一个非官方的 python puppeteer 端口称为 pyppeteer
根据我使用 python 工具(如 imgkit 的经验),在加载图像或 iFrame 等外部资源时,Chromium 解决方案要可靠得多。
要使用 pyppeteer 获取呈现的 HTML 的图像版本,您只需加载页面,然后制作完整的页面屏幕截图:
import asyncio
from pyppeteer import launch
async def main():
browser = await launch()
page = await browser.newPage()
await page.goto('http://example.com')
await page.screenshot({'path': 'example.png', 'fullPage': 'true'})
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
查看 HtmlWebShot
# pip install htmlwebshot
from htmlwebshot import WebShot
shot = WebShot()
shot.quality = 100
image = shot.create_pic(html="file.html")
如果您不希望您的项目像其他 Python 模块一样依赖于 wkhtmltopdf,我推荐 html2image。
from html2image import Html2Image
hti = Html2Image()
hti.screenshot(url='https://www.python.org', save_as='python_org.png')
from html2image import Html2Image
hti = Html2Image()
with open('./test.html') as f:
hti.screenshot(f.read(), save_as='out.png')
这里有一个变量html_str,它是一个包含html个标签和body内容的字符串。 我使用 python.
中的以下代码从此字符串创建了一个 .html 文件html_file = open("filename.html", "w")
html_file.write(html_str)
html_file.close()
现在我得到了 html 个名为文件“filename.html”的文件。现在 我想将那个 "filename.html" 转换为图像 ,命名为 filename.jpg,具有 html 文件的确切内容。 请帮助我。
您可以使用 imgkit
import imgkit
imgkit.from_file('test.html', 'out.jpg')
或者你也可以使用htmlcsstoimage Api
# pip3 install requests
import requests
HCTI_API_ENDPOINT = "https://hcti.io/v1/image"
HCTI_API_USER_ID = 'your-user-id'
HCTI_API_KEY = 'your-api-key'
data = { 'html': "<div class='box'>Hello, world!</div>",
'css': ".box { color: white; background-color: #0f79b9; padding: 10px; font-family: Roboto }",
'google_fonts': "Roboto" }
image = requests.post(url = HCTI_API_ENDPOINT, data = data, auth=(HCTI_API_USER_ID, HCTI_API_KEY))
print("Your image URL is: %s"%image.json()['url'])
# https://hcti.io/v1/image/7ed741b8-f012-431e-8282-7eedb9910b32
另一个非常有用的呈现 HTML 网站的工具是无头 Chromium 浏览器。
在 javascript 中,您可以使用 puppeteer api 与其交互,但有一个非官方的 python puppeteer 端口称为 pyppeteer
根据我使用 python 工具(如 imgkit 的经验),在加载图像或 iFrame 等外部资源时,Chromium 解决方案要可靠得多。
要使用 pyppeteer 获取呈现的 HTML 的图像版本,您只需加载页面,然后制作完整的页面屏幕截图:
import asyncio
from pyppeteer import launch
async def main():
browser = await launch()
page = await browser.newPage()
await page.goto('http://example.com')
await page.screenshot({'path': 'example.png', 'fullPage': 'true'})
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
查看 HtmlWebShot
# pip install htmlwebshot
from htmlwebshot import WebShot
shot = WebShot()
shot.quality = 100
image = shot.create_pic(html="file.html")
如果您不希望您的项目像其他 Python 模块一样依赖于 wkhtmltopdf,我推荐 html2image。
from html2image import Html2Image
hti = Html2Image()
hti.screenshot(url='https://www.python.org', save_as='python_org.png')
from html2image import Html2Image
hti = Html2Image()
with open('./test.html') as f:
hti.screenshot(f.read(), save_as='out.png')