Output an image of your source code (not-quite-a-Quine)

29

4

Code golf: Print an image of the source code.

Requirements

  1. Must output the image of the source code itself, not a version stored elsewhere and retrieved at run time.
  2. Code must be legible enough to copy out by hand and reproduce the results.

Any image format is applicable.

Bonuses

  • -10% of your score if you allow more than one output format.
  • -15% if your code is also a 'true' quine. i.e. it does not read its source code but the source code is embedded (see here for an example)
  • -30% if your code is a strict quine - i.e the image of the code is embedded in the program (Piet solutions, I'm looking at you.).

Pureferret

Posted 2014-03-06T14:38:42.950

Reputation: 960

I assume you must output just the code, and not just print screen and output – sagiksp – 2017-05-04T05:20:25.987

@sagiksp nope

– Pureferret – 2017-05-04T09:51:18.617

@Pureferret It was worth a shot haha – sagiksp – 2017-05-05T05:25:45.527

Does it have to be legible? – Tim Seguine – 2014-03-06T14:53:33.680

@TimSeguine, the image should allow someone to copy it by hand and reproduce tt exactly. – Pureferret – 2014-03-06T14:59:53.883

7"Must output the code image itself, not a stored version from elsewhere." Is it allowed and/or required to read the source code file? – Tim Seguine – 2014-03-06T15:07:23.107

+1 Tim's question, because normally reading the source file is forbidden for quines. – Jonathan Van Matre – 2014-03-06T15:17:36.970

29I have just implemented a solution in whitespace. The resulting image looks quite boring. – Howard – 2014-03-06T15:32:18.797

7

Here is an interesting image quine written in Piet: http://mamememo.blogspot.be/2009/10/piet-quine.html

– ProgramFOX – 2014-03-06T15:32:38.977

@Howard Indeed. – duci9y – 2014-03-06T18:31:16.143

What I'm getting from the answers is $0 or the equivalent ought to be forbidden just as the source file is. – Ryan Reich – 2014-03-06T19:27:49.113

What i was trying to prevent is someone writing minimal code manually printing the screen and say 'renaming it's or re-outputting it. – Pureferret – 2014-03-06T19:32:22.410

So if I have -15% and -30% is it score * (1 - 0.15) * (1 - 0.30) or score * (1 - 0.15 - 0.30)? – The Guy with The Hat – 2014-03-06T23:54:22.427

@TheGuywithTheHat add each separately, so the former. – Pureferret – 2014-03-06T23:57:34.653

My PostScript CV does this.

– luser droog – 2014-03-07T07:58:08.620

3

Have a look here!!! http://en.wikipedia.org/wiki/Tupper%27s_self-referential_formula

– Kaz – 2014-03-07T10:00:58.940

Answers

8

Sh, X & ImageMagick 18.9:

 import -window root a.jpg

This should work in any shell that has ImageMagick.

To print only the code prepend clear && this comes out at 26.1

clear && import -window root a.jpg

Sample output:

enter image description here

Pureferret

Posted 2014-03-06T14:38:42.950

Reputation: 960

1Just curious, I see bitcoin and litecoin wallets; It's ~3 years later now, any serious gains? :D – Albert Renshaw – 2017-04-13T00:37:47.843

Why did you accept this? – MD XF – 2017-07-31T19:32:23.703

@MDXF shortest answer. – Pureferret – 2017-08-01T07:21:30.453

Tested on my computer, it works. – user12205 – 2014-03-06T19:43:55.670

@Ace, could I be cheeky and ask you to edit the image into my answer? – Pureferret – 2014-03-06T20:25:05.780

4

Didn't work for me on the Linux console. You may want to add X to the requirements.

– Ilmari Karonen – 2014-03-06T22:57:17.017

2Not a strict quine. (Doesn't programmatically generate its own source code.) – nitro2k01 – 2014-03-06T23:04:13.357

@nitro agreed, but the only strict prime would have an image as the input as well. – Pureferret – 2014-03-06T23:07:12.087

Well, you're calling the question "an image based quine", and says that the objective is to print the source code to a image instead of as text. Imo, it doesn't make sense to arbitrarily remove the conventional quine criterion that the output should be generated, just because the output target is an image instead of text. (Note btw that I left the same comment on most of the answers.) – nitro2k01 – 2014-03-06T23:12:02.957

1@nitro I may have misused the term 'Quine' but the first line of my post only mentions outputting an image of the source code. I'm editing now to clarify my original intent, and also to benefit true/strict quines. – Pureferret – 2014-03-06T23:14:23.033

25

shell

By "Print an image of the source code", I assume that actually printing an image on paper would be acceptable.

#!/bin/sh
lpr $0

Mark Plotnick

Posted 2014-03-06T14:38:42.950

Reputation: 1 231

21

Piet, 24399.76

enter image description here

This was not made by me.

The Guy with The Hat

Posted 2014-03-06T14:38:42.950

Reputation: 778

7Could you community wiki this answer? That has become our standard policy for externally-sourced answers. This is really cool, though! – Jonathan Van Matre – 2014-03-06T23:12:24.820

@JonathanVanMatre Whoops, sorry. That's what I meant to do. I guess I shouldn't try to answer questions while participating in another discussion.

– The Guy with The Hat – 2014-03-06T23:15:07.607

It's the lower of the two. -30% and -15% – Pureferret – 2014-03-07T06:47:50.753

14

PHP - 487 × 0.9 × 0.85 = 372.555 (2000×99px)

<?php $y="imagecolorallocate";$l=[
'<?php $y="imagecolorallocate";$l=[',
'];$i=imagecreate(2e3,99);$y($i,99,99,99);$w=$y($i,$j=0,0,0);$z=function($_)use(&$j,$i,$w){imagestring($i,4,9,$j+=15,$_,$w);};$z($l[0]);foreach($l as$m)$z(chr(39).$m.chr(39).",");$z($l[1]);$argv[1]($i,"o");',
];$i=imagecreate(2e3,99);$y($i,99,99,99);$w=$y($i,$j=0,0,0);$z=function($_)use(&$j,$i,$w){imagestring($i,4,9,$j+=15,$_,$w);};$z($l[0]);foreach($l as$m)$z(chr(39).$m.chr(39).",");$z($l[1]);$argv[1]($i,"o");

If warnings are fine: PHP - 479 × 0.9 × 0.85 = 366.435

<?php $y=imagecolorallocate;$l=[
'<?php $y=imagecolorallocate;$l=[',
'];$i=imagecreate(2e3,99);$y($i,99,99,99);$w=$y($i,$j=0,0,0);$z=function($_)use(&$j,$i,$w){imagestring($i,4,9,$j+=15,$_,$w);};$z($l[0]);foreach($l as$m)$z(chr(39).$m.chr(39).",");$z($l[1]);$argv[1]($i,o);',
];$i=imagecreate(2e3,99);$y($i,99,99,99);$w=$y($i,$j=0,0,0);$z=function($_)use(&$j,$i,$w){imagestring($i,4,9,$j+=15,$_,$w);};$z($l[0]);foreach($l as$m)$z(chr(39).$m.chr(39).",");$z($l[1]);$argv[1]($i,o);

You provide the output function to use as the first command line argument:

php timwolla.php imagepng

Solution with warnings:

TimWolla

Posted 2014-03-06T14:38:42.950

Reputation: 1 878

5I think this is the first actual quine and not just a script reads its own source code, or just takes a screenshot of itself. – nitro2k01 – 2014-03-06T22:06:48.680

Two questions, can you use a different notation for your decimals? It's mildly confusing. Also why is the image so small? It's difficult to read (admittedly I'm on my phone) – Pureferret – 2014-03-07T06:51:19.030

2@Pureferret just zoom it in. It's actually quite readable despite being pixelized. – Ruslan – 2014-03-07T07:52:38.337

1@Pureferret Just changed them, I never remember it being different in english… – TimWolla – 2014-03-07T12:42:27.183

13

Whitespace, 125



















Outputs an image file in the pbm format.

If you're testing this code, please copy it by clicking on "Edit", and copying everything between the <pre> tags.

Output:

P1 1 1 0

duci9y

Posted 2014-03-06T14:38:42.950

Reputation: 1 256

7copies by hand, squinting at screen to see subtle differences between spaces and tabs.... runs program.... hard drive explodes – Jonathan Van Matre – 2014-03-06T18:34:16.407

1@JonathanVanMatre You must have copied it wrong. Whitespace can only manipulate registers and RAM. :) – duci9y – 2014-03-06T18:35:04.250

17This answer doesn't meet requirement 2. The image isn't readable enough to reproduce the source code. – user2357112 supports Monica – 2014-03-06T18:35:31.603

2@user2357112 But you cannot prove that. That's the loophole I'm exploiting here. – duci9y – 2014-03-06T18:37:21.773

21Yes I can, using information theory. A fully-rigorous justification would be beyond the scope of a 600-character comment, but the gist of it is that your image is identical to an image of code that doesn't work. – user2357112 supports Monica – 2014-03-06T18:41:05.623

3

Pardon the French, but BS! The rules state that the code must be legible. Writing code in Whitespace doesn't negate that. This is your program shown in Sublime Text, when selected: http://i.imgur.com/A2ZaQwI.png This is legible. Whatever your program outputs isn't. And besides, you haven't explained whether what your program outputs is actually your source code or just literally an empty image.

– nitro2k01 – 2014-03-06T21:56:30.533

2At present this doesn't pass rule two. A loop hole would be embedding a the number of spaces tabs etc in text, like S S S T T S or similar. That you could reproduce. – Pureferret – 2014-03-07T08:08:45.830

@Pureferret Sorry for doing that, but this wasn't meant to be a serious answer. You do not have to consider it. – duci9y – 2014-03-07T09:12:44.203

13

Mathematica, 37 31 chars

(#2[#1[#0[#1,#2]]]&)[Defer,Rasterize]

(Rasterize[#1[#0[#1]]]&)[Defer]

enter image description here

Inspired by an answer in mathematica.stackexchange.com.

alephalpha

Posted 2014-03-06T14:38:42.950

Reputation: 23 988

Explain it...what are the #n for? Maybe we can figure it out. – Pureferret – 2014-03-07T06:43:22.173

At least it works without FrontEnd. (Tried with Export["/tmp/out.png",%] after running this command) – Ruslan – 2014-03-07T07:57:36.717

2@Pureferret #n means nth argument of a pure function (created at the & point). The whole expression expands to Rasterize[Defer[#0[Defer,Rasterize]]], where #0 is recursive call of the original pure function. In a way, this isn't a strict quine, but it doesn't need any frontend to work, and is IMO a nice solution. – Ruslan – 2014-03-07T08:03:01.643

I admire the use of Defer[] and Rasterize[] as both the commands and the object of the commands. I've never seen recursion presented this way before. – Michael Stern – 2014-03-08T01:58:20.680

10

AppleScript, 68 37

Alright, if you can call ImageMagick in zsh then this too is valid. I'm still hacking at something more elegant and of-the-quine-spirit for my own satisfaction, but for pure golfiness, here we are:

New version

do shell script "screencapture q.jpg"

Old version

tell application "System Events" to keystroke "#" using command down

I imagine this will still be beaten, but verbose old AppleScript does an admirable imitation of succinctness for this one.

stop your quine

do shell script "screencapture -c"

Jonathan Van Matre

Posted 2014-03-06T14:38:42.950

Reputation: 2 307

3

I think a quine, by definition, produces its source code as its only output, but not sure if that should apply to this question. http://www.nyx.net/~gthompso/quine.htm

– duci9y – 2014-03-06T18:23:44.337

Yeah, I am actually trying to figure out how to get it to select only the code area of its own window, but I figured I'd go ahead and throw this up to plant my flag in AppleScript territory. – Jonathan Van Matre – 2014-03-06T18:25:59.733

I'm considering this as valid and was expecting something of this sort. – Pureferret – 2014-03-06T19:01:52.500

Not a strict quine. (Doesn't programmatically generate its own source code.) – nitro2k01 – 2014-03-06T23:03:40.760

3Technically, none of these can be strict quines except the Piet answer, because none of these languages take images as source code. But I'm still determined to make a more stricter-er version of mine. :) – Jonathan Van Matre – 2014-03-06T23:15:44.553

8

Mathematica, 83

SelectionMove[InputNotebook[],Previous,Cell];Rasterize@NotebookRead@SelectedCells[]

enter image description here

chuy

Posted 2014-03-06T14:38:42.950

Reputation: 389

1Not a strict quine. (Doesn't programmatically generate its own source code.) – nitro2k01 – 2014-03-06T23:02:46.630

1Doesn't work without frontend (in math or MathKernel), even with `<<JavaGraphics`` imported. – Ruslan – 2014-03-07T07:50:53.660

5

HTML5/Javascript : 615

<canvas id='i' width=500 height=5000></canvas><script>function d(){var e=document.getElementById("i");var t=e.getContext("2d");t.font="20px Arial";var n=400;var r=25;var i=(e.width-n)/2;var s=60;str="<canvas id='i' width=5000 height=500></canvas>\n<script>"+d+"d();"+wrapText+"<\/script>";wrapText(t,str,i,s,n,r)}function wrapText(e,t,n,r,i,s){var o=t.split(" ");var u="";for(var a=0;a<o.length;a++){var f=u+o[a]+" ";var l=e.measureText(f);var c=l.width;if(c>i&&a>0){e.lineWidth=1;e.strokeStyle="blue";e.strokeText(u,n,r);u=o[a]+" ";r+=s}else{u=f}}e.lineWidth=1;e.strokeStyle="blue";e.strokeText(u,n,r)}d()</script>

Demo : http://jsfiddle.net/E2738/2/

One can right click on the image and save its as a PNG

Clyde Lobo

Posted 2014-03-06T14:38:42.950

Reputation: 1 395

2it ends with <//script> – Not that Charles – 2014-03-06T15:50:49.177

1at end of line 11 in the fiddle change +wrapText+"<//script>"; to +wrapText+"<\/script>"; – Luke – 2014-03-07T00:38:26.000

@Charles Thanks for pointing that out. Fixed. – Clyde Lobo – 2014-03-07T09:47:21.693

4

Javascript + JQuery 153 148

Regular

(function f(){
  c=$('<canvas/>')[0];
  a=c.getContext('2d');
  l=('('+f+')()').split('\n');
  for(i=0;i<l.length;i++)
    a.fillText(l[i],5,12*(i+1));
  $('body').append('<img src="'
      +c.toDataURL("image/png")+'"/>')
})()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Golfed

function f(){c=$('<canvas>')[0];c.width=750;a=c.getContext('2d');a.fillText(f+'f()',5,9);$('body').append('<img src="'+c.toDataURL("png")+'"/>')}f()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

aebabis

Posted 2014-03-06T14:38:42.950

Reputation: 433

1Not a strict quine. (Doesn't programmatically generate its own source code.) – nitro2k01 – 2014-03-06T23:08:09.780

$ ? are you using jQuery as well?? – Clyde Lobo – 2014-03-07T09:43:34.930

@ClydeLobo Yup, seems he is. Wait, you can do Javascript without JQuery? Who'd have thought! – Pierre Arlaud – 2014-03-07T14:59:21.147

@ClydeLobo Yes I am. But libraries should be allowed. Its really no different from a C program using stdio.h xD. Writing the program without jQuery is easy though. Just replace $('<canvas/>')[0] with document.createElement('canvas') and $('body').append with document.write. jQuery saved me bytes. – aebabis – 2014-03-07T16:19:59.007

4

Java, 570 - 10% - 15% = 427.5

("filepath" included), 554 - 10% - 15% = 415.5 ("filepath" not included)
Thanks to Andreas for removing BufferedImage in java.awt.image.BufferedImage

import java.awt.image.*;class Q{public static void main(String[]a)throws Exception{BufferedImage i=new BufferedImage(3500,12,1);String s="import java.awt.image.*;class Q{public static void main(String[]a)throws Exception{BufferedImage i=new BufferedImage(3500,12,1);String s=%s%s%s;char q=34;i.getGraphics().drawString(String.format(s,q,s,q,q,q,q,q),0,9);javax.imageio.ImageIO.write(i,%spng%s,new java.io.File(%sfilepath%s));}}";char q=34;i.getGraphics().drawString(String.format(s,q,s,q,q,q,q,q),0,9);javax.imageio.ImageIO.write(i,"png",new java.io.File("filepath"));}}

Output:enter image description here

To view properly, see this link: http://i.stack.imgur.com/RRSDw.png

This works just like a regular quine, except it outputs to an image. The current format is png, but the format can easily be changed by replacing all instances of png in the program with whatever format you want.

Unlike a few answers here, this is a true quine; no reading of the program file.

Justin

Posted 2014-03-06T14:38:42.950

Reputation: 19 757

you can get rid of another 24 characters if you replace both "java.awt.image.BufferedImage" with "java.awt.image.*" – Andreas – 2014-03-07T14:08:54.120

3

zsh, 57 × 0.9 = 51.3

Pass it the output filename as an argument.

convert -annotate +0+10 "$(<$0)" -size 320x14 xc:white $1

Produces:

Example output

Ry-

Posted 2014-03-06T14:38:42.950

Reputation: 5 283

1is that zsh or imagemagick? – Kaya – 2014-03-06T17:19:12.847

@Kaya: Well, both. The language is zsh (or bash, but it’s not Posix), and the library/tool is ImageMagick (or GraphicsMagick, if you prefer) – like PHP with GD or Ruby with RMagick. – Ry- – 2014-03-06T19:03:17.957

1@Pureferret: Added. – Ry- – 2014-03-06T19:07:03.777

Not a strict quine. (Doesn't programmatically generate its own source code.) – nitro2k01 – 2014-03-06T23:03:56.260

3

C99 (using SDL & SDL_ttf), 414 354 346 - 15% = 294.1

#include<SDL_ttf.h>
#define Q(P)char*q=#P;P
Q(
i=5;main(){for(SDL_Surface*s=SDL_SetVideoMode(2048,80,SDL_Init(SDL_INIT_VIDEO),TTF_Init());i--;SDL_SaveBMP(s,"q.bmp"))SDL_BlitSurface(TTF_RenderText_Blended(TTF_OpenFont("q.ttf",9),(char*[]){"#include<SDL_ttf.h>","#define Q(P)char*q=#P;P","Q(",q,")"}[i],(SDL_Color){~0}),0,s,&(SDL_Rect){0,16*i});}
)

This is pretty ugly without more line breaks, but unfortunately they need to be absent. The text-rendering function doesn't grok control characters at all, so any line breaks in the code have to be rendered manually in the output.

Here's the same code but with some extra line breaks thrown in for legibility:

#include<SDL_ttf.h>
#define Q(P)char*q=#P;P
Q(
i=5;main(){for(SDL_Surface*s=SDL_SetVideoMode(2048,80,
SDL_Init(SDL_INIT_VIDEO),TTF_Init());i--;SDL_SaveBMP(s,"q.bmp"))
SDL_BlitSurface(TTF_RenderText_Blended(TTF_OpenFont("q.ttf",9),
(char*[]){"#include<SDL_ttf.h>","#define Q(P)char*q=#P;P","Q(",q,")"}[i],
(SDL_Color){~0}),0,s,&(SDL_Rect){0,16*i});}
)

Sadly, this doesn't also add line breaks to the graphical output:

output

The output is still legible, though with 9-point output and the red font color, it's a bit squinty. You can improve it at the cost of a character by replacing the 9 with 12. (Note that the dimension of the resulting image is hardcoded to 2048x80. To accommodate the differences in various fonts, a fair bit of excess has been added to the right margin and the leading, enough so that a size-12 font should still fit comfortably. If you wish to increase it further, however, the dimensions will probably need to be altered as well.)

The command to build the program is:

gcc -Wall -o imgquine imgquine.c -lSDL_ttf `sdl-config --cflags --libs`

The program assumes that there is a font file called q.ttf in the current directory when run. I took care of this beforehand by running the following command (which should work on most modern Linuxes):

ln -s `fc-match --format='%{file}' sans` ./q.ttf

(Feel free to import your own favorite TrueType font instead.)

After running the program, the image output will be created in the current directory, in a file named q.bmp. Unfortunately Windows bitmap files are the only output format that this program provides. Adding more output formats would require linking in more libraries.

Note that this program takes advantage of C99's syntax for introducing non-simple literal values, thus significantly reducing the number of variables that need to be defined. This is something that more C golfers should take advantage of.

breadbox

Posted 2014-03-06T14:38:42.950

Reputation: 6 893

2

Ruby, 104 characters

require "RMagick"
include Magick
Draw.new.annotate(i=Image.new(999,99),0,0,0,9,File.read($0))
i.display

Example output, per request: i.imgur.com/jMC594C.png

Shelvacu

Posted 2014-03-06T14:38:42.950

Reputation: 610

Do you have an example output? – Pureferret – 2014-03-06T17:12:54.453

@Pureferret see edit – Shelvacu – 2014-03-06T17:28:30.363

1Not a strict quine. (Doesn't programmatically generate its own source code.) – nitro2k01 – 2014-03-06T23:04:43.093

2

C# - 498 - 15% = 423.3

This can probably be golfed more. I've never done quines or this kind of graphics in C# before:

using System;using System.Drawing;class Q{static void Main(){var b = new Bitmap(3050, 20);var g=Graphics.FromImage(b);string f="using System;using System.Drawing;class Q{{static void Main(){{var b = new Bitmap(3050, 20);var g=Graphics.FromImage(b);string f={0}{1}{0},e={3}{0}{2}{0};g.DrawString(String.Format(f,(char)34,f,e,'@'),SystemFonts.MenuFont,Brushes.Black,0,0);b.Save(e);}}}}",e=@"D:\p.png";g.DrawString(String.Format(f,(char)34,f,e,'@'),SystemFonts.MenuFont,Brushes.Black,0,0);b.Save(e);}}

Output: enter image description here

Adding a different format support would be easy. Not sure if it's worth it, though.

Ray Poward

Posted 2014-03-06T14:38:42.950

Reputation: 281

1

Python: 255 238 -10% -15% = 195.075 182.07

import sys,PIL.ImageDraw as D;i=D.Image.new('L',(2000,20));r="import sys,PIL.ImageDraw as D;i=D.Image.new('L',(2000,20));r=%r;D.Draw(i).text((0,0),r%%r,fill=255);i.save(sys.argv[1])";D.Draw(i).text((0,0),r%r,fill=255);i.save(sys.argv[1])

Usage:

python imgquine.py quine.jpg

This is a true quine that draws the output to the file specified on the commandline. The file format is set simply by changing the filename extension (e.g. quine.jpg for a JPEG and quine.png for a PNG).

Example output (2000x20 image):

Quine output

nneonneo

Posted 2014-03-06T14:38:42.950

Reputation: 11 445

You could save some chars by using 2e3 instead of 2000. – Kaya – 2014-03-23T05:17:21.657

@Kaya: No, I can't, unfortunately: 2e3 is a float, and Image.new demands ints. – nneonneo – 2014-03-23T05:39:53.270

oh bother, I assumed it would be coerced. – Kaya – 2014-03-23T05:45:39.343