Skip to content

Commit

Permalink
Color improvement at high-contrast edges. Drastic speed improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnhansen committed Oct 28, 2017
1 parent c17b0f9 commit 80ab1cf
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 147 deletions.
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def readme():
return f.read()

setup(name='vimg',
version='0.4.1',
version='0.4.2',
description='An image viewer for the command line',
long_description=readme(),
keywords='cli terminal console image picture graphics viewer preview',
Expand All @@ -28,7 +28,6 @@ def readme():
packages=find_packages(),
install_requires=[
'numpy',
'scipy',
'opencv-python',
],
entry_points={
Expand Down
63 changes: 41 additions & 22 deletions vimg/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import curses
from threading import Timer

FLAG_TTY = sys.stdout.isatty()
PY2 = sys.version_info < (3,0)

class GUI:
Expand All @@ -20,7 +21,7 @@ def __init__(self, image, mode='ascii'):
"""
self.image = image
self.mode = mode
self.WINDOW = curses.initscr()
self.start()

self.OFFSET_Y = (1,1)
self.OFFSET_X = (0,0)
Expand All @@ -34,15 +35,6 @@ def __init__(self, image, mode='ascii'):
self.DEFAULT_BG_COLOR = 0
self.ZOOM_DELAY = 0.2

curses.noecho()
curses.cbreak()
curses.curs_set(0)
self.WINDOW.keypad(1)
curses.mousemask(1)

curses.start_color()
curses.use_default_colors()

self._fg_color = self.DEFAULT_FG_COLOR
self._bg_color = self.DEFAULT_BG_COLOR
self._cursor_x = 0
Expand All @@ -52,7 +44,36 @@ def __init__(self, image, mode='ascii'):

self._stdout = ''

self.clear()
if FLAG_TTY:
self.clear()

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
self.quit()

def start(self):
""" Start curses application. """
self.WINDOW = curses.initscr()
if FLAG_TTY:
curses.start_color()
curses.use_default_colors()
curses.noecho()
curses.cbreak()
curses.curs_set(0)
self.WINDOW.keypad(1)
curses.mousemask(1)

def quit(self):
""" End curses application. """
# if FLAG_TTY:
self._cancel_render()
curses.nocbreak()
curses.echo()
curses.curs_set(1)
curses.mousemask(0)
curses.endwin()

@property
def term_height(self):
Expand Down Expand Up @@ -152,7 +173,7 @@ def _print_statusline(self):
curses.COLOR_WHITE, 0
)
statusline_bottom = self._print(
('{:%s}'%self.term_width).format(' c:color e:edges r:refresh q:quit +/-:zoom hjkl:move'),
('{:%s}'%self.term_width).format(' c:color e:edges r:refresh q:quit +/-:zoom hjkl:move 0:reset'),
0,self.term_height-1,
curses.COLOR_WHITE, 0
)
Expand Down Expand Up @@ -204,21 +225,14 @@ def save(self):
with open('stdout.log','w') as f:
f.write(self._stdout + '\n')

def quit(self):
""" End curses application. """
curses.nocbreak()
curses.echo()
curses.curs_set(1)
curses.mousemask(0)
curses.endwin()

def render(self, refresh=True):
""" Render the image and print to screen. """
im = self.image.render(shape=self.shape, mode=self.mode)
xpos = int((self.width - im.shape[1])/2)
ypos = int((self.height - im.shape[0])/2)
self.clear_buffer()
self.clear()
if FLAG_TTY:
self.clear()

for y,line in enumerate(im):
for x,vec in enumerate(line):
Expand All @@ -227,7 +241,7 @@ def render(self, refresh=True):
fg = int(fg)
self.printat(char, x+xpos+self.OFFSET_X[0], y+ypos+self.OFFSET_Y[0], color=fg, bg=bg)

if refresh:
if refresh and FLAG_TTY:
self.refresh()

def getch(self):
Expand All @@ -244,6 +258,10 @@ def wrapper():
self._render_timer = Timer(seconds, wrapper)
self._render_timer.start()

def _cancel_render(self):
if self._render_timer is not None:
self._render_timer.cancel()

def _screen2pixel(self,x,y):
""" Not implemented. """
pass
Expand Down Expand Up @@ -304,6 +322,7 @@ def main(self):
self.image.move_right()
self._schedule_render(self.ZOOM_DELAY)
elif c == ord('q'):
self._cancel_render()
break

except Exception as e:
Expand Down
Loading

0 comments on commit 80ab1cf

Please sign in to comment.