Last active
June 7, 2021 07:35
-
-
Save meikuam/e713927336e3296bbb2e96e568a29d53 to your computer and use it in GitHub Desktop.
This file contains 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
""" | |
this script takes `image.png` as input, | |
resize it to 63pix height, | |
then binarize it, | |
to create `_Bootscreen.h` file | |
`_Bootscreen.h` can be used as part of https://github.com/MarlinFirmware/Marlin | |
I used it with: https://github.com/bigtreetech/BIGTREETECH-SKR-mini-E3/tree/master/firmware/V2.0/Marlin-2.0.7.2-SKR-mini-E3-V2.0 | |
""" | |
import numpy as np | |
import skimage.io as skio | |
import matplotlib.pyplot as plt | |
from skimage.color import rgb2gray, rgba2rgb | |
from skimage.filters import threshold_li, threshold_otsu, threshold_triangle | |
from skimage.transform import rescale, resize, downscale_local_mean | |
default_strings = """ | |
#pragma once | |
#define CONFIG_EXAMPLES_DIR "Creality/Ender-3 Pro/BigTreeTech SKR Mini E3 2.0" | |
#define CUSTOM_BOOTSCREEN_TIMEOUT 2000 | |
#define CUSTOM_BOOTSCREEN_INVERTED | |
""" | |
if __name__ == "__main__": | |
lcd_width = 127 | |
lcd_height = 63 | |
# open input image | |
image = skio.imread("image.png") | |
# to grayscale | |
if image.shape[-1] == 4: | |
image = rgba2rgb(image) | |
image = rgb2gray(image) | |
im_height, im_width = image.shape[:2] | |
scale = min(lcd_width / im_width, lcd_height / im_height) | |
plt.imshow(image, cmap=plt.cm.gray) | |
plt.show() | |
image = rescale(image, scale, anti_aliasing=False) | |
CUSTOM_BOOTSCREEN_BMPWIDTH = image.shape[1] | |
CUSTOM_BOOTSCREEN_BMPHEIGHT = image.shape[0] | |
plt.imshow(image, cmap=plt.cm.gray) | |
plt.show() | |
thresh = threshold_otsu(image) | |
image = image > thresh | |
plt.imshow(image, cmap=plt.cm.gray) | |
plt.show() | |
with open('_Bootscreen.h', 'w') as f: | |
f.write(default_strings) | |
f.write(f"#define CUSTOM_BOOTSCREEN_BMPWIDTH {CUSTOM_BOOTSCREEN_BMPWIDTH}\n") | |
f.write(f"#define CUSTOM_BOOTSCREEN_BMPHEIGHT {CUSTOM_BOOTSCREEN_BMPHEIGHT}\n") | |
f.write("const unsigned char custom_start_bmp[] PROGMEM = {\n") | |
for image_line in image: | |
mod = 8 - image_line.shape[0] % 8 | |
b = np.array([True] * mod) | |
image_line = np.concatenate([image_line, b]) | |
image_lines = np.split(image_line, image_line.shape[0] // 8) | |
for char in image_lines: | |
char = char.astype(np.uint8).tolist() | |
char = "".join(map(str, char)) | |
char = f"B{char}, " | |
f.write(char) | |
f.write('\n') | |
f.write("};\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment