Python3 and PEP8 (#131)
This commit is contained in:
committed by
Ben V. Brown
parent
53399f5866
commit
b6c193309d
@@ -1,24 +1,23 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
# coding=utf-8
|
||||||
VERSION_STRING = '0.01'
|
from __future__ import division
|
||||||
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import PIL, PIL.Image, PIL.ImageOps
|
from PIL import Image, ImageOps
|
||||||
except ImportError,error:
|
except ImportError as error:
|
||||||
raise ImportError, "%s: %s requres Python Imaging Library (PIL). " \
|
raise ImportError("{}: {} requres Python Imaging Library (PIL). "
|
||||||
"Install with `pip` or OS-specific package " \
|
"Install with `pip` or OS-specific package "
|
||||||
"management tool." \
|
"management tool."
|
||||||
% (error, sys.argv[0])
|
.format(error, sys.argv[0]))
|
||||||
|
|
||||||
|
|
||||||
|
VERSION_STRING = '0.01'
|
||||||
|
|
||||||
LCD_WIDTH = 96
|
LCD_WIDTH = 96
|
||||||
LCD_HEIGHT = 16
|
LCD_HEIGHT = 16
|
||||||
LCD_NUM_BYTES = LCD_WIDTH * LCD_HEIGHT / 8
|
LCD_NUM_BYTES = LCD_WIDTH * LCD_HEIGHT // 8
|
||||||
LCD_PADDED_SIZE = 1024
|
LCD_PADDED_SIZE = 1024
|
||||||
|
|
||||||
INTELHEX_DATA_RECORD = 0x00
|
INTELHEX_DATA_RECORD = 0x00
|
||||||
@@ -28,40 +27,36 @@ INTELHEX_BYTES_PER_LINE = 16
|
|||||||
INTELHEX_MINIMUM_SIZE = 4096
|
INTELHEX_MINIMUM_SIZE = 4096
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def split16(word):
|
def split16(word):
|
||||||
'''return high and low byte of 16-bit word value as tuple'''
|
"""return high and low byte of 16-bit word value as tuple"""
|
||||||
return ((word >> 8) & 0xff, word & 0xff)
|
return (word >> 8) & 0xff, word & 0xff
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def intel_hex_line(file, record_type, offset, data):
|
def intel_hex_line(file, record_type, offset, data):
|
||||||
'''write a line of data in Intel hex format'''
|
"""write a line of data in Intel hex format"""
|
||||||
# length, address offset, record type
|
# length, address offset, record type
|
||||||
record_length = len(data)
|
record_length = len(data)
|
||||||
file.write(':%02X%04X%02X' % (record_length, offset, record_type))
|
file.write(':{:02X}{:04X}{:02X}'.format(record_length, offset, record_type))
|
||||||
|
|
||||||
# data
|
# data
|
||||||
map(lambda byte: file.write("%02X" % byte), data)
|
map(lambda byte: file.write("{:02X}".format(byte)), data)
|
||||||
|
|
||||||
# compute and write checksum (with DOS line ending for compatibility/safety)
|
# compute and write checksum (with DOS line ending for compatibility/safety)
|
||||||
file.write( "%02X\r\n"
|
file.write("{:02X}\r\n"
|
||||||
% ( ( ( sum(data, # sum data ...
|
.format(((sum(data, # sum data ...
|
||||||
record_length # ... and other ...
|
record_length # ... and other ...
|
||||||
+ sum(split16(offset)) # ... fields ...
|
+ sum(split16(offset)) # ... fields ...
|
||||||
+ record_type) # ... on line
|
+ record_type) # ... on line
|
||||||
& 0xff) # low 8 bits
|
& 0xff) # low 8 bits
|
||||||
^ 0xff) # two's ...
|
^ 0xff) # two's ...
|
||||||
+ 1)) # ... complement
|
+ 1)) # ... complement
|
||||||
|
|
||||||
|
|
||||||
|
def intel_hex(file, bytes_, start_address=0x0):
|
||||||
def intel_hex(file, bytes, start_address = 0x0):
|
"""write block of data in Intel hex format"""
|
||||||
'''write block of data in Intel hex format'''
|
if len(bytes_) % INTELHEX_BYTES_PER_LINE != 0:
|
||||||
if len(bytes) % INTELHEX_BYTES_PER_LINE != 0:
|
raise ValueError("Program error: Size of LCD data is not evenly divisible by {}"
|
||||||
raise ValueError, \
|
.format(INTELHEX_BYTES_PER_LINE))
|
||||||
"Program error: Size of LCD data is not evenly divisible by %s" \
|
|
||||||
% INTELHEX_BYTES_PER_LINE
|
|
||||||
|
|
||||||
address_lo = start_address & 0xffff
|
address_lo = start_address & 0xffff
|
||||||
address_hi = (start_address >> 16) & 0xffff
|
address_hi = (start_address >> 16) & 0xffff
|
||||||
@@ -74,11 +69,11 @@ def intel_hex(file, bytes, start_address = 0x0):
|
|||||||
size_written = 0
|
size_written = 0
|
||||||
while size_written < INTELHEX_MINIMUM_SIZE:
|
while size_written < INTELHEX_MINIMUM_SIZE:
|
||||||
offset = address_lo
|
offset = address_lo
|
||||||
for line_start in range(0, len(bytes), INTELHEX_BYTES_PER_LINE):
|
for line_start in range(0, len(bytes_), INTELHEX_BYTES_PER_LINE):
|
||||||
intel_hex_line(file,
|
intel_hex_line(file,
|
||||||
INTELHEX_DATA_RECORD,
|
INTELHEX_DATA_RECORD,
|
||||||
offset,
|
offset,
|
||||||
bytes[line_start:line_start+INTELHEX_BYTES_PER_LINE])
|
bytes_[line_start:line_start + INTELHEX_BYTES_PER_LINE])
|
||||||
size_written += INTELHEX_BYTES_PER_LINE
|
size_written += INTELHEX_BYTES_PER_LINE
|
||||||
if size_written >= INTELHEX_MINIMUM_SIZE:
|
if size_written >= INTELHEX_MINIMUM_SIZE:
|
||||||
break
|
break
|
||||||
@@ -87,14 +82,13 @@ def intel_hex(file, bytes, start_address = 0x0):
|
|||||||
intel_hex_line(file, INTELHEX_END_OF_FILE_RECORD, 0, ())
|
intel_hex_line(file, INTELHEX_END_OF_FILE_RECORD, 0, ())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def img2hex(input_filename,
|
def img2hex(input_filename,
|
||||||
output_file,
|
output_file,
|
||||||
preview_filename=None,
|
preview_filename=None,
|
||||||
threshold=128,
|
threshold=128,
|
||||||
dither=False,
|
dither=False,
|
||||||
negative=False):
|
negative=False):
|
||||||
'''
|
"""
|
||||||
Convert 'input_filename' image file into Intel hex format with data
|
Convert 'input_filename' image file into Intel hex format with data
|
||||||
formatted for display on TS100 LCD and file object.
|
formatted for display on TS100 LCD and file object.
|
||||||
Input image is converted from color or grayscale to black-and-white,
|
Input image is converted from color or grayscale to black-and-white,
|
||||||
@@ -107,13 +101,12 @@ def img2hex(input_filename,
|
|||||||
dithering algorithm used.
|
dithering algorithm used.
|
||||||
Optional `negative' inverts black/white regardless of input image type
|
Optional `negative' inverts black/white regardless of input image type
|
||||||
or other options.
|
or other options.
|
||||||
'''
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
image = PIL.Image.open(input_filename)
|
image = Image.open(input_filename)
|
||||||
except BaseException,error:
|
except BaseException as e:
|
||||||
raise IOError, \
|
raise IOError("error reading image file \"{}\": {}".format(input_filename, e))
|
||||||
"error reading image file \"%s\": %s" % (input_filename, error)
|
|
||||||
|
|
||||||
# convert to luminance
|
# convert to luminance
|
||||||
# do even if already black/white because PIL can't invert 1-bit so
|
# do even if already black/white because PIL can't invert 1-bit so
|
||||||
@@ -124,10 +117,10 @@ def img2hex(input_filename,
|
|||||||
image = image.convert('L')
|
image = image.convert('L')
|
||||||
|
|
||||||
if image.size != (LCD_WIDTH, LCD_HEIGHT):
|
if image.size != (LCD_WIDTH, LCD_HEIGHT):
|
||||||
image = image.resize((LCD_WIDTH, LCD_HEIGHT), PIL.Image.BICUBIC)
|
image = image.resize((LCD_WIDTH, LCD_HEIGHT), Image.BICUBIC)
|
||||||
|
|
||||||
if negative:
|
if negative:
|
||||||
image = PIL.ImageOps.invert(image)
|
image = ImageOps.invert(image)
|
||||||
threshold = 255 - threshold # have to invert threshold
|
threshold = 255 - threshold # have to invert threshold
|
||||||
|
|
||||||
if dither:
|
if dither:
|
||||||
@@ -135,7 +128,8 @@ def img2hex(input_filename,
|
|||||||
else:
|
else:
|
||||||
image = image.point(lambda pixel: 0 if pixel < threshold else 1, '1')
|
image = image.point(lambda pixel: 0 if pixel < threshold else 1, '1')
|
||||||
|
|
||||||
if preview_filename: image.save(preview_filename)
|
if preview_filename:
|
||||||
|
image.save(preview_filename)
|
||||||
|
|
||||||
''' DEBUG
|
''' DEBUG
|
||||||
for row in range(LCD_HEIGHT):
|
for row in range(LCD_HEIGHT):
|
||||||
@@ -155,7 +149,7 @@ def img2hex(input_filename,
|
|||||||
data[3] = 0xF0
|
data[3] = 0xF0
|
||||||
|
|
||||||
# convert to TS100 LCD format
|
# convert to TS100 LCD format
|
||||||
for ndx in range(LCD_WIDTH * 16 / 8):
|
for ndx in range(LCD_WIDTH * 16 // 8):
|
||||||
bottom_half_offset = 0 if ndx < LCD_WIDTH else 8
|
bottom_half_offset = 0 if ndx < LCD_WIDTH else 8
|
||||||
byte = 0
|
byte = 0
|
||||||
for y in range(8):
|
for y in range(8):
|
||||||
@@ -167,7 +161,6 @@ def img2hex(input_filename,
|
|||||||
intel_hex(output_file, data, 0x0800B800)
|
intel_hex(output_file, data, 0x0800B800)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def parse_commandline():
|
def parse_commandline():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
||||||
@@ -175,10 +168,8 @@ def parse_commandline():
|
|||||||
"at startup")
|
"at startup")
|
||||||
|
|
||||||
def zero_to_255(text):
|
def zero_to_255(text):
|
||||||
try:
|
value = int(text)
|
||||||
value = int(text)
|
if not 0 <= value <= 255:
|
||||||
assert(value >= 0 and value <= 255)
|
|
||||||
except:
|
|
||||||
raise argparse.ArgumentTypeError("must be integer from 0 to 255 ")
|
raise argparse.ArgumentTypeError("must be integer from 0 to 255 ")
|
||||||
return value
|
return value
|
||||||
|
|
||||||
@@ -215,37 +206,36 @@ def parse_commandline():
|
|||||||
|
|
||||||
parser.add_argument('-v', '--version',
|
parser.add_argument('-v', '--version',
|
||||||
action='version',
|
action='version',
|
||||||
version="%(prog)s version " + VERSION_STRING,
|
version="%(prog)s version " + VERSION_STRING,
|
||||||
help="print version info")
|
help="print version info")
|
||||||
|
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import argparse
|
import argparse
|
||||||
args = parse_commandline()
|
args = parse_commandline()
|
||||||
|
|
||||||
if os.path.exists(args.output_filename) and not args.force:
|
if os.path.exists(args.output_filename) and not args.force:
|
||||||
sys.stderr.write( "Won't overwrite existing file \"%s\" (use --force "
|
sys.stderr.write("Won't overwrite existing file \"{}\" (use --force "
|
||||||
"option to override)\n"
|
"option to override)\n"
|
||||||
% args.output_filename)
|
.format(args.output_filename))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if args.preview and os.path.exists(args.preview) and not args.force:
|
if args.preview and os.path.exists(args.preview) and not args.force:
|
||||||
sys.stderr.write( "Won't overwrite existing file \"%s\" (use --force "
|
sys.stderr.write("Won't overwrite existing file \"{}\" (use --force "
|
||||||
"option to override)\n"
|
"option to override)\n"
|
||||||
% args.preview)
|
.format(args.preview))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(args.output_filename, 'w') as output_file:
|
with open(args.output_filename, 'w') as output:
|
||||||
img2hex(args.input_filename,
|
img2hex(args.input_filename,
|
||||||
output_file,
|
output,
|
||||||
args.preview,
|
args.preview,
|
||||||
args.threshold,
|
args.threshold,
|
||||||
args.dither,
|
args.dither,
|
||||||
args.negative)
|
args.negative)
|
||||||
except BaseException,error:
|
except BaseException as error:
|
||||||
sys.stderr.write("Error converting file: %s\n" % error)
|
sys.stderr.write("Error converting file: {}\n".format(error))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user