Instagram 卡片

正方形新闻卡片:底图照片、红色标签、衬线体主标题和新闻来源信息行。
安装 uv 并克隆代码仓库即可运行此示例:
git clone https://github.com/sjquant/quickthumb
cd quickthumb
uv run python examples/instagram_news_card.py
该脚本将写入 examples/instagram_news_card.png,并覆盖同名旧文件。
构成方式
- 标签采用带有
Background效果的文本。Background(color="#CC0000", padding=(11, 22))围绕“BREAKING NEWS”绘制红底边框,使其随文本长度自适应拉伸,而无需手动调整独立形状的尺寸。 - 标题使用有别于正文的独特字体。
font="NotoSerif"从内置字体目录加载衬线体;其余图层均使用默认的 Roboto 字体。 - 红色装饰条是一个 9px 宽的矩形。 作为一个独立的形状,其位置和高度(176px)经过手动调整以精准匹配两行标题文字。
- 新闻来源信息为单个文本图层。 通过两个
TextPart分别赋予“WORLD NEWS”与日期不同的颜色和字重。 - 大多数坐标使用百分比定位。 例如
("8%", "92%"),以便随画布尺寸自适应缩放。红色装饰条属于特例:使用像素坐标以便与标题文字精确对齐。
代码
"""
Instagram News Card Example
Creates a breaking news-style Instagram card (1080x1080) with:
- Fire background image (cover fit, darkened)
- Dark gradient overlay for text legibility
- "BREAKING NEWS" badge
- Bold headline with stroke
- Source and timestamp at the bottom
"""
import os
from quickthumb import Canvas, Filter, FitMode, LinearGradient, TextPart
from quickthumb.models import Background, Shadow, Stroke
FILE_DIR = os.path.dirname(__file__)
ASSETS_DIR = os.path.join(FILE_DIR, "..", "assets")
os.environ["QUICKTHUMB_FONT_DIR"] = os.path.join(ASSETS_DIR, "fonts")
os.environ["QUICKTHUMB_DEFAULT_FONT"] = "Roboto"
SIZE = 1080
(
Canvas(SIZE, SIZE)
# 1. Background: fire image, cropped to fill the square
.background(
image=os.path.join(ASSETS_DIR, "images", "tobias-rademacher-wnF27F85ZKw-unsplash.jpg"),
fit=FitMode.COVER,
effects=[Filter(brightness=0.9, contrast=1.1, saturation=0.9)],
)
# 2. Dark gradient overlay — bottom two-thirds darkened for text legibility
.background(
gradient=LinearGradient(
angle=90,
stops=[("#00000000", 0.0), ("#000000B8", 0.42), ("#000000F2", 1.0)],
),
)
.text(
content="LIVE / 20:42 UTC",
size=19,
color="#FFFFFFAA",
weight=700,
letter_spacing=3,
position=("8%", "6%"),
align=("left", "top"),
)
.shape(
shape="rectangle",
position=("8%", "11%"),
width=908,
height=2,
color="#FFFFFF44",
)
# 3. "BREAKING NEWS" badge near the top
.text(
content="BREAKING NEWS",
size=28,
color="#FFFFFF",
weight=900,
letter_spacing=4,
position=("8%", "15%"),
align=("left", "top"),
effects=[
Background(color="#CC0000", padding=(11, 22), border_radius=2),
],
)
.shape(
shape="rectangle",
position=("8%", 464),
width=9,
height=176,
color="#E11D2E",
border_radius=4,
)
# 4. Main headline — large, bold, white with shadow
.text(
content="Wildfires Spread\nAcross 18,000 Acres",
font="NotoSerif",
size=84,
color="#FFFFFF",
weight=900,
position=("11%", "52%"),
align=("left", "middle"),
line_height=1.12,
effects=[
Stroke(width=1, color="#000000"),
Shadow(offset_x=0, offset_y=6, color="#000000AA", blur_radius=10),
],
)
# 5. Source and timestamp row
.text(
content=[
TextPart(text="WORLD NEWS ", color="#FF4444", weight=700),
TextPart(text="· Feb 20, 2026", color="#AAAAAA", weight=400),
],
size=30,
position=("8%", "92%"),
align=("left", "top"),
)
# Render
.render(os.path.join(FILE_DIR, "instagram_news_card.png"))
)
print(f"✓ Instagram news card created: {os.path.join(FILE_DIR, 'instagram_news_card.png')}")