Skip to content

Commit 141056f

Browse files
committed
perf: increase scale factor
1 parent e9979c2 commit 141056f

4 files changed

Lines changed: 200 additions & 196 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[project]
2-
name = "campuxutility"
2+
name = "astrbot-t2i-service"
33
version = "0.1.0"
4-
description = "Add your description here"
4+
description = "A text-to-image rendering service using FastAPI and Playwright."
55
readme = "README.md"
66
requires-python = ">=3.11"
77
dependencies = [

src/api.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,21 @@ async def text2img(request: GenerateRequest):
9595
if request.options
9696
else ScreenshotOptions(
9797
timeout=None,
98-
type=None,
98+
type="png",
9999
quality=None,
100100
omit_background=None,
101101
full_page=True,
102102
clip=None,
103103
animations=None,
104104
caret=None,
105-
scale=None,
105+
scale="device",
106106
)
107107
)
108108

109109
pic = await render.html2pic(abs_path, options)
110110

111+
media_type = "image/png" if pic.endswith(".png") else "image/jpeg"
112+
111113
if is_json_return:
112114
return JSONResponse(
113115
content={
@@ -117,7 +119,7 @@ async def text2img(request: GenerateRequest):
117119
},
118120
)
119121
else:
120-
return fastapi.responses.FileResponse(pic, media_type="image/jpeg")
122+
return fastapi.responses.FileResponse(pic, media_type=media_type)
121123

122124

123125
if __name__ == "__main__":

src/render.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
import logging
2-
31
from .util import generate_data_path
42
from playwright.async_api import async_playwright
53
from jinja2.sandbox import SandboxedEnvironment
64
from pydantic import BaseModel
75
from typing_extensions import TypedDict
86
from typing import Literal
7+
from loguru import logger
8+
99

1010
class FloatRect(TypedDict):
1111
x: float
1212
y: float
1313
width: float
1414
height: float
1515

16+
1617
class ScreenshotOptions(BaseModel):
1718
"""Playwright 截图参数
1819
@@ -36,15 +37,15 @@ class ScreenshotOptions(BaseModel):
3637
@author: Redlnn(https://github.com/GraiaCommunity/graiax-text2img-playwright)
3738
"""
3839

39-
timeout: float | None
40-
type: Literal["jpeg", "png", None]
41-
quality: int | None
42-
omit_background: bool | None
43-
full_page: bool | None
44-
clip: FloatRect | None
45-
animations: Literal["allow", "disabled", None]
46-
caret: Literal["hide", "initial", None]
47-
scale: Literal["css", "device", None]
40+
timeout: float | None = None
41+
type: Literal["jpeg", "png", None] = None
42+
quality: int | None = None
43+
omit_background: bool | None = None
44+
full_page: bool | None = True
45+
clip: FloatRect | None = None
46+
animations: Literal["allow", "disabled", None] = None
47+
caret: Literal["hide", "initial", None] = None
48+
scale: Literal["css", "device", None] = None
4849

4950

5051
class Text2ImgRender:
@@ -72,16 +73,17 @@ async def html2pic(
7273
if self.context is None:
7374
self.playwright = await async_playwright().start()
7475
self.browser = await self.playwright.chromium.launch()
75-
self.context = await self.browser.new_context()
76-
result_path, _ = generate_data_path(suffix="jpeg", namespace="rendered")
77-
78-
logging.info(f"Rendering {html_file_path}")
76+
self.context = await self.browser.new_context(
77+
device_scale_factor=1.8,
78+
)
7979

80+
suffix = screenshot_options.type if screenshot_options.type else "png"
81+
result_path, _ = generate_data_path(suffix=suffix, namespace="rendered")
8082
page = await self.context.new_page()
8183
await page.goto(f"file://{html_file_path}")
8284
await page.screenshot(path=result_path, **screenshot_options.model_dump())
8385
await page.close()
8486

85-
logging.info(f"Rendered {html_file_path} to {result_path}")
87+
logger.info(f"Rendered {html_file_path} to {result_path}")
8688

8789
return result_path

0 commit comments

Comments
 (0)