如何批量下载指定分辨率的Noto Color Emoji透明背景PNG
批量获取Noto Color Emoji网页同款透明背景PNG方案
方法1:爬取网页CDN资源(快速批量)
网页中展示的表情是谷歌CDN托管的高清PNG,可通过以下步骤批量获取:
- 解析资源URL规则:打开目标页面开发者工具(F12),过滤
png类型资源,可发现表情URL格式为https://fonts.gstatic.com/s/notocoloremoji/v[版本号]/[表情码点文件名].png。其中版本号可从当前页面加载的资源中获取(如v124),表情码点文件名由Unicode码点拼接而成(例如😀对应emoji_u1f600.png,组合表情👨👩👧对应emoji_u1f468_200d_1f469_200d_1f467.png)。 - 批量生成下载链接:整理需要的表情Unicode码点,按上述格式批量生成URL。
- 批量下载:使用Python
requests库或命令行工具(wget/curl)批量下载。示例代码:
import requests import os # 替换为你需要的表情码点文件名前缀(去掉emoji_) emoji_codes = ["u1f600", "u1f601", "u1f468_200d_1f469_200d_1f467"] save_dir = "noto_emojis" os.makedirs(save_dir, exist_ok=True) # 替换为当前页面使用的版本号 base_url = "https://fonts.gstatic.com/s/notocoloremoji/v124/emoji_{}.png" for code in emoji_codes: url = base_url.format(code) resp = requests.get(url, stream=True) if resp.status_code == 200: save_path = os.path.join(save_dir, f"{code}.png") with open(save_path, "wb") as f: for chunk in resp.iter_content(1024): f.write(chunk) print(f"已保存:{save_path}") else: print(f"下载失败:{url}")
方法2:基于官方字体生成(样式完全匹配)
网页表情由Noto Color Emoji字体渲染,使用官方字体文件可生成完全一致的表情,且可自定义分辨率:
- 下载官方字体:获取Noto Color Emoji的TTF字体文件(NotoColorEmoji.ttf)。
- 用fonttools提取并转换:借助
fonttools提取字体中的彩色字形,再转为指定分辨率的透明背景PNG。需预先安装依赖:pip install fonttools svgutils cairosvg。示例代码:
from fontTools.ttLib import TTFont from fontTools.pens.svgPathPen import SVGPathPen from fontTools.pens.boundsPen import BoundsPen import svgutils.transform as sg import cairosvg import os font_path = "NotoColorEmoji.ttf" save_dir = "noto_emojis_custom_size" os.makedirs(save_dir, exist_ok=True) target_resolution = 256 # 自定义分辨率 font = TTFont(font_path) cmap = font.getBestCmap() glyph_set = font.getGlyphSet() # 替换为你需要的表情Unicode码点(十进制) emoji_unicodes = [0x1F600, 0x1F601, 0x1F468] for uni in emoji_unicodes: if uni not in cmap: print(f"字体中无该表情:U+{hex(uni)[2:].upper()}") continue glyph_name = cmap[uni] glyph = glyph_set[glyph_name] # 获取字形边界 bounds_pen = BoundsPen(glyph_set) glyph.draw(bounds_pen) x_min, y_min, x_max, y_max = bounds_pen.bounds max_dim = max(x_max - x_min, y_max - y_min) # 生成SVG路径 pen = SVGPathPen(glyph_set) glyph.draw(pen) svg_path = pen.getCommands() # 创建SVG并缩放至目标分辨率 svg = sg.SVGFigure(f"{target_resolution}px", f"{target_resolution}px") group = sg.GroupElement() group.add(sg.PathElement(d=svg_path, fill="currentColor")) scale = target_resolution / max_dim group.scale(scale, scale) # 居中对齐 group.translate(-x_min*scale, (target_resolution - (y_max - y_min)*scale) + y_min*scale) svg.append(group) # 保存为PNG(透明背景) svg_temp = os.path.join(save_dir, f"temp_{uni}.svg") png_path = os.path.join(save_dir, f"U+{hex(uni)[2:].upper()}.png") svg.save(svg_temp) cairosvg.svg2png(url=svg_temp, write_to=png_path, output_width=target_resolution, output_height=target_resolution, background_color="transparent") os.remove(svg_temp) print(f"已生成:{png_path}")
关键提示
- 方法1需确保版本号与当前网页一致,否则可能获取到旧版表情。
- 方法2支持任意分辨率调整,且生成的表情与网页样式完全一致,适合对样式精度要求高的场景。
- 批量处理时,可将需要的表情码点整理为列表,实现自动化遍历。
内容的提问来源于stack exchange,提问作者Emanuel
相关产品推荐
相关产品推荐

