From 9bd07982a4bfffab907ab5b7a3b5312c48922fa2 Mon Sep 17 00:00:00 2001 From: msmthng <40269445+msmthng@users.noreply.github.com> Date: Mon, 20 Feb 2023 19:40:26 +0100 Subject: [PATCH] make transparent thumbnail --- octoprint_prusaslicerthumbnails/__init__.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/octoprint_prusaslicerthumbnails/__init__.py b/octoprint_prusaslicerthumbnails/__init__.py index 107298f..f1e51a8 100644 --- a/octoprint_prusaslicerthumbnails/__init__.py +++ b/octoprint_prusaslicerthumbnails/__init__.py @@ -156,8 +156,21 @@ def _extract_flashprint_thumbnail(self, gcode_encoded_images): encoded_image = gcode_encoded_images[0] image = Image.open(io.BytesIO(encoded_image)).resize((160,120)) + rgba = image.convert("RGBA") + pixels = rgba.getdata() + newData = [] + + alphamaxvalue = 35 + for pixel in pixels: + if pixel[0] >= 0 and pixel[0] <= alphamaxvalue and pixel[1] >= 0 and pixel[1] <= alphamaxvalue and pixel[2] >= 0 and pixel[2] <= alphamaxvalue : # finding black colour by its RGB value + newData.append((255, 255, 255, 0)) # storing a transparent value when we find a black/dark colour + else: + newData.append(pixel) # other colours remain unchanged + + rgba.putdata(newData) + with io.BytesIO() as png_bytes: - image.save(png_bytes, "PNG") + rgba.save(png_bytes, "PNG") png_bytes_string = png_bytes.getvalue() return png_bytes_string