Created
March 3, 2016 20:36
-
-
Save matteblair/63d7d5459e3edc56aafe to your computer and use it in GitHub Desktop.
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
# USAGE | |
# python pngpixelbleed.py input_file [output_file] | |
# | |
# input_file: path from the call site to the input image file; supports | |
# png, jpg, tiff, gif, and psd formats | |
# output_file: optional path from the call site to the desired output | |
# image file; supports all input formats except psd; if omitted, | |
# output will be written to the input file | |
# | |
# This script requires the the Pillow python module: | |
# http://pillow.readthedocs.org/en/3.1.x/installation.html | |
import sys, os | |
from PIL import Image | |
input_file = sys.argv[1] | |
output_file = input_file | |
if len(sys.argv) > 2: | |
output_file = sys.argv[2] | |
image = Image.open(input_file) | |
(width, height) = image.size | |
for x in range(width): | |
for y in range(height): | |
if x == 0 or x == width - 1 or y == 0 or y == height - 1: | |
continue | |
input_pixel = image.getpixel((x, y)) | |
if (input_pixel[3] != 0): | |
continue | |
neighbors = [] | |
neighbors.append(image.getpixel((x + 1, y + 0))) | |
neighbors.append(image.getpixel((x + 1, y + 1))) | |
neighbors.append(image.getpixel((x + 0, y + 1))) | |
neighbors.append(image.getpixel((x - 1, y + 1))) | |
neighbors.append(image.getpixel((x - 1, y + 0))) | |
neighbors.append(image.getpixel((x - 1, y - 1))) | |
neighbors.append(image.getpixel((x + 0, y - 1))) | |
neighbors.append(image.getpixel((x + 1, y - 1))) | |
neighbor_colors = [n for n in neighbors if n[3] != 0] | |
(r_avg, g_avg, b_avg, a_avg) = (0, 0, 0, 0) | |
for n in neighbor_colors: | |
r_avg += n[0] / len(neighbor_colors) | |
g_avg += n[1] / len(neighbor_colors) | |
b_avg += n[2] / len(neighbor_colors) | |
if (len(neighbor_colors) > 0): | |
image.putpixel((x, y), (r_avg, g_avg, b_avg, a_avg)) | |
with open(output_file, 'wb') as f: | |
image.save(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment