Your goal is to write a full program or function that takes 5 positive integers and a string with an image filename as input [X1,Y1], [X2,Y2], N, image.jpg in any reasonable format, where:

  • First couple [X1,Y1] are the top left X,Y coordinates (<=20) of the blue rectangle area in the example picture (16,11).
  • Second couple [X2,Y2] are the bottom right X,Y coordinates (<=20) of the blue rectangle area in the example picture (20,22).
  • Fifth number N such 0 <= N <= 9 is the number that has to be found.
  • Sixth parameter is a string with the filename of this JPG picture.

Output how many of N's are present in the indicated area (outlined in blue in the picture for this example):

enter image description here

Click here to obtain a bigger clean version of the same image.

In the picture there are 500 numbers from 0 to 9 (included) arranged in 20 columnns per 25 rows, in monospaced Courier New font. Each different number has a different color (you can take advantage of this fact or ignore it and consider or convert the image in monocrome if that helps you).

Test cases:

[4,4],[13,5],1,image.jpg    > 4  (outlined in red)
[4,4],[13,5],4,image.jpg    > 2  (outlined in red)
[17,5],[17,5],2,image.jpg   > 1  (outlined in magenta)
[17,5],[17,5],9,image.jpg   > 0  (outlined in magenta)
[20,8],[20,9],3,image.jpg   > 1  (outlined in cyan)
[20,8],[20,9],2,image.jpg   > 0  (outlined in cyan)
[16,11],[20,22],0,image.jpg > 8  (outlined in blue)
[16,11],[20,22],3,image.jpg > 9  (outlined in blue)
[3,14],[11,20],7,image.jpg  > 6  (outlined in green)
[3,14],[11,20],5,image.jpg  > 6  (outlined in green)
[3,14],[11,20],8,image.jpg  > 8  (outlined in green)
[1,1],[20,25],0,image.jpg   > 47 (whole picture)
[1,1],[20,25],8,image.jpg   > 50 (whole picture)
[1,1],[20,25],1,image.jpg   > 55 (whole picture)


  • This is so the shortest code wins.
  • You must take as input this JPG file.
  • You are not allowed to hardcode the matrix of numbers represented in the JPG, or the results.
  • Standard loopholes are forbidden.

Mathematica, 92 bytes


Unnamed function taking the arguments in this format: [{X1,X2}, {Y1,Y2}, N, "image.jpg"]. (Indeed, the fourth argument can be either the local file name or the URL http://i67.tinypic.com/6qh5lj.jpg.)

Import@#4 imports the image file, Binarize[...,.9] darkens all of the numbers to black, and TextRecognize (the function clearly doing the heavy lifting here!) extracts a multi-line string from the resulting image, which is split into a nested list of characters with Characters@.

Take[...,#2,#] keeps only the characters corresponding to the outlined rectangle, and Count[...,ToString@#3,2] counts the number of occurrences of N in the result.


Python 3 + pillow + pytesseract, 239 bytes

from PIL.Image import*
from pytesseract import*
def c(a,b,n,f):w,h=b[0]-a[0]+1,b[1]-a[1]+1;return len([1for i in range(h*w)if image_to_string(open(f).convert('L').point(lambda x:[9,0][x<250],'1')).split()[i//w+a[1]-1][i%w+a[0]-1]==str(n)])

This is horribly inefficient as for each number tile, the whole file is parsed. The much faster and slightly longer 243 bytes solution would be

from PIL.Image import*
from pytesseract import*
def c(a,b,n,f):s=image_to_string(open(f).convert('L').point(lambda x:[9,0][x<250],'1')).split();w,h=b[0]-a[0]+1,b[1]-a[1]+1;return len([1for i in range(h*w)if s[i//w+a[1]-1][i%w+a[0]-1]==str(n)])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.