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

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.).
• Does it have to be legible? 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? Mar 6 '14 at 15:07
• I have just implemented a solution in whitespace. The resulting image looks quite boring. Mar 6 '14 at 15:32
• Here is an interesting image quine written in Piet: mamememo.blogspot.be/2009/10/piet-quine.html Mar 6 '14 at 15:32
• Have a look here!!! en.wikipedia.org/wiki/Tupper%27s_self-referential_formula
– Kaz
Mar 7 '14 at 10:00

# 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  ## Piet, 24399.76 This was not made by me. • Could you community wiki this answer? That has become our standard policy for externally-sourced answers. This is really cool, though! Mar 6 '14 at 23:12 • It's the lower of the two. -30% and -15% Mar 7 '14 at 6:47 # 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: • 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. 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) Mar 7 '14 at 6:51 • @Pureferret just zoom it in. It's actually quite readable despite being pixelized. Mar 7 '14 at 7:52 • @Pureferret Just changed them, I never remember it being different in english… Mar 7 '14 at 12:42 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  • copies by hand, squinting at screen to see subtle differences between spaces and tabs.... runs program.... hard drive explodes Mar 6 '14 at 18:34 • This answer doesn't meet requirement 2. The image isn't readable enough to reproduce the source code. Mar 6 '14 at 18:35 • @user2357112 But you cannot prove that. That's the loophole I'm exploiting here. Mar 6 '14 at 18:37 • 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. 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: 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. Mar 6 '14 at 21:56 # Mathematica, 37 31 chars (#2[#1[#0[#1,#2]]]&)[Defer,Rasterize]  (Rasterize[#1[#0[#1]]]&)[Defer]  Inspired by an answer in mathematica.stackexchange.com. • Explain it...what are the #n for? Maybe we can figure it out. Mar 7 '14 at 6:43 • At least it works without FrontEnd. (Tried with Export["/tmp/out.png",%] after running this command) 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. 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. Mar 8 '14 at 1:58 ## 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. do shell script "screencapture -c"  • 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 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. Mar 6 '14 at 18:25 • I'm considering this as valid and was expecting something of this sort. Mar 6 '14 at 19:01 • Not a strict quine. (Doesn't programmatically generate its own source code.) 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. :) Mar 6 '14 at 23:15 ## Mathematica, 83 SelectionMove[InputNotebook[],Previous,Cell];Rasterize@NotebookRead@SelectedCells[]  • Not a strict quine. (Doesn't programmatically generate its own source code.) Mar 6 '14 at 23:02 • Doesn't work without frontend (in math or MathKernel), even with <<JavaGraphics imported. Mar 7 '14 at 7:50 # 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: • Tested on my computer, it works. Mar 6 '14 at 19:43 • Didn't work for me on the Linux console. You may want to add X to the requirements. Mar 6 '14 at 22:57 • Not a strict quine. (Doesn't programmatically generate its own source code.) 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. Mar 6 '14 at 23:14 • Just curious, I see bitcoin and litecoin wallets; It's ~3 years later now, any serious gains? :D Apr 13 '17 at 0:37 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>  One can right click on the image and save its as a PNG • it ends with <//script> 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. 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 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: 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. • you can get rid of another 24 characters if you replace both "java.awt.image.BufferedImage" with "java.awt.image.*" Mar 7 '14 at 14:08 ## 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> • Not a strict quine. (Doesn't programmatically generate its own source code.) Mar 6 '14 at 23:08 • $ ? are you using jQuery as well?? Mar 7 '14 at 9:43
• @ClydeLobo Yup, seems he is. Wait, you can do Javascript without JQuery? Who'd have thought! 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. Mar 7 '14 at 16:19

## 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: • 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. – Ry- Mar 6 '14 at 19:03 • @Pureferret: Added. – Ry- Mar 6 '14 at 19:07 • Not a strict quine. (Doesn't programmatically generate its own source code.) Mar 6 '14 at 23:03 ## C99 (using SDL & SDL_ttf), 414354 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: 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. # 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: Adding a different format support would be easy. Not sure if it's worth it, though. ## 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

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

# 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):

• 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 Image.new demands int`s. Mar 23 '14 at 5:39
• oh bother, I assumed it would be coerced.
– Kaya
Mar 23 '14 at 5:45