#!/usr/bin/python
#coding: utf-8
import PIL
import PIL.Image
import PIL.ImageDraw
import PIL.ImageFont
import sys
import codecs
import commands
reload(sys)
sys.setdefaultencoding('utf-8')


def GenerateTxt2Pic(files):
    """
        将文本文件转换成图片

    """
    ###以下几行是将windows格式的文件转换成linux可以cat的
    stat,res = commands.getstatusoutput('file %s' % files)
    if stat == 0:
        if 'ISO-8859' in res:
            stat,res = commands.getstatusoutput('enca -L zh_CN -x UTF-8 %s' % files)
            if stat == 0:
                pass
    ##字体大小
    sizeChar = 12 
    charColor = '#FFFFFF'
    #backGround ='#000000' ##333333
    backGround ='#333333'
    count = len(open(r"report.csv",'rU').readlines()) * sizeChar
    
    font = PIL.ImageFont.truetype('fonts/simsun.ttc', sizeChar)
    im = PIL.Image.new('RGB', (1865, count), backGround)
    draw = PIL.ImageDraw.Draw(im)
#    print(draw.textsize('Hello World!', font)) # (96, 10), 返回字符串将要占用的像素区域大小
    ii = ''
    
    with open('report.csv',"rb") as f:
        for i in f.readlines():
            ii = ii + u'%s' % i
    draw.multiline_text((10, 10), '%s' % ii , charColor, font)
#    im.show()
    im.save("%s.jpg" % files)

if __name__ == '__main__':
    finename = 'report.csv'
    GenerateTxt2Pic(finename)
        
