28
\$\begingroup\$

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.).
\$\endgroup\$
16
  • \$\begingroup\$ Does it have to be legible? \$\endgroup\$ Commented Mar 6, 2014 at 14:53
  • 7
    \$\begingroup\$ "Must output the code image itself, not a stored version from elsewhere." Is it allowed and/or required to read the source code file? \$\endgroup\$ Commented Mar 6, 2014 at 15:07
  • 31
    \$\begingroup\$ I have just implemented a solution in whitespace. The resulting image looks quite boring. \$\endgroup\$
    – Howard
    Commented Mar 6, 2014 at 15:32
  • 8
    \$\begingroup\$ Here is an interesting image quine written in Piet: mamememo.blogspot.be/2009/10/piet-quine.html \$\endgroup\$
    – ProgramFOX
    Commented Mar 6, 2014 at 15:32
  • 3
    \$\begingroup\$ Have a look here!!! en.wikipedia.org/wiki/Tupper%27s_self-referential_formula \$\endgroup\$
    – Kaz
    Commented Mar 7, 2014 at 10:00

16 Answers 16

26
\$\begingroup\$

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
\$\endgroup\$
21
\$\begingroup\$

Piet, 24399.76

enter image description here

This was not made by me.

\$\endgroup\$
2
  • 7
    \$\begingroup\$ Could you community wiki this answer? That has become our standard policy for externally-sourced answers. This is really cool, though! \$\endgroup\$ Commented Mar 6, 2014 at 23:12
  • \$\begingroup\$ It's the lower of the two. -30% and -15% \$\endgroup\$ Commented Mar 7, 2014 at 6:47
14
\$\begingroup\$

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:

\$\endgroup\$
4
  • 5
    \$\begingroup\$ I think this is the first actual quine and not just a script reads its own source code, or just takes a screenshot of itself. \$\endgroup\$
    – nitro2k01
    Commented Mar 6, 2014 at 22:06
  • \$\begingroup\$ 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) \$\endgroup\$ Commented Mar 7, 2014 at 6:51
  • 2
    \$\begingroup\$ @Pureferret just zoom it in. It's actually quite readable despite being pixelized. \$\endgroup\$
    – Ruslan
    Commented Mar 7, 2014 at 7:52
  • 1
    \$\begingroup\$ @Pureferret Just changed them, I never remember it being different in english… \$\endgroup\$
    – TimWolla
    Commented Mar 7, 2014 at 12:42
13
\$\begingroup\$

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
\$\endgroup\$
8
  • 7
    \$\begingroup\$ copies by hand, squinting at screen to see subtle differences between spaces and tabs.... runs program.... hard drive explodes \$\endgroup\$ Commented Mar 6, 2014 at 18:34
  • 19
    \$\begingroup\$ This answer doesn't meet requirement 2. The image isn't readable enough to reproduce the source code. \$\endgroup\$ Commented Mar 6, 2014 at 18:35
  • 2
    \$\begingroup\$ @user2357112 But you cannot prove that. That's the loophole I'm exploiting here. \$\endgroup\$
    – duci9y
    Commented Mar 6, 2014 at 18:37
  • 22
    \$\begingroup\$ Yes 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. \$\endgroup\$ Commented Mar 6, 2014 at 18:41
  • 3
    \$\begingroup\$ 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: 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. \$\endgroup\$
    – nitro2k01
    Commented Mar 6, 2014 at 21:56
13
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ Explain it...what are the #n for? Maybe we can figure it out. \$\endgroup\$ Commented Mar 7, 2014 at 6:43
  • \$\begingroup\$ At least it works without FrontEnd. (Tried with Export["/tmp/out.png",%] after running this command) \$\endgroup\$
    – Ruslan
    Commented Mar 7, 2014 at 7:57
  • 2
    \$\begingroup\$ @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. \$\endgroup\$
    – Ruslan
    Commented Mar 7, 2014 at 8:03
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 8, 2014 at 1:58
10
\$\begingroup\$

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"
\$\endgroup\$
5
  • 3
    \$\begingroup\$ I think a quine, by definition, produces its source code as its only output, but not sure if that should apply to this question. nyx.net/~gthompso/quine.htm \$\endgroup\$
    – duci9y
    Commented Mar 6, 2014 at 18:23
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 6, 2014 at 18:25
  • \$\begingroup\$ I'm considering this as valid and was expecting something of this sort. \$\endgroup\$ Commented Mar 6, 2014 at 19:01
  • \$\begingroup\$ Not a strict quine. (Doesn't programmatically generate its own source code.) \$\endgroup\$
    – nitro2k01
    Commented Mar 6, 2014 at 23:03
  • 3
    \$\begingroup\$ Technically, 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. :) \$\endgroup\$ Commented Mar 6, 2014 at 23:15
8
\$\begingroup\$

Mathematica, 83

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

enter image description here

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Not a strict quine. (Doesn't programmatically generate its own source code.) \$\endgroup\$
    – nitro2k01
    Commented Mar 6, 2014 at 23:02
  • 1
    \$\begingroup\$ Doesn't work without frontend (in math or MathKernel), even with <<JavaGraphics` imported. \$\endgroup\$
    – Ruslan
    Commented Mar 7, 2014 at 7:50
8
\$\begingroup\$

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

\$\endgroup\$
10
  • \$\begingroup\$ Tested on my computer, it works. \$\endgroup\$
    – user12205
    Commented Mar 6, 2014 at 19:43
  • 4
    \$\begingroup\$ Didn't work for me on the Linux console. You may want to add X to the requirements. \$\endgroup\$ Commented Mar 6, 2014 at 22:57
  • 2
    \$\begingroup\$ Not a strict quine. (Doesn't programmatically generate its own source code.) \$\endgroup\$
    – nitro2k01
    Commented Mar 6, 2014 at 23:04
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ Commented Mar 6, 2014 at 23:14
  • 1
    \$\begingroup\$ Just curious, I see bitcoin and litecoin wallets; It's ~3 years later now, any serious gains? :D \$\endgroup\$ Commented Apr 13, 2017 at 0:37
5
\$\begingroup\$

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

\$\endgroup\$
3
  • 2
    \$\begingroup\$ it ends with <//script> \$\endgroup\$ Commented Mar 6, 2014 at 15:50
  • 1
    \$\begingroup\$ at end of line 11 in the fiddle change +wrapText+"<//script>"; to +wrapText+"<\/script>"; \$\endgroup\$
    – Luke
    Commented Mar 7, 2014 at 0:38
  • \$\begingroup\$ @Charles Thanks for pointing that out. Fixed. \$\endgroup\$
    – Clyde Lobo
    Commented Mar 7, 2014 at 9:47
4
\$\begingroup\$

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: https://i.sstatic.net/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.

\$\endgroup\$
1
  • \$\begingroup\$ you can get rid of another 24 characters if you replace both "java.awt.image.BufferedImage" with "java.awt.image.*" \$\endgroup\$
    – Andreas
    Commented Mar 7, 2014 at 14:08
4
\$\begingroup\$

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>

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Not a strict quine. (Doesn't programmatically generate its own source code.) \$\endgroup\$
    – nitro2k01
    Commented Mar 6, 2014 at 23:08
  • \$\begingroup\$ $ ? are you using jQuery as well?? \$\endgroup\$
    – Clyde Lobo
    Commented Mar 7, 2014 at 9:43
  • \$\begingroup\$ @ClydeLobo Yup, seems he is. Wait, you can do Javascript without JQuery? Who'd have thought! \$\endgroup\$ Commented Mar 7, 2014 at 14:59
  • \$\begingroup\$ @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. \$\endgroup\$
    – aebabis
    Commented Mar 7, 2014 at 16:19
3
\$\begingroup\$

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

\$\endgroup\$
4
  • 1
    \$\begingroup\$ is that zsh or imagemagick? \$\endgroup\$
    – Kaya
    Commented Mar 6, 2014 at 17:19
  • \$\begingroup\$ @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. \$\endgroup\$
    – Ry-
    Commented Mar 6, 2014 at 19:03
  • 1
    \$\begingroup\$ @Pureferret: Added. \$\endgroup\$
    – Ry-
    Commented Mar 6, 2014 at 19:07
  • \$\begingroup\$ Not a strict quine. (Doesn't programmatically generate its own source code.) \$\endgroup\$
    – nitro2k01
    Commented Mar 6, 2014 at 23:03
3
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\begingroup\$ Do you have an example output? \$\endgroup\$ Commented Mar 6, 2014 at 17:12
  • \$\begingroup\$ @Pureferret see edit \$\endgroup\$
    – Shelvacu
    Commented Mar 6, 2014 at 17:28
  • 2
    \$\begingroup\$ Not a strict quine. (Doesn't programmatically generate its own source code.) \$\endgroup\$
    – nitro2k01
    Commented Mar 6, 2014 at 23:04
1
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\begingroup\$ You could save some chars by using 2e3 instead of 2000. \$\endgroup\$
    – Kaya
    Commented Mar 23, 2014 at 5:17
  • \$\begingroup\$ @Kaya: No, I can't, unfortunately: 2e3 is a float, and Image.new demands ints. \$\endgroup\$
    – nneonneo
    Commented Mar 23, 2014 at 5:39
  • \$\begingroup\$ oh bother, I assumed it would be coerced. \$\endgroup\$
    – Kaya
    Commented Mar 23, 2014 at 5:45

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.