Last active
December 29, 2015 05:39
-
-
Save phillipjohnson/7623587 to your computer and use it in GitHub Desktop.
Quick Python script to process image RGB data.
This file contains hidden or 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
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