Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for indexed color image rendering; fixes #483 #485

Merged
merged 1 commit into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions toot/tui/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def image_support_enabled():
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
Expand All @@ -36,6 +36,8 @@ def get_base_image(image, image_format) -> BaseImage:
else BlockImage
)
_ImageCls.forced_support = True
if colors == 256 and not can_render_pixels(image_format):
_ImageCls.set_render_method("INDEXED")

return _ImageCls(image)

Expand Down Expand Up @@ -79,7 +81,7 @@ def load_image(url):
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=" ")

Expand All @@ -88,7 +90,7 @@ def graphics_widget(img, image_format="block", corner_radius=0) -> urwid.Widget:
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:
Expand Down
6 changes: 4 additions & 2 deletions toot/tui/overlays.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,17 @@ def account_header(self, account):
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)

if image_support_enabled() and account['header'] and not account["header"].endswith("missing.png"):
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)

Expand Down
6 changes: 4 additions & 2 deletions toot/tui/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ def update_status_image(self, status, path, placeholder_index):
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.
Expand Down Expand Up @@ -408,7 +409,8 @@ def image_widget(self, path, rows=None, aspect=None) -> urwid.Widget:
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)
Expand Down