Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

output an ascii representation of images #1

Open
wants to merge 2 commits into from

2 participants

@billychasen

a little present :)

feel free to change anything or reject

@nanotone
Owner
  1. Does not use tabs for indentation like the rest of the file.

  2. Does not make full use of ncurses to animate gifs.

@billychasen
  1. Oh, my superior editor failed to distinguish the difference and instead just let me code.

  2. It does, the framerate is just 1,284,991,211 fps

@nanotone
Owner
  1. It's not a question of editors; you should prefer running python with -tt (or rather, just alias it) which distinguishes between proper and improper mixing of tabs and spaces. \end{short-whitespace-rant}

\begin{long-whitespace-rant}

@billychasen

okay, it's now been nanospaced

I prefer to just pretend tabs are deprecated, along with line-feed and scroll lock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 18, 2012
  1. output an ascii representation of images

    billy authored
Commits on Apr 19, 2012
  1. nanospaced

    billy authored
This page is out of date. Refresh to see the latest.
Showing with 32 additions and 0 deletions.
  1. +32 −0 pyro.py
View
32 pyro.py
@@ -90,6 +90,33 @@ def notifyGrowl(username, body):
if notifier: notifier.notify("msg", username, body)
+################# IMAGE HANDLING #################
+import re
+from subprocess import Popen, PIPE, STDOUT
+
+def isImage(text):
+ if re.match(r'^http.*\.(png|gif|jpg)$', str(text)):
+ return True
+ return False
+
+def handleImage(url):
+ # requires convert, jp2a
+ # just catch all errors, if something went wrong just show url
+ try:
+ u = urllib2.urlopen(url).read()
+ if u:
+ p = Popen('convert - jpg:-'.split(' '), stdout=PIPE, stdin=PIPE, stderr=STDOUT)
+ stdout = p.communicate(input=u)[0]
+ # higher forced width gives more "resolution", easier to grok image
+ p = Popen('jp2a --width=100 -'.split(' '), stdout=PIPE, stdin=PIPE, stderr=STDOUT)
+ msg = p.communicate(input=stdout)[0]
+ except Exception:
+ return url
+ else:
+ return msg + '\nsource: %s' % url
+##################################################
+
+
def handleMsg(msg, notify=True): # expects a 37s json msg
assert(type(msg is dict) and 'type' in msg)
msgType = msg['type']
@@ -107,6 +134,8 @@ def handleMsg(msg, notify=True): # expects a 37s json msg
username = users.get(userId, "Anonymous")
line += "<%s>" % username
if msgType == "TextMessage":
+ if isImage(msg['body']):
+ msg['body'] = handleImage(msg['body'])
indent = len(line) + 2
line += ": %s" % msg['body']
if notify and (ECHO_MESSAGES or msg.get('user_id') != selfUserId):
@@ -131,9 +160,12 @@ def handleMsg(msg, notify=True): # expects a 37s json msg
if notify:
notifyGrowl(username, msg['body'])
else:
+ if isImage(msg):
+ msg = handleImage(msg)
line += ": %r" % msg
chat.log(line, indent)
+
################################################################################
import time
Something went wrong with that request. Please try again.