- Download image
- Remove background and keep only one colour line
- Run script that will replace all non transparent colours with black
Original image
Image without background with remnants of other colours
Result with only black colour
| # 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) |
Original image
Image without background with remnants of other colours
Result with only black colour