On Tuesday, 7 April 2015 15:43:44 UTC-7, kai.p...@gmail.com wrote: > I just wrote this bit (coming from Pascal) and am wondering how seasoned > Python programmers would have done the same? Anything terribly non-python? > > As always, thanks for all input. > > K > > > > """ > Creates a PNG image from EPD file > """ > > import os, sys > from PIL import Image, ImageFont, ImageDraw > > # > ----------------------------------------------------------------------------- > def RenderByte(draw, byte, x, y): > > blist = list(bin(byte).lstrip('0b')) # turn byte into list with 8 > elements, > c = 0 # each representing one bit > for bit in blist: > if bit: > draw.point((x + c, y), fcolor) > > c += 1 > return > > # > ----------------------------------------------------------------------------- > def EPD_2_Image(edpfilename, imagefilename): > > # get out of here if EPD file not present > if not os.path.isfile(epdfilename): > print 'file not found: ' + edpfilename > return > > # is this a valid EPD file? > filesize = os.path.getsize(epdfilename) > if (((xdim / 8) * ydim) + header) <> filesize: > print 'incorrect file size: ' + edpfilename > return > > # blow old destination file away > if os.path.isfile(imagefilename): > print 'deleting old dest. file: ' + imagefilename > os.remove(imagefilename) > > print 'processing...' > > # set up PIL objects > img = Image.new('1', (xdim, ydim), bcolor) # '1' = Bi-tonal image > draw = ImageDraw.Draw(img) > > # read entire EPD file into byte array (without the header) > content = bytearray(open(epdfilename, 'rb').read())[16:] > > # image coord origin at top/left according to PIL documentation > pos = 0 > for y in range(ydim): > x = 0 > for byte in range(xdim / 8): # 8 pixels 'stuffed' into one byte > RenderByte(draw, content[pos], x, y) > pos += 1 > x += 8 > > img.save(imagefilename) # format is inferred from given extension > print 'done.' > > return > # > ----------------------------------------------------------------------------- > > xdim = 1024 > ydim = 1280 > header = 16 > black = 0 > white = 1 > bcolor = black > fcolor = white > > epdfilename = 'c:\\temp\\drawtest2.epd' > imagefilename = 'c:\\temp\\drawtest2.png' > > EPD_2_Image(epdfilename, imagefilename)
Thanks for taking the time to give such detailed suggestions - very much appreciated! K -- https://mail.python.org/mailman/listinfo/python-list