2017-09-23, post № 180

PIL, programming, Python, #color model, #hsl, #hsv, #image filter, #images, #nature, #photography, #rainbow

To digitally represent colors, one most often uses the RGB color system. By combining three fundamental light colors in certain ways, one can define a variety of different wavelengths of light. The human eye has three distinct photoreceptors for the aforementioned three colors, nearly all screens use pixels consisting of three parts in those colors and most image formats store the image data in the RGB color system.

Honey bee (original)

However, there are other color systems than RGB with other strengths. Cycling through the colors of the rainbow, for example, is a lot easier using the HSL (or HSV) color model, as it is simply controlled by the hue.

Fruit (original)

Rainbowify uses the HSL color model to rainbowify a given image. To do so, the image is first converted into a grayscale image (averaging all three color channels). A pixel’s brightness is then interpreted as its hue with its saturation and lightness set to the maximum. As a final touch, the hue gets offset by a pixel-position dependent amount to create the overall appearance of a rainbow.
Source code is listed below and can also be downloaded.

Sunflower (original)
Thistle (original)
Source code: rainbowify.py
Extra assets: rainbowify-120.jpg, rainbowify-120_rainbowified.jpg, rainbowify-121.jpg, rainbowify-121_rainbowified.jpg, rainbowify-122.jpg, rainbowify-122_rainbowified.jpg, rainbowify-125.jpg, rainbowify-125_rainbowified.jpg, rainbowify-126.jpg, rainbowify-126_rainbowified.jpg, rainbowify-127.jpg, rainbowify-127_rainbowified.jpg, rainbowify-128.jpg, rainbowify-128_rainbowified.jpg, rainbowify-129.jpg, rainbowify-129_rainbowified.jpg, rainbowify-130.jpg, rainbowify-130_rainbowified.jpg, rainbowify-131.jpg, rainbowify-131_rainbowified.jpg, rainbowify-132.jpg, rainbowify-132_rainbowified.jpg, rainbowify-133.jpg, rainbowify-133_rainbowified.jpg, rainbowify-134.jpg, rainbowify-134_rainbowified.jpg, rainbowify-135.jpg, rainbowify-135_rainbowified.jpg, rainbowify-136.jpg, rainbowify-136_rainbowified.jpg, rainbowify-138.jpg, rainbowify-138_rainbowified.jpg, rainbowify-139.jpg, rainbowify-139_rainbowified.jpg, rainbowify-140.jpg, rainbowify-140_rainbowified.jpg, rainbowify-141.jpg, rainbowify-141_rainbowified.jpg, rainbowify-142.jpg, rainbowify-142_rainbowified.jpg, rainbowify-143.jpg, rainbowify-143_rainbowified.jpg, rainbowify-144.jpg, rainbowify-144_rainbowified.jpg, rainbowify-145.jpg, rainbowify-145_rainbowified.jpg, rainbowify-146.jpg, rainbowify-146_rainbowified.jpg, rainbowify-147.jpg, rainbowify-147_rainbowified.jpg, rainbowify-148.jpg, rainbowify-148_rainbowified.jpg, rainbowify-149.jpg, rainbowify-149_rainbowified.jpg, rainbowify-150.jpg, rainbowify-150_rainbowified.jpg, rainbowify-151.jpg, rainbowify-151_rainbowified.jpg, rainbowify-152.jpg, rainbowify-152_rainbowified.jpg, rainbowify-153.jpg, rainbowify-153_rainbowified.jpg, rainbowify-155.jpg, rainbowify-155_rainbowified.jpg, rainbowify-156.jpg, rainbowify-156_rainbowified.jpg, rainbowify-157.jpg, rainbowify-157_rainbowified.jpg, rainbowify-158.jpg, rainbowify-158_rainbowified.jpg, rainbowify-159.jpg, rainbowify-159_rainbowified.jpg

Arithmetic Golfing

2017-09-09, post № 179

code golf, mathematics, programming, Python, #byte count, #code golfing, #golfing, #short code

A recent PCG golfing question When do I get my sandwich? asked to find a mapping between seven input strings (sandwich names) and the seven days of the week (indexed by number).

The first answer was made by a user named i cri everytim and utilized a string of characters which uniquely appear at the same position in all seven input strings, enklact, to perform the mapping in Python 2 requiring 𝟤𝟫 bytes. After their answer, a lot of answers appeared using the same magic string in different languages to reduce the number of bytes needed. Yet nobody reduced the byte count in Python.

Trying to solve the problem on my own, my first attempt was using only the input strings’ last decimal digit to perform the mapping, though this approach did not save on bytes (read my PCG answer for more on this 𝟥𝟢 byte solution).

After a few more hours of working on this problem, however, I achieved to bring down the byte count by one entire byte.

I did so by using a simple brute-force algorithm to check for Python expressions which can be used to perform the sought after mapping. To do so, I use Python’s apostrophes (`...`) to turn the found expression into a string — str(...) is three whole bytes longer — and index that string with the input strings’ lengths. It sure is not very readable, but only takes 𝟤𝟪 bytes — and that is all that matters.

lambda S:`6793**164`[len(S)]

After finding the 𝟤𝟪 byte function which uses a 𝟫 byte expression (6793**164), I attempted to find an even shorter expression. And even though I did not yet find one, I did write a more general brute-force Python program (source code shown below; can also be downloaded) than the one I linked to in my PCG answer.

Brute-forcing takes exponentially more time the more digits you have to check, so my brute-forcer still requires the user to decide for themselves which expressions should be tried.
There are three parameters that define the search; a regex pattern that should be contained in the expression’s string, an offset that pattern should ideally have and a target length. If an expression is found that takes as many bytes as or less bytes than the target length, an exclamation point is printed
Though this program did not prove useful in this case, there may come another challenge where an arithmetic expression golfer could come in handy.

My program may not have found shorter expressions, but definitely some impressive ones (the +... at the end refers to an additional offset from the string index which — unsurprisingly — take additional bytes):

I also considered using division to generate long strings of digits which may match; the only problem is that Python floating-point numbers only have a certain precision which does not produce long enough strings. Again, using exponentiation (**) and bitshifting (<<) I could not come up with a working expression that takes less bytes.

Source code: arithmetic-golfing_brute.py

brainfuck X

2017-08-26, post № 178

brainfuck, programming, Python, #ANSI, #argparse, #braindraw, #color, #esoteric, #interpreter, #PPM, #tape, #Turing machine

While browsing StackExchange PCG [1] questions and answers, I came across a challenge regarding drawing the swiss flag. In particular, I was interested in benzene’s answer, in which they showcased a brainfuck dialect capable of creating two-dimensional 𝟤𝟦-bit color images. In this post I present this dialect with slight changes of my own, as well as an interpreter I wrote in Python 2.7 (source code is listed below and can also be downloaded).


Urban Müller’s original brainfuck (my vanilla brainfuck post can be found here) works similar to a Turing machine, in that the memory consists of a theoretically infinitely large tape with individual cells which can be modified. What allows brainfuck X (or braindraw, as benzene called their dialect) to create color images is, that instead of a one-dimensional tape, a three-dimensional tape is used. This tape extends infinitely in two spacial dimensions and has three color planes. Each cell’s value is limited to a byte (an integer value from 𝟢 to 𝟤𝟧𝟧) which results in a 𝟤𝟦-bit color depth.

Adding to brainfuck’s eight commands (+-<>[].,), there are two characters to move up and down the tape (^v) and one character to move forwards in the color dimension (*). Starting on the red color plane, continuing with the green and ending in the blue. After the blue color plane, the color planes cycle and the red color plane is selected. benzene’s original language design which I altered slightly had three characters (rgb) to directly select a color plane. Whilst this version is supported by my interpreter )the flag --colorletters is necessary for that functionality(, I find my color star more brainfucky — directly calling color planes by their name seems nearly readable.
brainfuck’s vanilla eight characters still work in the same way, brainfuck X can thereby execute any vanilla brainfuck program [2]. Also, there still is a plaintext output — the tape’s image is a program’s secondary output.

Having executed the final brainfuck instruction, the interpreter prints out the tape to the terminal — using ANSI escape codes. Because of this, the color depth is truncated in the terminal view, as there are only 𝟤𝟣𝟨 colors supported. [3]
For the full 𝟤𝟦-bit color depth output, I use the highly inefficient Portable Pixmap Format (.ppm) as an output image file format. To open .ppm files, I recommend using the GNU Image Manipulation Program; specifying the output file name is done via the --output flag.

The Swiss flag image above was generated by benzene’s braindraw code (see their StackExchange answer linked to above); the resulting .ppm file was then scaled and converted using GIMP.
Interpreter command: python brainfuckx.py swiss.bfx -l -o swiss.ppm


  • Being written in pure Python, the interpreter is completely controlled via the command line. The basic usage is python brainfuck-x.py <source code file>; by using certain flags the functionality can be altered.
  • --input <input string>, -i <input string> specifies brainfuck’s input and is given as a byte stream (string).
  • --simplify, -s outputs the source code’s simplified version; the source code with all unnecessary characters removed.
  • --colorstar selects the color star color plane change model which is the default.
  • --colorletters, -l selects the color letter color plane change model.
  • --silent stops the interpreter from outputting warnings, infos and the final tape.
  • --maxcycles <cycles>, -m <cycles> defines the maximum number of cycles the brainfuck program can run; the default is one million.
  • --watch, -w allows the user to watch the program’s execution.
  • --watchdelay <delay> defines the time in seconds the interpreter sleeps between each watch frame.
  • --watchskip <N> tells the interpreter to only show every 𝑁th cycle of the execution.
  • --output <output file name>, -o <output file name> saves the final tape as a .ppm image file.
Source code: brainfuck-x.py
Jonathan Frech's blog; built 2021/04/16 21:21:49 CEST