📜 ⬆️ ⬇️

Automated xkcd-style diagram creation: from serious to funny


Translation of Vitaly Kaurov's post " Automating xkcd Diagrams: Transforming Serious to Funny ".
Download the file containing the text of the article, interactive models and all the code given in the article here .
I express my deep gratitude to Kirill Guzenko for his help in translating.
On Monday morning, I came across an interesting question, published in the Mathematica Stack Exchange , with a simple title - " creating graphs in xkcd-style ." Due to the popularity of xkcd web comics by Randall Munroe, I expected people to add a few bookmarks to this page and a dozen up-vote. Then I did not know how everything will turn out. It is difficult to predict the virality of a meme, but if you managed to create one, then it’s great to see how its popularity grows and how it spreads on the Internet. Two days later, this post had already more than 100 thousand views, two hundred up-vote and 150 bookmarks; Answers and similar posts appeared in other sections of the Stack Exchange, a small hurricane broke out on Twitter about this, discussions in Hacker News and reddit appeared . Here I will provide the original Amatya post with an example of an xkcd-style image:

“I received an email that I wanted to answer with the xkcd-style schedule, but I couldn't handle it. Everything I painted looked right, but I could not think of such a command for Plot Legends to make text fragments floating. Maybe there are some ideas, how could you create xkcd-style graphics? When everything looks drawn and inaccurate. I think drawing such strange curves in Mathematica should be difficult to implement. ”

Walking back to my front door at night


However, it turns out that drawing such curved lines in Mathematica is as easy as it is fun. Mathematica Stack Exchange is a fast-growing community organized in a question-answer format, which is developed exclusively by the efforts of the users themselves. I was glad to see that he had drawn so much attention to this intriguing question and received so many beautiful and thoughtful answers. I recommend visiting the original post to see all the answers. Here I will give only one of the solutions that Simon Woods has brought, which is more or less universal and applicable to all types of graphs in Mathematica , and even to images.
')
Simon started by specifying the font that appears in the xkcd () comics.



The next step was to set such a function, which would convert the displayed font into a stylized xkcd. For a given point on the graph and the offset for the text, using the spline, the indicated points are connected by a smooth curve.



Now it's time to set the rules, which for some graphic object will replace the text style with the xkcd-style we have defined, as well as change the line style. Notice how in the sixth line of the code lines are replaced with bold black, which are drawn over the original, but somewhat wider white lines. This is necessary in order to set a noticeable gap on one curve when intersecting with another — a characteristic feature of hand-drawn cartoons.



The following function applies the above rules and makes axes thicker.





Finally, the time has come for the part responsible for image processing - in my opinion, the most innovative and interesting part of Simon’s solution. The reason is the possibility of working independently of the rest of the code and with arbitrary images, and not only with Mathematica graphics. As we will see later, this gives us the opportunity to apply the xkcd-style to any chart. The purpose of the function below is to slightly distort the lines in the image in order to simulate the roughness, which is obtained by hand-drawing.



First, the function rasterizes the Mathematica graphics, converting it into an image. Then it creates two random images and smoothes them with a convolution with a Gaussian core. Randomly, but smoothly varying values ​​of each pixel of these images are the basis for determining the pixel displacements in the original image. Pixel offset is set by the ImageTransformation function. The latest feature applies xkcd styles and distorts images.



Let's try it out on several graphs. Here is the first example that Simon gives, showing how it all works together.





And here are some other examples of 2D and 3D graphics that we got using xkcdConvert , from a wide variety of Mathematica graphics features. It should be noted that xkcdConvert can be applied both to function names and to the results of graphic functions. That is, you can first create some 3D graphics, rotate it, zoom in, pan, and only then, to what happened, use xkcdConvert .



It is worth noting that Jon McLoone several years ago wrote in a post " Self-Description " about automating the creation of images in the xkcd-style. There, he showed how to create xkcd-style diagrams and add different colors. Now we can add “curvature” caused by manual writing:





I guess everything looks pretty authentic. Great job, Simon! I highly recommend looking at the code samples and other solutions that are given in the original post. Notice how the xkcdConvert function can be applied to the image directly. However, we have no opportunity to replace the fonts with “funny” ones, as the fonts are also in raster form. However, the effect is quite realistic.

Yu-Sung Chang had a great idea to create a CDF application that takes an image by reference and styles it with xkcd. Below we use an idea that is slightly different from that of Simon. In our case, the randomized BSplineFunction function serves as a guide to displace ImageTransformation pixels. It also adds the ability to adjust the amplitude and frequency. Try inserting a link to a graphic image and just press Enter. If you want, you can invert the colors, as well as you can copy the result to the clipboard to save the image on your computer.


( download CDF-manipulator)

The question arises - how can a product that is used by professionals in various industries and researchers in their scientific work can so effectively solve such unusual tasks, including tasks in the field of art, creative directions and non-standard approaches. The answer is integration. Mathematica presents a wide range of technologies and algorithms that are available "right out of the box." They are designed very carefully and consistently, so that users can assemble their programs from the components of the LEGO designer. Let's see which areas we touched on: graphics, rules and patterns, image processing, import and export, network operations, dynamics and interactivity, the creation of interfaces, the deployment and use of CDF. So I hope that our readers, when next time they will work in Mathematica , when they solve equations, build graphs - they will remember that practically limitless possibilities for improvisation can take them far away from everyday tasks.

Note


Few people know that Wolfram | Alpha can produce xkcd-style results. To do this, at the beginning of the request simply indicate the word " handwritten "

A few examples:

Query " handwritten habrahabr "



Request " handwritten Runge-Kutta method, dy / dx = -2xy, y (0) = 2, from 1 to 3, h = .25 "



Query " handwritten radioactive elements "

Source: https://habr.com/ru/post/260017/


All Articles