1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows Terminal で Python から Sixel

1
Last updated at Posted at 2025-06-08

Windows Terminal では Sixel がサポートされています。Python から Sixel で画像を表示します。

python-sixel

Python で Sixel を扱うには python-sixel というライブラリがよく紹介されますが、Windows 非対応の termios に依存しているため、WSL でしか動きません。

Windows に対応したフォークを利用します。

pip install git+https://github.com/sbamboo/python-sixel.git

サンプル

test.png を表示する例です。

import sys, sixel
sixel.converter.SixelConverter("test.png").write(sys.stdout)

これを拡張して、画像ファイルの指定やアスペクト比を維持したリサイズを実装しました。

matplotlib で描画した数式を表示する例です。(グラフも同様にできます)

formula.py
formula = r"\int_{0}^{\pi} \sin(x) dx = 2"

import sys, io, matplotlib.figure, sixel
fig = matplotlib.figure.Figure(figsize=(1, 1), dpi=120)
fig.text(0, 0, f"${formula}$", fontsize=12, color="white")
with io.BytesIO() as buf:
    fig.savefig(buf, format="png", bbox_inches="tight", facecolor="black")
    sixel.converter.SixelConverter(buf).write(sys.stdout)

実行例:
image.png

画像生成と組み合わせた例です。ガチャをするときに画像ファイルを別途確認しなくて済むのが便利です。

pizza.py
model  = "Linaqruf/anything-v3-1"
prompt = "girl eating pizza"
output = "pizza.png"

import sys, diffusers, torch, sixel
pipe = diffusers.StableDiffusionPipeline.from_pretrained(model)
pipe = pipe.to("cuda")
result = pipe(prompt=prompt, width=256, height=256, num_inference_steps=10)
result.images[0].save(output)
sixel.converter.SixelConverter(output).write(sys.stdout)

実行例:
image.png

絵柄は毎回変わります。

関連記事

diffusers の使い方については以下の記事を参照してください。

この記事は CPU で動かす前提のため、GPU で動かすには pipe = pipe.to("cuda") を追加する必要があります。

Gemini による画像生成ガチャの記事です。Sixel を利用しています。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?