PIL Image to CV2 image

Bands need to be reversed for CV2. PIL is RGB. CV2 is BGR.
Also, if you are getting errors with CV2, it may be because it doesn’t like transformed ‘views’ of arrays. Use the copy method.

# PIL RGB 'im' to CV2 BGR 'imcv'
imcv = np.asarray(im)[:,:,::-1].copy()
# Or
imcv = cv2.cvtColor(np.asarray(im), cv2.COLOR_RGB2BGR)
# To gray image
imcv = np.asarray(im.convert('L'))
# Or
imcv = cv2.cvtColor(np.asarray(im), cv2.COLOR_RGB2GRAY)
view raw gistfile1.py hosted with ❤ by GitHub

PIL and CV2 use the same percentages of R,G, and B to make a greyscale image but the results may have a difference of 1 in many places due to rounding off. PIL uses integer division and CV2 uses floating point percentages.
PIL: GRAY = R * 299/1000 + G * 587/1000 + B * 114/1000
CV2: GRAY = R * 0.299 + G * 0.587 + B * 0.114

If the PIL image has four bands, only reverse the first three.

Sudoku game generator

I wrote a Sudoku game generator based on what I know from playing Sudoku.

it is on ActiveState.com http://goo.gl/FbDQ0

I think it’s possible that some games may have more than one solution. I don’t feel like working with it more, but one way to prevent multiple solutions is have the computer play the game a few times and throw out those that do have more than one solution. I would have to randomize the order in which play options are done. It takes me awhile to solve one by hand, so I haven’t tested it too much, but it’s good so far.