Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
st7789_mpy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
st7789_mpy
Commits
8de08fed
Commit
8de08fed
authored
Mar 10, 2021
by
Russ Hughes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add depth conversion and usage
parent
55ca1dcd
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
77 additions
and
55 deletions
+77
-55
utils/imgtobitmap.py
utils/imgtobitmap.py
+77
-55
No files found.
utils/imgtobitmap.py
View file @
8de08fed
#!
/usr/local/bin/python3.6
#!
python3
'''
Convert image file to python module for use with blit_bitmap.
Usage imgtobitmap image_file bits
-per-
pixel >image.py
Usage imgtobitmap image_file bits
_per_
pixel >image.py
'''
import
sys
from
PIL
import
Image
from
itertools
import
groupby
import
argparse
bits
=
int
(
sys
.
argv
[
2
])
# output bits per pixel (1-8)
img
=
Image
.
open
(
sys
.
argv
[
1
])
img
=
img
.
convert
(
"P"
,
palette
=
Image
.
ADAPTIVE
)
# Make sure we are in 8-bit RGB
palette
=
img
.
getpalette
()
# Make copy of palette colors
def
main
():
# For all the colors in the palette
colors
=
[]
for
color
in
range
(
1
<<
bits
):
parser
=
argparse
.
ArgumentParser
(
prog
=
'imgtobitmap'
,
description
=
'Convert image file to python module for use with bitmap method.'
)
# get rgb values and convert to 565
color565
=
(
((
palette
[
color
*
3
]
&
0xF8
)
<<
8
)
|
((
palette
[
color
*
3
+
1
]
&
0xFC
)
<<
3
)
|
((
palette
[
color
*
3
+
2
]
&
0xF8
)
>>
3
))
parser
.
add_argument
(
'image_file'
,
help
=
'Name of file containing image to convert'
)
# swap bytes in 565
color
=
((
color565
&
0xff
)
<<
8
)
+
((
color565
&
0xff00
)
>>
8
)
parser
.
add_argument
(
'bits_per_pixel'
,
type
=
int
,
choices
=
range
(
1
,
9
),
default
=
1
,
metavar
=
'bits_per_pixel'
,
help
=
'The number of bits to use per pixel (1..8)'
)
# append byte swapped 565 color to colors
colors
.
append
(
f'
{
color
:
04
x
}
'
)
args
=
parser
.
parse_args
()
bits
=
args
.
bits_per_pixel
img
=
Image
.
open
(
args
.
image_file
)
img
=
img
.
convert
(
"P"
,
palette
=
Image
.
ADAPTIVE
,
colors
=
2
**
bits
)
palette
=
img
.
getpalette
()
# Make copy of palette colors
image_bitstring
=
''
bit_index
=
0
max_colors
=
1
<<
bits
# For all the colors in the palette
colors
=
[]
for
color
in
range
(
1
<<
bits
):
# Run through the image and create a string with the ascii binary representation
# of the color of each pixel.
for
y
in
range
(
img
.
height
):
for
x
in
range
(
img
.
width
):
pixel
=
img
.
getpixel
((
x
,
y
))
color
=
pixel
bstring
=
''
for
bit
in
range
(
bits
,
0
,
-
1
):
bstring
+=
'1'
if
(
color
&
(
1
<<
bit
-
1
))
else
'0'
image_bitstring
+=
bstring
# get rgb values and convert to 565
color565
=
(
((
palette
[
color
*
3
]
&
0xF8
)
<<
8
)
|
((
palette
[
color
*
3
+
1
]
&
0xFC
)
<<
3
)
|
((
palette
[
color
*
3
+
2
]
&
0xF8
)
>>
3
))
bitmap_bits
=
len
(
image_bitstring
)
# swap bytes in 565
color
=
((
color565
&
0xff
)
<<
8
)
+
((
color565
&
0xff00
)
>>
8
)
# Create python source with image parameters
print
(
f'HEIGHT =
{
img
.
height
}
'
)
print
(
f'WIDTH =
{
img
.
width
}
'
)
print
(
f'COLORS =
{
max_colors
}
'
)
print
(
f'BITS =
{
bitmap_bits
}
'
)
print
(
f'BPP =
{
bits
}
'
)
print
(
'PALETTE = ['
,
sep
=
''
,
end
=
''
)
# append byte swapped 565 color to colors
colors
.
append
(
f'
{
color
:
04
x
}
'
)
for
color
,
rgb
in
enumerate
(
colors
):
if
color
:
print
(
','
,
sep
=
''
,
end
=
''
)
print
(
f'0x
{
rgb
}
'
,
sep
=
''
,
end
=
''
)
print
(
"]"
)
# Run though image bit string 8 bits at a time
# and create python array source for memoryview
image_bitstring
=
''
bit_index
=
0
max_colors
=
1
<<
bits
print
(
"_bitmap =
\\
"
,
sep
=
''
)
print
(
"b'"
,
sep
=
''
,
end
=
''
)
# Run through the image and create a string with the ascii binary representation
# of the color of each pixel.
for
y
in
range
(
img
.
height
):
for
x
in
range
(
img
.
width
):
pixel
=
img
.
getpixel
((
x
,
y
))
color
=
pixel
bstring
=
''
for
bit
in
range
(
bits
,
0
,
-
1
):
bstring
+=
'1'
if
(
color
&
(
1
<<
bit
-
1
))
else
'0'
image_bitstring
+=
bstring
for
i
in
range
(
0
,
bitmap_bits
,
8
):
bitmap_bits
=
len
(
image_bitstring
)
if
i
and
i
%
(
16
*
8
)
==
0
:
print
(
"'
\\\n
b'"
,
end
=
''
,
sep
=
''
)
# Create python source with image parameters
print
(
f'HEIGHT =
{
img
.
height
}
'
)
print
(
f'WIDTH =
{
img
.
width
}
'
)
print
(
f'COLORS =
{
max_colors
}
'
)
print
(
f'BITS =
{
bitmap_bits
}
'
)
print
(
f'BPP =
{
bits
}
'
)
print
(
'PALETTE = ['
,
sep
=
''
,
end
=
''
)
value
=
image_bitstring
[
i
:
i
+
8
]
color
=
int
(
value
,
2
)
print
(
f'
\\
x
{
color
:
02
x
}
'
,
sep
=
''
,
end
=
''
)
for
color
,
rgb
in
enumerate
(
colors
):
if
color
:
print
(
','
,
sep
=
''
,
end
=
''
)
print
(
f'0x
{
rgb
}
'
,
sep
=
''
,
end
=
''
)
print
(
"]"
)
print
(
"'
\n
BITMAP = memoryview(_bitmap)"
)
# Run though image bit string 8 bits at a time
# and create python array source for memoryview
print
(
"_bitmap =
\\
"
,
sep
=
''
)
print
(
"b'"
,
sep
=
''
,
end
=
''
)
for
i
in
range
(
0
,
bitmap_bits
,
8
):
if
i
and
i
%
(
16
*
8
)
==
0
:
print
(
"'
\\\n
b'"
,
end
=
''
,
sep
=
''
)
value
=
image_bitstring
[
i
:
i
+
8
]
color
=
int
(
value
,
2
)
print
(
f'
\\
x
{
color
:
02
x
}
'
,
sep
=
''
,
end
=
''
)
print
(
"'
\n
BITMAP = memoryview(_bitmap)"
)
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment