fix: Addressing depreceation of PIL.PyAccess in ImageToLPBM

This commit is contained in:
Duncan Mundell 2024-11-08 07:28:17 -07:00
parent 3d94e8e810
commit 4da329385f
2 changed files with 64 additions and 14 deletions

45
g13gui.geany Normal file
View File

@ -0,0 +1,45 @@
[editor]
line_wrapping=false
line_break_column=72
auto_continue_multiline=true
[file_prefs]
final_new_line=true
ensure_convert_new_lines=false
strip_trailing_spaces=false
replace_tabs=false
[indentation]
indent_width=4
indent_type=0
indent_hard_tab_width=8
detect_indent=false
detect_indent_width=true
indent_mode=2
[project]
name=g13gui
base_path=/home/duncan/projects/g13gui
description=
file_patterns=
[long line marker]
long_line_behaviour=1
long_line_column=72
[files]
current_page=0
FILE_NAME_0=1386;Python;0;EUTF-8;1;1;0;%2Fhome%2Fduncan%2Fprojects%2Fg13gui%2Fg13gui%2Fg13%2Fdisplaydevice.py;0;4
[VTE]
last_dir=/home/duncan
[prjorg]
expanded_paths=/home/duncan/projects/g13gui/g13gui/g13;
source_patterns=*.c;*.C;*.cpp;*.cxx;*.c++;*.cc;*.m;
header_patterns=*.h;*.H;*.hpp;*.hxx;*.h++;*.hh;
ignored_dirs_patterns=.*;CVS;
ignored_file_patterns=*.o;*.obj;*.a;*.lib;*.so;*.dll;*.lo;*.la;*.class;*.jar;*.pyc;*.mo;*.gmo;
generate_tag_prefs=0
show_empty_dirs=true
external_dirs=

View File

@ -11,31 +11,36 @@ class DisplayMetrics(object):
def ImageToLPBM(image):
"""Simple function to convert a PIL Image into LPBM format."""
i = PIL.PyAccess.new(image, readonly=True)
#Simple function to convert a PIL Image into LPBM format.
# Ensure the image is in grayscale ('L') or monochrome ('1') mode
image = image.convert('1') # Convert to monochrome (1-bit) if not already
pixels = image.load() # Access the pixel data directly
bio = BytesIO()
maxBytes = (DisplayMetrics.WIDTH_PIXELS *
DisplayMetrics.HEIGHT_PIXELS // 8)
# Assuming DisplayMetrics with fixed width and height
maxBytes = (DisplayMetrics.WIDTH_PIXELS * DisplayMetrics.HEIGHT_PIXELS) // 8
row = 0
col = 0
for byteNum in range(0, maxBytes):
b = int()
b = 0
if row == 40:
maxSubrow = 3
else:
maxSubrow = 8
# Adjust the number of rows processed in a byte based on row position
maxSubrow = 3 if row == 40 else 8
for subrow in range(0, maxSubrow):
b |= i[col, row + subrow] << subrow
for subrow in range(maxSubrow):
# Get pixel value at (col, row + subrow) and shift into position
pixel_value = pixels[col, row + subrow]
b |= (1 if pixel_value == 0 else 0) << subrow # 0 = black, 1 = white in '1' mode
# Write the byte to the output buffer
bio.write(struct.pack('<B', b))
# Update column and row positions
col += 1
if (col % 160) == 0:
if col % DisplayMetrics.WIDTH_PIXELS == 0:
col = 0
row += 8