I text the correct combo of ones and zeros

9

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!

caird coinheringaahing

Posted 2017-06-10T21:31:40.737

Reputation: 13 702

1Can 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? – Ian Miller – 2017-06-11T07:00:42.187

What kind of image formats need to be supported? Can I choose (or invent) an image format? – L3viathan – 2017-06-11T07:51:37.960

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? – Ian Miller – 2017-06-11T08:45:57.623

@IanMiller output can be any accepted method here and input can either be a file name or an image, if you support that.

– caird coinheringaahing – 2017-06-11T09:43:00.367

Do you have any testcases? – Erik the Outgolfer – 2017-06-11T09:59:44.753

@EriktheOutgolfer no as I don't have a piece of code to do them for me. Feel free to edit any in if you want to though – caird coinheringaahing – 2017-06-11T10:05:27.017

Should the output be row major or column major? – Suever – 2017-06-11T14:08:48.533

@Suever either is fine – caird coinheringaahing – 2017-06-11T14:10:41.097

Answers

4

Pyth, 14 13 bytes

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

sm.[\08.Bd.n'

Explanations

sm.[\08.Bd.n'

            '    # 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.

Jim

Posted 2017-06-10T21:31:40.737

Reputation: 1 442

1You can assume quoted input and remove z. – Erik the Outgolfer – 2017-06-11T10:07:36.170

4

MATL, 13 12 bytes

YiH&!8&YB!1e

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.

Explanation

        % 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

Suever

Posted 2017-06-10T21:31:40.737

Reputation: 10 257

2

Mathematica, 103 bytes

F=Flatten;Export["x.txt",FromDigits@F[IntegerDigits[#,2,8]&/@F[ImageData[Import["x.bmp"],"Byte"],1],2]]


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

J42161217

Posted 2017-06-10T21:31:40.737

Reputation: 15 931

Instead of PadLeft, you could use the third argument of IntegerDigits (i.e. IntegerDigits[#,2,8]& – JungHwan Min – 2017-06-11T03:13:19.280

you are absolutely right! I had forgotten I could use this! – J42161217 – 2017-06-11T07:20:34.180

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 – J42161217 – 2017-06-11T10:00:05.757

Oops, I hadn't refreshed so I was looking at an old version. – Ian Miller – 2017-06-11T10:24:17.803

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`. – Ian Miller – 2017-06-11T10:47:22.703

2

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)));}}

Harry Wilkins

Posted 2017-06-10T21:31:40.737

Reputation: 51

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! – caird coinheringaahing – 2017-06-11T13:11:07.297

1

Octave, 46 bytes

@(x)dec2bin(permute(imread(x),[3,1,2]),8)'(:)'

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

Suever

Posted 2017-06-10T21:31:40.737

Reputation: 10 257