Created
September 17, 2013 17:47
-
-
Save Vanuan/6597932 to your computer and use it in GitHub Desktop.
Sorts images by a number of non-white pixels
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
import cv, cv2 | |
import numpy as np | |
import sys | |
def read(filenames): | |
files = [] | |
for filename in filenames: | |
img = cv2.imread(filename) | |
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) | |
files.append(gray) | |
return files | |
def sort_by_black_count(files): | |
return sorted(files, key=lambda img: cv2.countNonZero(img)) | |
def write(files): | |
for i,img in enumerate(files): | |
cv2.imwrite('sorted/%d.png' % i, img) | |
def sort(filenames): | |
files = read(filenames) | |
files = sort_by_black_count(files) | |
write(files) | |
if __name__ == '__main__': | |
filenames = sys.argv[1:] | |
sort(filenames) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment