Skip to content

Instantly share code, notes, and snippets.

@paultag
Created December 4, 2011 15:49
Show Gist options
  • Save paultag/1430501 to your computer and use it in GitHub Desktop.
Save paultag/1430501 to your computer and use it in GitHub Desktop.
Nyangifer
#!/usr/bin/env python
import Image
import time
import socket
import struct
import sys
import colortrans
def printPix( color ):
return "%s[0m%s[48;5;%sm " % ( chr(0x1B), chr(0x1B), color )
def convertToXterm( r, g, b ):
RGBint = 0
RGBint += b
RGBint += g << 8
RGBint += r << 16
color = "#%0.6X" % RGBint
short, full = colortrans.rgb2short( color )
return short
def loadsImage( im ):
outputBufferLoop = ""
screens = []
width, height = im.size
palette = im.palette.palette
colortable = [(ord(palette[i]),ord(palette[i + 1]),ord(palette[i + 2])) for i in xrange(0, len(palette), 3)]
try:
while 1:
im.seek(im.tell()+1)
pix = im.load()
for y in range(0, height):
lastPix = -1
for x in range(0, width):
colls = pix[x,y]
if lastPix != colls:
r, g, b = colortable[colls]
if colls == 0 or r == 255 and g == 0 and b == 255:
color = convertToXterm(0,0,0)
else:
color = convertToXterm(r,g,b)
outputBufferLoop += printPix( color )
else:
outputBufferLoop += " "
lastPix = pix[x, y]
outputBufferLoop += "%s[0m\n" % ( chr(0x1B) )
screens.append( outputBufferLoop )
outputBufferLoop = ""
except EOFError:
pass # end of sequence
return screens
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment