Long story short, I need to check an entire folder of images to see if they're grayscale. Here's a python script I just wrote that will do that, and output the results to a timestamped log file. I added some comments to explain how it works.
graycheck.py
from PIL import Image
import ImageChops
import os
import string
from time import strftime
def main():
subdir = 'C:\\pathtoimages\\'
diffdir = subdir + 'diff' # output directory if you want to save the results
badFiles = []
for subdir, dirs, files in os.walk(subdir):
if subdir != diffdir: #only check the main dir
for file in files:
if string.upper(file[-3:])=="JPG": # jpegs only
im = Image.open(file)
imGray = im.convert("L") # convert the original jpeg to grayscale and keep a copy
# need to convert back to RGB, but only if the original file wasn't already in mode L
if im.mode == "RGB":
imGray = imGray.convert("RGB")
# get the difference of the original image and the converted grayscale
# if the original was grayscale, there's no difference, so output will be entirely black
output = ImageChops.difference(imGray, im)
i = 0
for color in list(output.getdata()):
if color != (0,0,0): # check RGB black and single black value
if color != 0:
# Image is not grayscale, for we found a difference
i=i+1
print i, color
break
outputFile = diffdir + "\\" + file[:-4] + "_Diff.jpg" # output dir plus filename only plus an extension
if i==0: # image is all black, could not be blacker
badFiles.append(file)
# output.save(outputFile) # uncomment to save the results as an image - though it should be a solid black file
# print badFiles # uncomment to see the results
logfilename = strftime("%Y-%m-%d_%H-%M-%S") + ".txt"
logfile = open(logfilename, "w")
for badFile in badFiles:
logfile.write(badFile + '\n')
logfile.close()
if _name_ == '_main_':
main()
If you desire me to release this under a license, consider it released under the
MS Public License.
Somebody just threw a beer bottle at me. I didn't see anyone around, except a dog. It landed a foot from my head. If I'd seen who it was, some serious shit was probably about to go down. It's probably for the best that I didn't see who it was. Apparently you can't even take the garbage out in the middle of the day in this godforsaken town anymore.
Friiot? Hardly. For when you take a bite of tangerine pie, you expect to spill the crumbs on your bed. A wise man once said, why don't you go and make me some toast. And a wiser man said, it is not you who makes the toast, but the toast who makes you. An even wiser man still said, In Soviet Russia, toaster cooks with you? I am part animal, part machine gun. Smithers, there's a rocket in my pocket. Do you enjoy highlighting your face? I do since I cut myself while shaving once. Bled all over the carpet. I bought a hovercraft. I got a big raise and bought myself a hovercraft. WHo put these things under the floor Boards? It's the beating of his hideous heart! Wait, ... wait ... he found a heart container. Problem solved folks, we can all go home to our pork bellies. bellly. belllybelllybelllybellly. It's inside of me! And it's attached to my soul. A piece of ham was driving down the street, when he saw a gang banger banging a gang. He drove to his girlfriend's house, cut her throat, and took her kid to the ball pit. That was some fucking ball pit! Lots of ball sauce and shit.
New, tomorrow night: the most mediocre night of your life. The night after that: the most mediacore night of your life. Thanks, a latte. A man drove to Denver where his grandmother owned a coffee shop. They found her dead in her bathtub. She had dranken a gallon of motor oil for breakfast. Lightens up the chest area, keeps things running on time. You have violated the law: the law of Pepsico. For this we sentence you to die of voltage poisoning. I have to throw up. I had pizza for lunch and now I'm going to throw it up. Here it comes. Here it comes! Transaction rollback in progress. Commit transaction.
This above all else, be true: grape popsicles taste like dying. FOllow grape popsicles on Twitter: @grapepopsicles I wonder if your mom was sad the day you were born because she thought her life was over. I think I'll go buy an ox cart. For carting my oxen. Unix? I don't know this. I like other GUI interfaces. Sausages? Never mind.
What will they call Martha Stewart Living after she passes away
Oink oink piggy wiggy I've got a ham in my belly
if Kevin Bacon was from Toronto would they call him Canadian Kevin Bacon
Corned beef, grits grits, grits grits grits grits, aaaaaaaaaaand sunglasses
at
fucking
midnight
I bet it would be hard to be engaged to Batgirl