Created
May 13, 2025 12:03
-
-
Save 7shi/91c2940e1ce5c5463ff4173e7d6a23bd to your computer and use it in GitHub Desktop.
[py] Display formulas with Sixel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example usage | |
# uv run equ.py "\int_{0}^{\pi} \sin(x) dx = 2" -H 64 | |
def main(): | |
import argparse | |
parser = argparse.ArgumentParser(description='LaTeX数式を画像化してSIXEL表示') | |
parser.add_argument('equation', type=str, help='LaTeX形式の数式($記号は不要)') | |
parser.add_argument('--fontsize', type=int, default=12, help='フォントサイズ') | |
parser.add_argument('--dpi', type=int, default=300, help='画像解像度(DPI)') | |
parser.add_argument('--color', type=str, default='white', help='文字色') | |
parser.add_argument('--facecolor', type=str, default='black', help='背景色') | |
parser.add_argument('--transparent', action='store_true', help='背景を透明にする') | |
parser.add_argument('--width', '-W', type=int, default=None, help='表示幅(px)') | |
parser.add_argument('--height', '-H', type=int, default=None, help='表示高さ(px)') | |
args = parser.parse_args() | |
with formula_to_image( | |
args.equation, fontsize=args.fontsize, dpi=args.dpi, | |
color=args.color, facecolor=args.facecolor, transparent=args.transparent) as buf: | |
display_image_with_correct_aspect_ratio(buf, target_width=args.width, target_height=args.height) | |
import sys | |
from io import BytesIO | |
from PIL import Image | |
from matplotlib.figure import Figure | |
from sixel import converter | |
def formula_to_image(formula, fontsize=12, dpi=300, format='png', color=None, facecolor=None, transparent=True): | |
""" | |
LaTeX数式を画像ファイルに変換する | |
Parameters: | |
formula (str): LaTeX形式の数式文字列($記号は不要) | |
filename (str): 出力ファイル名 | |
fontsize (int): フォントサイズ | |
dpi (int): 解像度(DPI) | |
format (str): 画像フォーマット('png', 'pdf', 'svg'など) | |
""" | |
# 黒背景・白文字の透明な背景の図を作成 | |
fig = Figure(figsize=(1, 1), dpi=dpi, facecolor=facecolor) | |
fig.text(0.5, 0.5, f'${formula}$', | |
fontsize=fontsize, color=color, | |
ha='center', va='center') | |
# 図を保存(余白なし、黒背景) | |
buf = BytesIO() | |
fig.savefig(buf, format=format, bbox_inches='tight', | |
pad_inches=0.1, transparent=transparent, facecolor=facecolor) | |
buf.seek(0) | |
return buf | |
def display_image_with_correct_aspect_ratio(image_buffer, target_width=None, target_height=None): | |
""" | |
画像をバイトとして読み込み、アスペクト比を維持しながら表示する | |
Args: | |
image_path: 画像ファイルパス | |
target_height: 表示する高さ(ピクセル) | |
""" | |
# 画像サイズを取得するためにImageに変換 | |
with Image.open(image_buffer) as img: | |
w, h = img.size | |
# バッファの位置を先頭に戻す | |
image_buffer.seek(0) | |
# 片側だけ指定されていれば、アスペクト比を計算して新しい幅を決定 | |
if target_width and target_height: | |
w, h = target_width, target_height | |
elif target_width: | |
aspect_ratio = h / w | |
w, h = target_width, int(target_width * aspect_ratio) | |
elif target_height: | |
aspect_ratio = w / h | |
w, h = int(target_height * aspect_ratio), target_height | |
# バイトデータをSixelConverterに渡し、w, hパラメータでリサイズ | |
sixel_conv = converter.SixelConverter(image_buffer, w=w, h=h) | |
# 標準出力に書き込む | |
sixel_conv.write(sys.stdout) | |
if __name__ == "__main__": | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[project] | |
name = "sixel-equation" | |
version = "0.1.0" | |
description = "Add your description here" | |
requires-python = ">=3.12" | |
dependencies = [ | |
"matplotlib>=3.10.3", | |
"pillow>=11.2.1", | |
"sixel", | |
] | |
[tool.uv.sources] | |
sixel = { git = "https://github.com/sbamboo/python-sixel.git" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment