Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Code golf: Print an image of the source code.


  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.


  • -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.).
share|improve this question
Does it have to be legible? – Tim Seguine Mar 6 '14 at 14:53
"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 Mar 6 '14 at 15:07
I have just implemented a solution in whitespace. The resulting image looks quite boring. – Howard Mar 6 '14 at 15:32
Here is an interesting image quine written in Piet: – ProgramFOX Mar 6 '14 at 15:32
Have a look here!!! – Kaz Mar 7 '14 at 10:00

17 Answers 17


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

lpr $0
share|improve this answer

Piet, 24399.76

enter image description here

This was not made by me.

share|improve this answer
Could you community wiki this answer? That has become our standard policy for externally-sourced answers. This is really cool, though! – Jonathan Van Matre Mar 6 '14 at 23:12
@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 Mar 6 '14 at 23:15
It's the lower of the two. -30% and -15% – Pureferret Mar 7 '14 at 6:47

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.


P1 1 1 0
share|improve this answer
copies by hand, squinting at screen to see subtle differences between spaces and tabs.... runs program.... hard drive explodes – Jonathan Van Matre Mar 6 '14 at 18:34
This answer doesn't meet requirement 2. The image isn't readable enough to reproduce the source code. – user2357112 Mar 6 '14 at 18:35
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. – user2357112 Mar 6 '14 at 18:41
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: 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 Mar 6 '14 at 21:56
At 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 Mar 7 '14 at 8:08

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:

share|improve this answer
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. – nitro2k01 Mar 6 '14 at 22:06
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 Mar 7 '14 at 6:51
@Pureferret just zoom it in. It's actually quite readable despite being pixelized. – Ruslan Mar 7 '14 at 7:52
@Pureferret Just changed them, I never remember it being different in english… – TimWolla Mar 7 '14 at 12:42

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"
share|improve this answer
I think a quine, by definition, produces its source code as its only output, but not sure if that should apply to this question. – duci9y Mar 6 '14 at 18:23
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 Mar 6 '14 at 18:25
I'm considering this as valid and was expecting something of this sort. – Pureferret Mar 6 '14 at 19:01
Not a strict quine. (Doesn't programmatically generate its own source code.) – nitro2k01 Mar 6 '14 at 23:03
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. :) – Jonathan Van Matre Mar 6 '14 at 23:15

Mathematica, 37 31 chars



enter image description here

Inspired by an answer in

share|improve this answer
Explain it...what are the #n for? Maybe we can figure it out. – Pureferret Mar 7 '14 at 6:43
At least it works without FrontEnd. (Tried with Export["/tmp/out.png",%] after running this command) – Ruslan Mar 7 '14 at 7:57
@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 Mar 7 '14 at 8:03
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 Mar 8 '14 at 1:58

Mathematica, 83


enter image description here

share|improve this answer
Not a strict quine. (Doesn't programmatically generate its own source code.) – nitro2k01 Mar 6 '14 at 23:02
Doesn't work without frontend (in math or MathKernel), even with <<JavaGraphics` imported. – Ruslan Mar 7 '14 at 7:50
up vote 7 down vote accepted

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

share|improve this answer
Tested on my computer, it works. – ace Mar 6 '14 at 19:43
@Ace, could I be cheeky and ask you to edit the image into my answer? – Pureferret Mar 6 '14 at 20:25
Didn't work for me on the Linux console. You may want to add X to the requirements. – Ilmari Karonen Mar 6 '14 at 22:57
Not a strict quine. (Doesn't programmatically generate its own source code.) – nitro2k01 Mar 6 '14 at 23:04
@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 Mar 6 '14 at 23:14

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 :

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

share|improve this answer
it ends with <//script> – Not that Charles Mar 6 '14 at 15:50
at end of line 11 in the fiddle change +wrapText+"<//script>"; to +wrapText+"<\/script>"; – Luke Mar 7 '14 at 0:38
@Charles Thanks for pointing that out. Fixed. – Clyde Lobo Mar 7 '14 at 9:47

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

Output:enter image description here

To view properly, see this link:

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.

share|improve this answer
you can get rid of another 24 characters if you replace both "java.awt.image.BufferedImage" with "java.awt.image.*" – Andreas Mar 7 '14 at 14:08

Javascript + JQuery 153 148


(function f(){
  $('body').append('<img src="'


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()

Here's a Fiddle:

share|improve this answer
Not a strict quine. (Doesn't programmatically generate its own source code.) – nitro2k01 Mar 6 '14 at 23:08
$ ? are you using jQuery as well?? – Clyde Lobo Mar 7 '14 at 9:43
@ClydeLobo Yup, seems he is. Wait, you can do Javascript without JQuery? Who'd have thought! – Pierre Arlaud Mar 7 '14 at 14:59
@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. – acbabis Mar 7 '14 at 16:19
+1 Has enough jQuery – NothingsImpossible Mar 7 '14 at 21:29

zsh, 57 × 0.9 = 51.3

Pass it the output filename as an argument.

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


Example output

share|improve this answer
is that zsh or imagemagick? – Kaya Mar 6 '14 at 17:19
@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. – Ryan O'Hara Mar 6 '14 at 19:03
@Pureferret: Added. – Ryan O'Hara Mar 6 '14 at 19:07
Not a strict quine. (Doesn't programmatically generate its own source code.) – nitro2k01 Mar 6 '14 at 23:03

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

#define Q(P)char*q=#P;P
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:

#define Q(P)char*q=#P;P
(char*[]){"#include<SDL_ttf.h>","#define Q(P)char*q=#P;P","Q(",q,")"}[i],

Sadly, this doesn't also add line breaks to the graphical 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.

share|improve this answer

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.

share|improve this answer

Ruby, 104 characters

require "RMagick"
include Magick,99),0,0,0,9,$0))

Example output, per request:

share|improve this answer
Do you have an example output? – Pureferret Mar 6 '14 at 17:12
@Pureferret see edit – shelvacu Mar 6 '14 at 17:28
Not a strict quine. (Doesn't programmatically generate its own source code.) – nitro2k01 Mar 6 '14 at 23:04

POSIX shell script, 0

Outputs itself in RAW image format (a 0x0 image) when run.

share|improve this answer
Where is the sample output? – Pureferret Mar 8 '14 at 9:28

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

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


python 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

share|improve this answer
You could save some chars by using 2e3 instead of 2000. – Kaya Mar 23 '14 at 5:17
@Kaya: No, I can't, unfortunately: 2e3 is a float, and demands ints. – nneonneo Mar 23 '14 at 5:39
oh bother, I assumed it would be coerced. – Kaya Mar 23 '14 at 5:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.