1
0
mirror of https://github.com/ihabunek/toot synced 2024-12-22 15:06:05 +01:00

Merge pull request #485 from ihabunek/indexed

Added support for indexed color image rendering; fixes #483
This commit is contained in:
Ivan Habunek 2024-06-08 09:32:09 +02:00 committed by GitHub
commit 6ea13db2a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 14 additions and 8 deletions

View File

@ -32,7 +32,7 @@ dependencies = [
# Required to display images in the TUI
images = [
"pillow>=9.5.0",
"term-image==0.7.0",
"term-image>=0.7.2",
]
# Required to display rich text in the TUI

View File

@ -22,7 +22,7 @@ try:
def can_render_pixels(image_format):
return image_format in _IMAGE_PIXEL_FORMATS
def get_base_image(image, image_format) -> BaseImage:
def get_base_image(image, image_format, colors) -> BaseImage:
# we don't autodetect kitty, iterm; we choose based on option switches
global _ImageCls
@ -36,6 +36,8 @@ try:
else BlockImage
)
_ImageCls.forced_support = True
if colors == 256 and not can_render_pixels(image_format):
_ImageCls.set_render_method("INDEXED")
return _ImageCls(image)
@ -79,7 +81,7 @@ try:
except Exception:
return None
def graphics_widget(img, image_format="block", corner_radius=0) -> urwid.Widget:
def graphics_widget(img, image_format="block", corner_radius=0, colors=16777216) -> urwid.Widget:
if not img:
return urwid.SolidFill(fill_char=" ")
@ -88,7 +90,7 @@ try:
else:
render_img = img
return UrwidImage(get_base_image(render_img, image_format), '<', upscale=True)
return UrwidImage(get_base_image(render_img, image_format, colors), '<', upscale=True)
# "<" means left-justify the image
except ImportError:

View File

@ -262,7 +262,8 @@ class Account(urwid.ListBox):
if image_support_enabled() and account['avatar'] and not account["avatar"].endswith("missing.png"):
img = load_image(account['avatar'])
aimg = urwid.BoxAdapter(
graphics_widget(img, image_format=self.options.image_format, corner_radius=10), 10)
graphics_widget(img, image_format=self.options.image_format, corner_radius=10,
colors=self.options.colors), 10)
else:
aimg = urwid.BoxAdapter(urwid.SolidFill(" "), 10)
@ -270,7 +271,8 @@ class Account(urwid.ListBox):
img = load_image(account['header'])
himg = (urwid.BoxAdapter(
graphics_widget(img, image_format=self.options.image_format, corner_radius=10), 10))
graphics_widget(img, image_format=self.options.image_format, corner_radius=10,
colors=self.options.colors), 10))
else:
himg = urwid.BoxAdapter(urwid.SolidFill(" "), 10)

View File

@ -339,7 +339,8 @@ class Timeline(urwid.Columns):
if img:
try:
status.placeholders[placeholder_index]._set_original_widget(
graphics_widget(img, image_format=self.tui.options.image_format, corner_radius=10))
graphics_widget(img, image_format=self.tui.options.image_format, corner_radius=10,
colors=self.tui.options.colors))
except IndexError:
# ignore IndexErrors.
@ -408,7 +409,8 @@ class StatusDetails(urwid.Pile):
pass
if img:
return (urwid.BoxAdapter(
graphics_widget(img, image_format=self.timeline.tui.options.image_format, corner_radius=10), rows))
graphics_widget(img, image_format=self.timeline.tui.options.image_format, corner_radius=10,
colors=self.timeline.tui.options.colors), rows))
else:
placeholder = urwid.BoxAdapter(urwid.SolidFill(fill_char=" "), rows)
self.status.placeholders.append(placeholder)