Last active
April 28, 2020 21:14
-
-
Save Kyrremann/4359bf8c7905515f1cbc28aa23a83f32 to your computer and use it in GitHub Desktop.
Based on this https://github.com/XScorpion2/Bitmap2OLED
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
def convertBitmap(filename): | |
result = [] | |
bitmap = Image.open(filename) | |
width = bitmap.width | |
height = bitmap.height | |
for y in range(0, height, 8): | |
for x in range(0, width): | |
col = 0 | |
for p in range(0, 8): | |
pixel = bitmap.getpixel((x, y + p)) | |
value = 0 | |
if type(pixel) is tuple: | |
values = sum(pixel) / 3 | |
value = 1 if values > 127 else 0 | |
else: | |
value = 1 if pixel == 255 else 0 | |
s = value << p | |
col = col | s | |
result.append(col) | |
return (result, width, height) | |
def printResult(result, width, height): | |
for y in range(0, int(height/8)): | |
for p in range(0, 8, 2): | |
for x in range(0, width): | |
i = x + y * width; | |
b = result[i] >> p & 1; | |
b2 = result[i] >> (p + 1) & 1; | |
if (b == 0 and b2 == 0): | |
print(" ", end = '') | |
elif (b == 1 and b2 == 0): | |
print("▀", end = '') | |
elif (b == 0 and b2 == 1): | |
print("▄", end = '') | |
elif (b == 1 and b2 == 1): | |
print("█", end = '') | |
print(""); | |
image = convertBitmap("smile-wide.bmp") | |
printResult(*image) | |
print(image[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment