There's no doubt that one should always post the Mathematica code text rather than its screenshot when asking questions in this site, but every time I saw someone posting a screenshot, I can't help wondering if it's possible to recognize the code on a picture with Mathematica. Just as an example:

enter image description here

Can I OCR the line of code from the picture?

A direct use of TextRecognize is ineffective.

If it's not possible, what's the threshold here?

  • 2
    $\begingroup$ Mathematica uses Tesseract; its main advantage is the price, IMO. The line above suffers from not being in any language that Tesseract would recognize, because it applies a lot of heuristics (a/k/a guesswork). Obliqued fonts won't help, either. The killer is the presence of special characters; in your example, there is one for a delayed-evaluation replacement, and there might be two double square brackets for the Part[] extraction. $\endgroup$ Commented Sep 22, 2015 at 9:52
  • $\begingroup$ You might be able to solve your problem by extending the method that I gave in my answer to Applying TextRecognize on alpha-numerical table. $\endgroup$ Commented Sep 22, 2015 at 10:21
  • $\begingroup$ closely related: Open an html saved notebook $\endgroup$
    – Kuba
    Commented Sep 22, 2015 at 10:54
  • 2
    $\begingroup$ @FelixKasza given that the font is fixed and line orientation is not a problem, it would be very easy to train Tesseract or Ocropy to recognise Mathematica's font. One could get fancy and, if the quality is not great, do some guesswork and take the maximum likelihood version that the parser doesn't complain. $\endgroup$
    – Davidmh
    Commented Sep 22, 2015 at 14:20
  • $\begingroup$ True. But I see that Murta has already figured out the important part, how to set tesseract options when run through TextRecognize[]: mathematica.stackexchange.com/a/31851/12120 $\endgroup$ Commented Sep 22, 2015 at 18:53

1 Answer 1


One can do TextRecognize on screenshots if one increases the dpi (screenshots are usually somewhat like 72dpi - far too low for text recognition). I post this as an answer, because I do not know how to put images in a comment (sorry). In the picture you see increasing accuracy in recognizing the text. I did this in Photoshop, producing images of 300 and 600 dpi and left all the options on "automatic". textrecognition with increasing DPI

  • 1
    $\begingroup$ Your answer got my upvote because it is clearly correct, but I am personally too lazy to 'shop a screenshot just so I can OCR it (although I have done that an more to large peices of badly scanned old magazines). $\endgroup$ Commented Sep 22, 2015 at 18:47
  • 1
    $\begingroup$ @Felix Kasza: ... regarding the "badly scanned old magazines", the only thing I can say to this subject is: me, too! $\endgroup$
    – mgamer
    Commented Sep 22, 2015 at 19:36
  • $\begingroup$ Hmm…Is photoshop necessary here? $\endgroup$
    – xzczd
    Commented Sep 24, 2015 at 2:28
  • $\begingroup$ @xzczd: Hmmm. I´m not sure. My feeling is - it is not. I tried a lot with adjusting the image and filtering / sharpening before TextRecognize but did not get as good results as using PS. O.K. I´m working a lot with PS - photography is my hobby ;-) $\endgroup$
    – mgamer
    Commented Sep 25, 2015 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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