Skip to content

Instantly share code, notes, and snippets.

@pierrel
Created December 15, 2009 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pierrel/257250 to your computer and use it in GitHub Desktop.
Save pierrel/257250 to your computer and use it in GitHub Desktop.
#! /usr/bin/python
import sys
import Image
def corners(image, test_square_size = 1):
"""returns an array of the corner bounds of the image"""
size_x, size_y = image.size
max_x, max_y = (size_x-1, size_y-1)
if max_x < test_square_size or max_y < test_square_size:
raise Exception, 'test_square_size cannot be larger than the image size'
top_left = (0, 0, test_square_size, test_square_size)
top_right = (max_x-test_square_size, 0, max_x, test_square_size)
bottom_left = (0, max_y-test_square_size, test_square_size, max_y)
bottom_right = (max_x-test_square_size, max_y-test_square_size, max_x, max_y)
return [top_left, top_right, bottom_left, bottom_right]
def is_transparent(area):
"""returns True if the entire area is transparent, False otherwise"""
transparents = [(255, 255, 255, 0), (0, 0, 0, 0), 255]
max_x, max_y = area.size
for x in range(0, max_x):
for y in range(0, max_y):
pixel = area.getpixel((x, y))
if not pixel in transparents:
return False
return True
# Will print "Transparent"" if any of the corners are transparent
# If all the corners are non-transparent then it will print "Not transparent"
filename = sys.argv[1]
image = Image.open(filename)
for corner in corners(image):
area = image.crop(corner)
if is_transparent(area):
print "Transparent"
sys.exit(0)
print "Not transparent"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment