Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #88705

Re: Code critique please

From Peter Otten <__peter__@web.de>
Subject Re: Code critique please
Date 2015-04-09 12:26 +0200
Organization None
References <53f4c2fd-a20c-4d09-8719-8f3f9b80670b@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.160.1428575207.12925.python-list@python.org> (permalink)

Show all headers | View raw


kai.peters@gmail.com wrote:

> I just wrote this bit (coming from Pascal) 

>     if (((xdim / 8) * ydim) + header) <> filesize:

Yeah, either Pascal or Barry Warsaw is your uncle ;)

https://www.python.org/dev/peps/pep-0401/

> and am wondering how seasoned
> Python programmers would have done the same? Anything terribly non-python?

> def RenderByte(draw, byte, x, y):

Please read PEP 8 for naming conventions etc.

The function at the core of your code

> 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

can be fixed/micro-optimised, and if I were to do that I'd precreate a 
lookup table that maps every byte (i. e. value in the range 0...255) to a 
sequence of offsets 

lookup = [
    (),      # for b"\x00" the inner loop is empty
    (0),
    (1),
    (0, 1),
    ...
    (0, 1, 2, 3, 4, 5, 6, 7), # for b"\xFF" the inner loop comprises 
                              # all 8 bits
]
or to a 8x1 image, but my idea of a "seasoned Python programmer" will try to 
keep the big picture in mind -- and that is that handling individual 
bits/pixels in Python is inefficient. Therefore many libraries tend to offer 
an alternative that is both easier to use and faster.

In this case that's

   img = Image.frombytes("1", (width, height), data)
 
and with the extra comfort of reading the size from the EPD file

from PIL import Image
import struct

EPDFILE = "tmp.epd"
PNGFILE = "tmp.png"

class EPDError(Exception):
    pass

with open(EPDFILE, "rb") as f:
    header = f.read(16)
    width, height, colordepth = struct.unpack("<HHb", header[1:6])
    if colordepth != 1:
        raise EPDError("Unsupported color depth {}".format(colordepth))
    data = f.read()
    datasize = width // 8 * height
    if len(data) != datasize:
        raise EPDError(
            "Inconsistent pixel data size. "
            "Expected {} but got {} bytes".format(
                datasize, len(data)))
    img = Image.frombytes("1", (width, height), data)
    img.save(PNGFILE)


I'm assuming the header follows the format spec given in this PDF:

http://www.pervasivedisplays.com/_literature_112691/Developer_Guide_for_Demo_Kit

PS: I thought that Image.frombytes() was already pointed out to you, but I 
failed to find it in the archives. So there.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Code critique please kai.peters@gmail.com - 2015-04-07 15:43 -0700
  Re: Code critique please Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-07 17:25 -0600
  Re: Code critique please Chris Kaynor <ckaynor@zindagigames.com> - 2015-04-07 16:38 -0700
  Re: Code critique please Cameron Simpson <cs@zip.com.au> - 2015-04-08 09:49 +1000
  Re: Code critique please kai.peters@gmail.com - 2015-04-07 16:51 -0700
  Re: Code critique please Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-04-08 01:54 +0100
  Re: Code critique please Robert Kern <robert.kern@gmail.com> - 2015-04-08 13:25 +0100
  Re: Code critique please Peter Otten <__peter__@web.de> - 2015-04-09 12:26 +0200

csiph-web