1

For the purpose of demonstrating a handwritten digit recognition system, I want to be able to draw digits with the mouse in a Python 3 application. The drawings must end up as gray scale raster images in a 28-by-28 Numpy array.

Since the digits must resemble the digits from the MNIST dataset and therefore can't be aliased, it's probably best if I can draw with thick strokes on a large canvas, read the image into a Numpy array and then downsample it.

How can I accomplish this?

Edit: Initially, I considered using a Tkinter canvas to do the drawing, but there seems to be no way to get a rasterized version of a Tkinter canvas drawing.

4
  • You can take a look at pygame to make tha drawing tool. and saving to an image so you can work with it. Commented Jul 29, 2017 at 0:56
  • @GustavoMagalhães Looks like your comment came seconds before my answer to my own question! :P Maybe pygame would have been simpler than my solution but now I have a solution using PySide. Thanks, anyway. Commented Jul 29, 2017 at 0:59
  • If your problem was resolved, can you please share the answer with us. I am also struggling with the same issue. Thanks in advance. Commented Apr 2, 2018 at 7:19
  • @abhilash_goyal Did you check out my answer below? Commented Apr 3, 2018 at 11:12

1 Answer 1

0

Ok, so I clearly wasn't able to solve this with Tkinter.

However, I found this question. It turns out that this is possible with Qt by using the Python bindings provided by PySide (PyQt would probably also have worked). The class QImage contains a method called constBits which returns a memory view of the pixel data. This can then be turned into a Numpy array using numpy.asarray.

While a QImage instance isn't a widget object and hence is never itself rendered by Qt, it can be drawn to an ordinary QWidget object. If then QWidget is subclassed, the methods mousePressEvent, mouseMoveEvent and mouseReleaseEvent can be overridden in the subclass and made to draw for example a QPainterPath in the QImage object, as done here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.