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

Improve Image Support #484

Merged
merged 3 commits into from
Jun 5, 2024
Merged
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
26 changes: 18 additions & 8 deletions toot/tui/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,34 @@
from term_image import disable_queries # prevent phantom keystrokes
from PIL import Image, ImageDraw

_IMAGE_PIXEL_FORMATS = frozenset({'kitty', 'iterm'})
_ImageCls = None

TuiScreen = UrwidImageScreen
disable_queries()

def image_support_enabled():
return True

def can_render_pixels(image_format):
return image_format in ['kitty', 'iterm']
return image_format in _IMAGE_PIXEL_FORMATS

def get_base_image(image, image_format) -> BaseImage:
# we don't autodetect kitty, iterm; we choose based on option switches
BaseImage.forced_support = True
if image_format == 'kitty':
return KittyImage(image)
elif image_format == 'iterm':
return ITerm2Image(image)
else:
return BlockImage(image)

global _ImageCls

if not _ImageCls:
_ImageCls = (
KittyImage
if image_format == 'kitty'
else ITerm2Image
if image_format == 'iterm'
else BlockImage
)
_ImageCls.forced_support = True

return _ImageCls(image)

def resize_image(basewidth: int, baseheight: int, img: Image.Image) -> Image.Image:
if baseheight and not basewidth:
Expand Down