Skip to content

Instantly share code, notes, and snippets.

@phillipjohnson
Last active December 29, 2015 05:39
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 phillipjohnson/7623587 to your computer and use it in GitHub Desktop.
Save phillipjohnson/7623587 to your computer and use it in GitHub Desktop.
Quick Python script to process image RGB data.
import Image
from os import listdir
rgb_counts = {}
def main():
process()
write_results()
def process():
for file in listdir("./images"):
if not file.endswith(".ini"):
get_img_details(file)
def get_img_details(image_name):
image = Image.open("./images/" + image_name)
width = image.size[0]
height = image.size[1]
pixels = image.load()
for x in range(width):
for y in range(height):
rgb = pixels[x,y]
if rgb_counts.get(rgb):
rgb_counts[rgb] += 1
else:
rgb_counts[rgb] = 1
def write_results():
f = open("data_all.txt","w")
f.truncate()
for k,v in rgb_counts.iteritems():
f.write("{}\t{}\t{}\t{}\n".format(k[0],k[1],k[2],v))
f.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment