Skip to content

Instantly share code, notes, and snippets.

@Vanuan
Created September 17, 2013 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Vanuan/6597932 to your computer and use it in GitHub Desktop.
Save Vanuan/6597932 to your computer and use it in GitHub Desktop.
Sorts images by a number of non-white pixels
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