Martin Krzywinski / Genome Sciences Center / Martin Krzywinski / Genome Sciences Center / - contact me Martin Krzywinski / Genome Sciences Center / on Twitter Martin Krzywinski / Genome Sciences Center / - Lumondo Photography Martin Krzywinski / Genome Sciences Center / - Pi Art Martin Krzywinski / Genome Sciences Center / - Hilbertonians - Creatures on the Hilbert Curve
Love itself became the object of her love.Jonathan Safran Foercount sadnesses

Bioinformatics and Genome Analysis Course. Izmir International Biomedicine and Genome Institute, Izmir, Turkey. May 2–14, 2016

visualization + design

ASCII Art—Proportional Spacing, Tone/Structure Mapping and Fixed Strings


  1. ASCII art
  2. proportional spaced fonts
  3. structural character selection
  4. tone-based character selection
  5. fixed string ASCII art
  6. angled text ASCII art
  7. multi-layer ASCII art
  8. recursive ASCII art

download code


This is a Perl script and requires Imager. See README in the archive for instructions. I cannot provide installation support, but welcome questions and ideas about the method.


Martin Krzywinski @MKrzywinski
Part of the Pioneer plaque rendered with the sequence of human chromosome 1, using 8 weights of Gotham. (zoom)
Martin Krzywinski @MKrzywinski
DNA helix rendered with string 'dna'. (zoom)

After finding a typographic portrait of Christopher Hitchens, created out of Gill Sans letters by Miles Chic at Capilano University, I thought to resurrect software I wrote a long time ago that converts images into letters and expanding traditional ASCII art by using

  • proportionally spaced fonts
  • a variety of font weights in a single image
  • both tone and structure of the image to select characters
  • fixed strings to render an image in legible text


The representation of images by characters—ASCII art—has a long history. ASCII art extends the emoticon (or smiley) to represent a larger piece of work. Typically, the works use a fixed-space font (e.g. Courier), originally designed for display on a terminal. Despite the sophistication of computer graphics today, ASCII art continues to have a strong following with new work continually added to public online galleries.

Martin Krzywinski @MKrzywinski
Community contributions to ASCII Art Museum. ASCII art can vary from simple cartoon-like depictions to photorealistic interpretations. (source, zoom)

Photos and paintings can be ASCIIfied using a tone-based approach and automated methods exist to do this (Paul D. O’Grady and Scott T. Rickard (2008) Automatic ASCII Art Conversion of Binary Images Using Non-Negative Constraints).

Many artists generate new creations, exclusive to the medium. Typically this kind of ASCII art is based on the interpretation of structure rather than tone—this method has also been automated (Xuemiao Xu, Linling Zhang, Tien-Tsin Wong (2010) Structure-based ASCII Art).

Proportional spaced and multi-font art

I have written code to generate ASCII art from images by using proportional spaced fonts.

Martin Krzywinski @MKrzywinski
Fixed width fonts (e.g. Pragmata) are popular. ASCII art can be extended to proportionally spaced fonts (e.g. Gotham). More than one weight (or font) can be used to add contrast.

Below is an example of how Pragmata and Gotham can be used to different effect to render an image. When a proportional spaced font is used, the ASCII shape can more fully fill the image.

Martin Krzywinski @MKrzywinski
Comparison of fixed and proportional spaced fonts in ASCII art. Employing multiple weights adds contrast. The grey background is added to emphasize the original image. (zoom)

Let's see how these methods work on a real image. Many ASCII art Mona Lisa versions exist. Below, I render the Mona Lisa with Pragmata, Gotham Book and 8 weights of Gotham.

structural character selection

Two-tone shapes like the S in the figure above require selecting characters that match the structure of the image. (e.g. "|" matches vertical lines). For a given character and image position there are four distinct match possibilities—a combination of whether the character and image have a signal at a position. I show this in the figure below.

Martin Krzywinski @MKrzywinski
Finding the best character involves maximizing overlap (s1, s3) and minimizing penalty (s2, s4).

By maximizing scores derived from matches (s1, s3) and minimizing any penalties (s2, s4), a character is identified based on maximal coverage of the image region and minimum coverage of areas that are blank.

Martin Krzywinski @MKrzywinski
Ink artwork, or thresholded bitmaps in which there are only two tone values, are approximated using structural matching. Here I compare the method of O'Grady and Rickard with my naive structural matching. (zoom)

When proportional text is used, edges are better approximated, such as in the Homer Simpson example below which uses Gotham Book.

Martin Krzywinski @MKrzywinski
For this image, 17pt text matches the detail well. (zoom)

tone-based character selection

Images that are not two-tone require that we match both structure and tone. Structure is approximated by the choice of character, while tone by choice of font weight. To select the best character based on tone, the character's average tone is compared to the average tone of the section of the image to which it is being compared.

Martin Krzywinski @MKrzywinski
Heavier weights are used to match dark areas of the image. (zoom)

It is possible to combine both structure and tone metrics in character selection. Below is an example of how an image with both tone and structure is interpreted as the tone and structure score weights are varied. The balance between these two metrics can be very hard to find—it greatly depends on the image. Tone-based mapping works well when font size is small and the image is viewed from larger distance—in this case, characters play the role of individual pixels with varying brightness. Structure-based mapping works with larger type and closer viewing distance.

Martin Krzywinski @MKrzywinski
A tone:structure ratio of 1:0.5 works well for the Star Trek logo. (zoom)

Continuous tone bitmaps are an idea application of multi-font ASCII art—images no longer need to be thresholded or dithered.

Martin Krzywinski @MKrzywinski
Applying both tone and structure character selection metrics to a greyscale image. (source, zoom, )

fixed string ASCII art

ASCII art is generated by dividing the image into a grid and finding the letter (the choice of characters is often expanded to include punctuation) that best matches the grid section. Typically, for each grid the entire set of allowable characters is sampled. Instead, we can limit the choice of character by successively sampling from a fixed string.

Martin Krzywinski @MKrzywinski
Fixed string ASCII art limits the choice of characters available at each grid. Characters can be drawn from a short string (e.g. 'ilovegotham') or from a larger corpus (e.g. Wikipedia entry for Mona Lisa). The string can be contiguous within the image, or locally within the font. (zoom)

rendered with the fixed string "monalisa" using 8 weights of Gotham.

Martin Krzywinski @MKrzywinski
Fixed string ASCII art rendering of Mona Lisa. (zoom)
Martin Krzywinski @MKrzywinski
DNA helix rendered with string 'dna'. (zoom)
Martin Krzywinski @MKrzywinski
DNA helix rendered with sequence from human chromosome 1. (zoom)

angled text ASCII art

Things get even more interesting when the text is angled.

Martin Krzywinski @MKrzywinski
By applying rotations to the input and output images, the image can be approximated by angled text. (zoom)

multi-layer ASCII art

The image can be textured with multiple layers of ASCII art. In the example below, four layers of text are used, each with a different font size.

Martin Krzywinski @MKrzywinski
Part of the Pioneer plaque rendered with the sequence of human chromosome 1, using 4 layers of sizes (17pt, 33pt, 59pt and 93pt) and 8 weights of Gotham. (zoom)

Instead of varying size, the angle of the text can be changed among layers. This results in a pattern reminiscent of a halftone.

Martin Krzywinski @MKrzywinski
Part of the Pioneer plaque rendered with the sequence of human chromosome 1, using 4 layers with different text rotation (-45, -15, 15, 45 degrees) and 8 weights of Gotham. (zoom)

recursive ASCII art

An image can be asciified several times, with each iteration the asciified output of the previous step used as input for the next. At each step, the font size should be reduced to s → √s.

Martin Krzywinski @MKrzywinski
ASCII, set in Gotham Ultra (zoom)
Martin Krzywinski @MKrzywinski
The above image asciified using 8 weights of 105pt Gotham and the fixed string 'ASCII'. (zoom)
Martin Krzywinski @MKrzywinski
The asciified example above, asciified again using 8 weights of 11pt Gotham and the fixed string 'ASCII'. (zoom)

news + thoughts

Association, correlation and causation

Thu 01-10-2015

Correlation implies association, but not causation. Conversely, causation implies association, but not correlation.

This month, we distinguish between association, correlation and causation.

Association, also called dependence, is a very general relationship: one variable provides information about the other. Correlation, on the other hand, is a specific kind of association: an increasing or decreasing trend. Not all associations are correlations. Moreover, causality can be connected only to association.

Martin Krzywinski @MKrzywinski
Nature Methods Points of Significance column: Association, correlation and causation. (read)

We discuss how correlation can be quantified using correlation coefficients (Pearson, Spearman) and show how spurious corrlations can arise in random data as well as very large independent data sets. For example, per capita cheese consumption is correlated with the number of people who died by becoming tangled in bedsheets.

...more about the Points of Significance column

Bayesian networks

Thu 01-10-2015

For making probabilistic inferences, a graph is worth a thousand words.

This month we continue with the theme of Bayesian statistics and look at Bayesian networks, which combine network analysis with Bayesian statistics.

In a Bayesian network, nodes represent entities, such as genes, and the influence that one gene has over another is represented by a edge and probability table (or function). Bayes' Theorem is used to calculate the probability of a state for any entity.

Martin Krzywinski @MKrzywinski
Nature Methods Points of Significance column: Bayesian networks. (read)

In our previous columns about Bayesian statistics, we saw how new information (likelihood) can be incorporated into the probability model (prior) to update our belief of the state of the system (posterior). In the context of a Bayesian network, relationships called conditional dependencies can arise between nodes when information is added to the network. Using a small gene regulation network we show how these dependencies may connect nodes along different paths.

Background reading

Puga, J.L, Krzywinski, M. & Altman, N. (2015) Points of Significance: Bayesian Statistics Nature Methods 12:277-278.

Puga, J.L, Krzywinski, M. & Altman, N. (2015) Points of Significance: Bayes' Theorem Nature Methods 12:277-278.

...more about the Points of Significance column

Unentangling complex plots

Fri 10-07-2015

The Points of Significance column is on vacation this month.

Meanwhile, we're showing you how to manage small multiple plots in the Points of View column Unentangling Complex Plots.

Martin Krzywinski @MKrzywinski
Nature Methods Points of View column: Unentangling complex plots. (download, more about Points of View)

Data in small multiples can vary in range, noise level and trend. Gregor McInerny and myself show you how you can deal with this by cropped and scaling the multiples to a different range to emphasize relative changes while preserving the context of the full data range to show absolute changes.

McInerny, G. & Krzywinski, M. (2015) Points of View: Unentangling complex plots. Nature Methods 12:591.

...more about the Points of View column

Fixing Jurassic World science visualizations

Fri 10-07-2015

The Jurassic World Creation Lab webpage shows you how one might create a dinosaur from a sample of DNA. First extract, sequence, assemble and fill in the gaps in the DNA and then incubate in an egg and wait.

Martin Krzywinski @MKrzywinski
We can't get dinosaur genomics right, but we can get it less wrong. (a) Corn genome used in Jurassic World Creation Lab website. Image is from the Science publication B73 Maize Genome: Complexity, Diversity, and Dynamics. Photo and composite by Universal Studios and Amblin Entertainment. (b) Random data on 8 chromosomes from chicken genome resized to triceratops genome size (3.2 Gb). Image by Martin Krzywinski. (c) Actual genome data for lizard genome, UCSC anoCar2.0, May 2010. Image by Martin Krzywinski. Triceratops outline in (b,c) from wikipedia.

With enough time, you'll grow your own brand new dinosaur. Or a stalk of corn ... with more teeth.

What went wrong? Let me explain.

Martin Krzywinski @MKrzywinski
Corn World: Teeth on the Cob.