From 73277766573f00d50ca6a857a18277bc96ad00d2 Mon Sep 17 00:00:00 2001 From: Jaewook Lee <11328376+jaewooklee93@users.noreply.github.com> Date: Thu, 18 Jul 2024 20:30:49 +0900 Subject: [PATCH] new display image on terminal manual --- image-on-terminal/README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 image-on-terminal/README.md diff --git a/image-on-terminal/README.md b/image-on-terminal/README.md new file mode 100644 index 0000000..1f8bf64 --- /dev/null +++ b/image-on-terminal/README.md @@ -0,0 +1,28 @@ +## Display Image on terminal directly + +```sh +pip install rich +``` + +```python +def show(image, width=80): + w, h = image.size + aspect = h / (2 * w) + height = int(width * aspect) + image = image.resize((width, height)) + + import rich.text, rich.console + text = rich.text.Text() + for y in range(height): + for x in range(width): + r, g, b = image.getpixel((x, y)) + text.append("█", style=f"rgb({r},{g},{b})") + text.append("\n") + + return rich.console.Console(width=width).print(text) + +image_path = 'your_image.png' +from PIL import Image +image = Image.open(image_path) +show(image, 160) +``` \ No newline at end of file