Skip to content

Instantly share code, notes, and snippets.

@giginet
Created December 16, 2011 07:00
Show Gist options
  • Select an option

  • Save giginet/1484878 to your computer and use it in GitHub Desktop.

Select an option

Save giginet/1484878 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
__author__ = 'giginet'
__version__ = '1.0.0'
__date__ = '2011/10/10'
import os
import sys
from cStringIO import StringIO
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import ImageFormatter
from pygments.styles import STYLE_MAP
def create_thumbnail(path, syntax=None):
if not syntax:
syntax = 'text'
try:
lexer = get_lexer_by_name(syntax)
except:
lexer = get_lexer_by_name('text')
for style in STYLE_MAP.keys():
formatter = ImageFormatter(
style=style,
font_size=12,
line_numbers=False
)
from PIL import Image
file = open(path, 'rb')
body = file.read()
data = highlight(body, lexer, formatter)
thumbnail = Image.open(StringIO(data))
thumbnail.save('%s.png' % style, 'png')
if __name__ == '__main__':
args = sys.argv
if len(args) == 0:
print "Second argument must be source code path."
else:
path = args[1]
ext = os.path.splitext(path)[1][1:]
create_thumbnail(path, syntax=ext)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment