Skip to content

Instantly share code, notes, and snippets.

@JakubAndrysek
Last active January 26, 2025 20:32
Show Gist options
  • Save JakubAndrysek/fa455593bd31b2fc48e69005ceb33391 to your computer and use it in GitHub Desktop.
Save JakubAndrysek/fa455593bd31b2fc48e69005ceb33391 to your computer and use it in GitHub Desktop.
Converting one colour line image for laser engraving
# Reimport necessary libraries and redefine the function due to the state reset
from PIL import Image
# Define the function to convert non-transparent pixels to black
def convert_non_transparent_to_black(image_path, output_path):
# Open the image
image = Image.open(image_path).convert("RGBA")
data = image.load()
# Iterate through all pixels
for y in range(image.height):
for x in range(image.width):
r, g, b, a = data[x, y] # Get RGBA values
if a != 0: # If not transparent, set to black
data[x, y] = (0, 0, 0, a)
# Save the updated image
image.save(output_path)
return output_path
# Process the newly uploaded image
new_image_path = "source.png"
new_output_path = new_image_path.replace(".png", "_black.png")
convert_non_transparent_to_black(new_image_path, new_output_path)

Converting one colour line image for laser engraving

  • Download image
  • Remove background and keep only one colour line
  • Run script that will replace all non transparent colours with black

Example

Original image

Image without background with remnants of other colours

  • image
  • image

Result with only black colour

  • image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment