78 lines
2.2 KiB
Python
78 lines
2.2 KiB
Python
import sys
|
|
import os
|
|
from PIL import Image
|
|
|
|
def process_image_to_canvas(image_path, threshold=128):
|
|
try:
|
|
img = Image.open(image_path).convert('L')
|
|
img = img.resize((16, 16))
|
|
|
|
img = img.transpose(Image.FLIP_TOP_BOTTOM)
|
|
img = img.transpose(Image.ROTATE_270)
|
|
|
|
pixels = img.load()
|
|
|
|
my_canvas = [[] for _ in range(4)]
|
|
|
|
row_ranges = [(0, 5), (5, 10), (10, 15), (15, 16)]
|
|
|
|
for row_idx, (start_y, end_y) in enumerate(row_ranges):
|
|
for x in range(16):
|
|
bits = ""
|
|
for y in range(start_y, end_y):
|
|
bits += "1" if pixels[x, y] < threshold else "0"
|
|
|
|
while len(bits) < 5:
|
|
bits += "0"
|
|
|
|
my_canvas[row_idx].append(bits)
|
|
|
|
return my_canvas
|
|
except Exception as e:
|
|
print(f"Error occurred while processing an image: {e}")
|
|
sys.exit(1)
|
|
|
|
def create_sysex_file(filename, canvas):
|
|
model_id = 0x45
|
|
address = [0x10, 0x01, 0x00]
|
|
|
|
data = []
|
|
for block_row in canvas:
|
|
for column_str in block_row:
|
|
val = int(column_str, 2)
|
|
data.append(val)
|
|
|
|
if len(data) < 64:
|
|
data += [0] * (64 - len(data))
|
|
|
|
def calc_checksum(addr, payload):
|
|
s = sum(addr) + sum(payload)
|
|
return (128 - (s % 128)) % 128
|
|
|
|
checksum = calc_checksum(address, data)
|
|
full_packet = bytes([0xF0, 0x41, 0x10, model_id, 0x12] + address + data + [checksum] + [0xF7])
|
|
|
|
with open(filename, "wb") as f:
|
|
f.write(full_packet)
|
|
print(f"Output file: {filename}")
|
|
print("HEX:", " ".join(f"{b:02X}" for b in full_packet))
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
print("Usage: python sc55art.py <image path>")
|
|
return
|
|
|
|
input_path = sys.argv[1]
|
|
|
|
if not os.path.exists(input_path):
|
|
print(f"File {input_path} not found.")
|
|
return
|
|
|
|
base_name = os.path.splitext(input_path)[0]
|
|
output_filename = f"{base_name}.syx"
|
|
|
|
canvas = process_image_to_canvas(input_path)
|
|
create_sysex_file(output_filename, canvas)
|
|
|
|
if __name__ == "__main__":
|
|
main() |