I recently listed to the song I'm a Textpert about texting while driving and one particular line in the song inspired this challenge.

I never stoop so low as to text photos / Instead I text the correct combo of ones and zeros

Now obviously, this would be an almost impossible thing to do in ones head and so you should help textperts world wide by coding a program to do the same thing.

Your job is to take the name of an image file with the dimensions 500x500 pixels and output the file as binary. You may also take an image as input, if you language supports it.

You may use image files with the extensions

  • .jpg
  • .png
  • .gif
  • .bmp
  • .svg

Allow me to demonstrate how to convert an image into binary, for those who don't understand what I mean.

Step 1

Take the first pixel in the file and retrieve its RGB value as 3 decimal numbers: R, G and B. Let's assume, for example, that the first pixel is 255,0,0 (red).

Step 2

Convert each number into its 8-bit binary representation and concatenate into a single 24-bit number. For the example, this yields 111111110000000000000000

Step 3

Add this 24-bit number to the final output. Therefore, the final output should be 6000000 digits long and should consist of entirely 1 and 0

This is a so shortest code wins!

  • 1
    \$\begingroup\$ Can you please specify the input. Is the filename to work on the only possible input or can languages which allow images as input use that? \$\endgroup\$
    – Ian Miller
    Commented Jun 11, 2017 at 7:00
  • \$\begingroup\$ What kind of image formats need to be supported? Can I choose (or invent) an image format? \$\endgroup\$
    – L3viathan
    Commented Jun 11, 2017 at 7:51
  • \$\begingroup\$ Additionally for output do you want it to output the ones and zeros to standard output (does this need to be a string or can it be a array of integers) or actually create a new file with the data in it? \$\endgroup\$
    – Ian Miller
    Commented Jun 11, 2017 at 8:45
  • \$\begingroup\$ @IanMiller output can be any accepted method here and input can either be a file name or an image, if you support that. \$\endgroup\$ Commented Jun 11, 2017 at 9:43
  • \$\begingroup\$ Do you have any testcases? \$\endgroup\$ Commented Jun 11, 2017 at 9:59

5 Answers 5


Pyth, 14 13 bytes

-1 byte thanks to Erik the Outgolfer (z can be removed for implicit input).




            '    # Open the path/URL given as implicit input. Return list of color triples for color images: [(255, 125, 23), ...]
          .n     # Flatten the list
 m     .Bd       # For each element of the list, convert to binary
  .[\08          # Pad with zeros on the left up to 8 characters
s                # Concatenate the list of bytes

Unfortunately, because of safety reasons, that cannot be tested with the online interpreter; you will have to clone Pyth to test it on your own computer.

  • 1
    \$\begingroup\$ You can assume quoted input and remove z. \$\endgroup\$ Commented Jun 11, 2017 at 10:07

MATL, 13 12 bytes


Accepts a filename as a string as input. The output 24-bit numbers are displayed in row-major order.

Unfortunately this doesn't work in the online interpreters for security reasons. Here is a screenshot of it working on my local machine

enter image description here

Here is a slightly modified version in which I manually create a 2 x 2 x 3 image and then use (almost) the same code to process it.


        % Implicitly grab input as a string
Yi      % Read in as an M x N x 3 image
H&!     % Permute the dimensions of the image to be 3 x N x M
8&YB    % Convert to a binary string using 8 bits for each element
!       % Transpose the result
1e      % Reshape to a row vector
        % Implicitly display the result

Mathematica, 103 bytes


takes an image x.bmp (or any other) and converts it to x.txt file

  • \$\begingroup\$ Instead of PadLeft, you could use the third argument of IntegerDigits (i.e. IntegerDigits[#,2,8]& \$\endgroup\$ Commented Jun 11, 2017 at 3:13
  • \$\begingroup\$ you are absolutely right! I had forgotten I could use this! \$\endgroup\$
    – ZaMoC
    Commented Jun 11, 2017 at 7:20
  • \$\begingroup\$ The only mistake was the missing "Import[]" because I was testing it with an image.Everything works fine now. Are you testing the updated code (?) cause I don't use PadLeft \$\endgroup\$
    – ZaMoC
    Commented Jun 11, 2017 at 10:00
  • \$\begingroup\$ Oops, I hadn't refreshed so I was looking at an old version. \$\endgroup\$
    – Ian Miller
    Commented Jun 11, 2017 at 10:24
  • \$\begingroup\$ Based on the rules you could save a bunch of bytes by writing it as a function which inputs an image and outputs to standard output rather than using Import`Export`. \$\endgroup\$
    – Ian Miller
    Commented Jun 11, 2017 at 10:47

C#, 309 bytes

My second code golf, if you can't tell. Takes an image x.jpg...

using System;using System.Drawing;using System.Linq;class Program{static void Main(string[]a){Bitmap b=(Bitmap)Image.FromFile("x.jpg");Console.WriteLine(string.Join("",from i in Enumerable.Range(0,250000)select Convert.ToString(Convert.ToInt32(b.GetPixel((int)Math.Floor((double)i/500),i%500).Name,16),2)));}}
  • \$\begingroup\$ Nice attempt and welcome to the site! I don't know C# so can't give you any golfs but I'm sure others will! \$\endgroup\$ Commented Jun 11, 2017 at 13:11

Octave, 46 bytes


Creates an anonymous function named ans that can be called with either a filename (ans('file.png')) or a URL (ans('http://image.png'))


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.