Last active
February 21, 2023 20:48
-
-
Save npanuhin/ebf2f84868a2095361473b9e475cd676 to your computer and use it in GitHub Desktop.
Find (and potentially eliminate π) semi-transparent pixels in a bunch of PNG files
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
from PIL import Image | |
import os | |
FOLDER_PATH = "output3" | |
for filename in os.listdir(FOLDER_PATH): | |
img = Image.open(os.path.join(FOLDER_PATH, filename), 'r') | |
has_alpha = (img.mode == 'RGBA') | |
print(f"Handling {filename}...") | |
if not has_alpha: | |
print(f"Image {filename} has no alpha channel!") | |
continue | |
# red, green, blue, alpha = img.split() | |
alpha = img.split()[-1] | |
if not all((pixel == 0 or pixel == 255) for pixel in alpha.getdata()): | |
# I know it iterates two times if any semi-transparent pixel is found, | |
# but the goal is to get rid of them, so this code is a bit faster | |
pixels = list(alpha.getdata()) | |
width, height = alpha.size | |
for y in range(height): | |
for x in range(width): | |
if pixels[y * width + x] not in (0, 255): | |
print(f'Found semitrasparent pixel in "{filename}": ({y}, {x})') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment