summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames E. Blair <corvus@gnu.org>2016-11-20 12:27:59 -0800
committerJames E. Blair <corvus@gnu.org>2016-11-20 12:27:59 -0800
commit3e4efbb2e0eb1ee33b0157468682b6aceffbadfe (patch)
treec176566b1e666260f969763e6724fecc90f6cd53
parent4c3c4ff03183b914750e889202435bc558933898 (diff)
Fail gracefully if jp2a is not installed
-rw-r--r--presentty/image.py47
1 files changed, 33 insertions, 14 deletions
diff --git a/presentty/image.py b/presentty/image.py
index 2dbd9ad..939536f 100644
--- a/presentty/image.py
+++ b/presentty/image.py
@@ -43,6 +43,9 @@ class ANSIImage(urwid.Widget):
43 scale = 1 43 scale = 1
44 self.scale = scale 44 self.scale = scale
45 self.background = background or 'black' 45 self.background = background or 'black'
46 self._prime = True
47 self.render((3,1))
48 self._prime = False
46 49
47 def _loadImage(self): 50 def _loadImage(self):
48 image = PIL.Image.open(self.uri) 51 image = PIL.Image.open(self.uri)
@@ -83,6 +86,12 @@ class ANSIImage(urwid.Widget):
83 r = self.pack(size) 86 r = self.pack(size)
84 return r[1] 87 return r[1]
85 88
89 def _blank(self, width, height):
90 ret = []
91 for y in range(height):
92 ret.append("<span style='color:#000000; background-color:#000000;'>%s</span>" % ('.'*width))
93 return '<br/>'.join(ret)
94
86 SPAN_RE = re.compile(r"<span style='color:#(......); background-color:#(......);'>(.*)") 95 SPAN_RE = re.compile(r"<span style='color:#(......); background-color:#(......);'>(.*)")
87 def render(self, size, focus=False): 96 def render(self, size, focus=False):
88 spanre = self.SPAN_RE 97 spanre = self.SPAN_RE
@@ -99,20 +108,30 @@ class ANSIImage(urwid.Widget):
99 right_pad = total_width - width - left_pad 108 right_pad = total_width - width - left_pad
100 padding_attr = urwid.AttrSpec(self.background, self.background) 109 padding_attr = urwid.AttrSpec(self.background, self.background)
101 110
102 jp2a = subprocess.Popen(['jp2a', '--colors', '--fill', 111 try:
103 '--width=%s' % width, 112 jp2a = subprocess.Popen(['jp2a', '--colors', '--fill',
104 '--height=%s' % height, 113 '--width=%s' % width,
105 '--html-raw', '-'], 114 '--height=%s' % height,
106 stdin=subprocess.PIPE, 115 '--html-raw', '-'],
107 stdout=subprocess.PIPE, 116 stdin=subprocess.PIPE,
108 stderr=subprocess.PIPE) 117 stdout=subprocess.PIPE,
109 image = self._loadImage() 118 stderr=subprocess.PIPE)
110 image = image.convert('RGBA') 119 except OSError, e:
111 image.save(jp2a.stdin, 'JPEG') 120 if self._prime:
112 jp2a.stdin.close() 121 if e.errno == 2:
113 data = jp2a.stdout.read() 122 print("ERROR: jp2a is used but is not installed.")
114 jp2a.stderr.read() 123 else:
115 jp2a.wait() 124 print("ERROR: unable to run jp2a: %s" % e)
125 raw_input("Press ENTER to continue.")
126 data = self._blank(width, height)
127 else:
128 image = self._loadImage()
129 image = image.convert('RGBA')
130 image.save(jp2a.stdin, 'JPEG')
131 jp2a.stdin.close()
132 data = jp2a.stdout.read()
133 jp2a.stderr.read()
134 jp2a.wait()
116 135
117 line_list = [] 136 line_list = []
118 attr_list = [] 137 attr_list = []