Skip to content

Instantly share code, notes, and snippets.

@tyhenry
Created August 29, 2023 20:57
Show Gist options
  • Save tyhenry/6ba9a98c08bc82846b52296f61e08673 to your computer and use it in GitHub Desktop.
Save tyhenry/6ba9a98c08bc82846b52296f61e08673 to your computer and use it in GitHub Desktop.
python script to help fix the RGB values of transparent pixels in images
#!/usr/bin/env python
# pip install Pillow
from PIL import Image
def replace_transparent_pixels(base_image_path, replacement_image_path, output_image_path):
base_image = Image.open(base_image_path)
replacement_image = Image.open(replacement_image_path)
base_image = base_image.convert("RGBA")
replacement_image = replacement_image.convert("RGBA")
base_data = base_image.getdata()
replacement_data = replacement_image.getdata()
result_image = Image.new("RGBA", base_image.size)
# loop through every pixel in base image, and replace the RGB of the pixel with RGB from same coordinates in replacement image if the pixel in base image is transparent
for index, pixel in enumerate(base_data):
xy = (index % base_image.size[0], index // base_image.size[0])
data = pixel
if pixel[3] == 0:
data = replacement_data[index][:3] + (0,)
result_image.putpixel(xy, data)
result_image.save(output_image_path)
def replace_transparent_pixels_RGB(base_image_path, rgb_value, output_image_path):
base_image = Image.open(base_image_path)
base_image = base_image.convert("RGBA")
base_data = base_image.getdata()
result_image = Image.new("RGBA", base_image.size)
# loop through every pixel in base image, and replace the RGB of the pixel with RGB value if the pixel in base image is fully transparent
for index, pixel in enumerate(base_data):
xy = (index % base_image.size[0], index // base_image.size[0])
data = pixel
if pixel[3] == 0:
data = tuple(rgb_value) + (0,)
result_image.putpixel(xy, data)
result_image.save(output_image_path)
def reveal_transparent_pixels(base_image_path, output_image_path):
base_image = Image.open(base_image_path)
base_image = base_image.convert("RGB")
base_image.save(output_image_path)
if __name__ == "__main__":
# accept arguments for base image path, replacement image path or RGB value, and output image path
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("base_image_path", help="path to base image")
parser.add_argument("output_image_path", help="path to output image")
parser.add_argument("-r", "--rgb", nargs=3, type=int, help="RGB value to replace transparent pixels with")
parser.add_argument("-i", "--replacement_image_path", help="path to replacement image")
parser.add_argument("-v", "--reveal", action="store_true", help="reveal transparent pixels in base image")
args = parser.parse_args()
# if RGB value is provided, use that to replace transparent pixels
if args.rgb:
replace_transparent_pixels_RGB(args.base_image_path, args.rgb, args.output_image_path)
# if replacement image path is provided, use that to replace transparent pixels
elif args.replacement_image_path:
replace_transparent_pixels(args.base_image_path, args.replacement_image_path, args.output_image_path)
elif args.reveal:
reveal_transparent_pixels(args.base_image_path, args.output_image_path)
# if neither is provided, print error message
else:
print("Error: either RGB value or replacement image path must be provided")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment