Paint Starry Night, objectively, in 1kB of code



Note: Anders Kaseorg has been awarded the accept for now, to draw attention to his great answer, but the challenge is by no means over! I will award a 1000 point bounty to anyone who takes the top score without using built-in compression.

Below is a 386x320 png representation of van Gogh's Starry Night.

enter image description here

Your goal is to reproduce this image as closely as possible, in no more than 1024 bytes of code. For the purposes of this challenge, the closeness of images is measured by the squared differences in RGB pixel values, as explained below.

This is . Scores are calculated using the validation script below. The lowest score wins.

Your code must obey the following restrictions:

  • It must be a complete program
  • It must output an image in a format that can be read by the validation script below, running on my machine. The script uses Python's PIL library, which can load a wide variety of file formats, including png, jpg and bmp.
  • It must be completely self-contained, taking no input and loading no files (other than importing libraries, which is allowed)
  • If your language or library includes a function that outputs Starry Night, you are not allowed to use that function.
  • It should run deterministically, producing the same output every time.
  • The dimensions of the output image must be 386x320
  • For the avoidance of doubt: valid answers must use programming languages as per the usual PPCG rules. It must be a program that outputs an image, not just an image file.

It is likely that some submissions will themselves be generated by code. If this is the case, please include in your answer the code that was used to produce your submission, and explain how it works. The above restrictions only apply to the 1kB image-generating program that you submit; they don't apply to any code used to generate it.


To calculate your score, take your output image and the original above and convert the RGB pixel values to floating point numbers ranging from 0 to 1. The score of a pixel is (orig_r-img_r)^2 +(orig_g-img_g)^2 + (orig_b-img_b)^2, i.e. the squared distance in RGB space between the two images. The score of an image is the sum of the scores of its pixels.

Below is a Python script that performs this calculation - in the case of any inconsistency or ambiguity, the definitive score is the one calculated by that script running on my machine.

Note that the score is calculated based on the output image, so if you use a lossy format that will affect the score.

The lower the score the better. The original Starry Night image would have a score of 0. In the astronomically unlikely event of a tie, the answer with the most votes will determine the winner.

Bonus objectives

Because the answers were dominated by solutions using built-in compression, I awarded a series of bounties to answers that use other techniques. The next one will be a bounty of 1000 points, to be awarded if and when an answer that does not use built-in compression takes the top place overall.

The previously awarded bonus bounties were as follows:

  • A 100 point bounty was awarded to nneonneo's answer, for being the highest-scoring answer that did not use built-in compression at the time. It had 4852.87 points at the time it was awarded. Honourable mentions go to 2012rcampion, who made a valiant attempt to beat nneonneo using an approach based on Voronoi tesselation, scoring 5076 points, and to Sleafar, whose answer was in the lead until near the end, with 5052 points, using a similar method to nneonneo.

  • A 200 point bounty was awarded to Strawdog's entry. This was awarded for being an optimization-based strategy that took the lead among non-built-in-compression answers and held it for a week. It scored 4749.88 points using an impressively clever method.

Scoring/validation script

The following Python script should be placed in the same folder as the image above (which should be named ORIGINAL.png) and run using a command of the form python myImage.png.

from PIL import Image
import sys

orig ="ORIGINAL.png")
img  =[1])

if img.size != orig.size:
    print("NOT VALID: image dimensions do not match the original")

w, h = img.size

orig = orig.convert("RGB")
img = img.convert("RGB")

orig_pix = orig.load()
img_pix = img.load()

score = 0

for x in range(w):
    for y in range(h):
        orig_r, orig_g, orig_b = orig_pix[x,y]
        img_r, img_g, img_b = img_pix[x,y]
        score += (img_r-orig_r)**2
        score += (img_g-orig_g)**2
        score += (img_b-orig_b)**2


Technical note: Objective measures of image similarity are a tricky thing. In this case I've opted for one that's easy for anyone to implement, in full knowledge that much better measures exist.


var QUESTION_ID=69930,OVERRIDE_USER=21034;function answersUrl(e){return""+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return""+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",,t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",,i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+(?:\.\d+))(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:400px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src=""></script> <link rel="stylesheet" type="text/css" href="//"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Score</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>


Posted 2016-01-23T15:16:57.587

Reputation: 6 641

5On Windows I had trouble installing the python requirements. A safer option is to use pillow (pip unistall PIL, then pip install pillow) and change the first line to from PIL import Image. – mınxomaτ – 2016-01-23T16:17:56.017

@mınxomaτ I've changed that line of code. (I have never consciously installed pillow but ways of importing Image work on my machine and appear to give the same answer.) – Nathaniel – 2016-01-23T16:20:25.217

1Can the program read a file if the combined file size + 1 byte is less than 1kB? Alternatively, can the program read its own source code? – orlp – 2016-01-23T19:14:11.003

@orlp no, no loading of files is permitted regardless of their size. But reading your own source is fine. – Nathaniel – 2016-01-24T01:20:42.783

1This is essentially the same similarity metric as PSNR, correct? – Damian Yerrick – 2016-01-24T04:50:17.437

2@tepples other than going in the opposite direction and not being logarithmic, yes :) – hobbs – 2016-01-24T16:11:14.417

I don't think that you'll "feel like" awarding the bounty until my answer becomes outvoted, will you? :p – LegionMammal978 – 2016-01-26T12:20:06.690

@LegionMammal978 by "highest-scoring" I meant best score according to the rules of this challenge, not votes. (Amended.) I voted for your answer! – Nathaniel – 2016-01-26T12:33:14.793

Would an SVG or HTML file be considered a 'program' ? – dronus – 2016-01-26T12:53:14.623

@dronus no, not unless (a) those formats are programming languages according to the PPCG rules, and (b) you can run them to produce an image in a raster format that can be read by PIL.

– Nathaniel – 2016-01-26T13:01:03.180

Given that the top four answers to this question seem to demonstrate an inverse relationship between their calculated score and their actual resemblance to the original painting, I think your scoring system may be broken.

– Ajedi32 – 2016-01-26T19:55:23.750


@Ajedi32 oh, I expected that to happen! I tried to post a version where the scoring system was based on human judgement (via voting) but it was deemed to be an "art contest" and closed. Doing it by least-squares is a fun challenge, but if we wanted the results to actually look like the image, there isn't really any other way than to rely on human judgement.

– Nathaniel – 2016-01-27T00:05:28.033

6I'm surprised that not a single answer has tried working with greyscale output yet. Averaging the channels in each pixel gives a score of something like 2800, and having only to compress a third of the data would introduce less error on top of that. – Martin Ender – 2016-01-28T10:16:05.417

3@MartinBüttner you could probably do even better by weighting a greyscale image by the average bluish colour of the image. I hadn't thought of this. – Nathaniel – 2016-01-28T10:29:29.510

3@Nathaniel hasn't been said enough, but this question is so awesome! ;-) – Pierre Arlaud – 2016-01-28T10:33:25.417

@Nathaniel Possibly, but I'm not sure it's worth it because then you probably also need more code to output a 3-channel image. – Martin Ender – 2016-01-28T10:36:26.220

@Nathaniel following your comment here, if you post a future challenge that rules out built in compression, it would interesting to see a score calculation that measures contrast in addition to colour similarity.

– trichoplax – 2016-01-28T13:48:16.810

@trichoplax yeah, I've been thinking about how best to do that. It's actually kind of hard to come up with a good measure that's both easy for people to understand and not too expensive to compute. – Nathaniel – 2016-01-28T14:22:52.430

@Nathaniel I commented here because I couldn't find you in [chat] but there are lots of helpful people there. If you can, pop in for a longer discussion :) – trichoplax – 2016-01-28T15:49:27.817


A metric that measures similarity in complexity might be more interesting. e.g. x264's psychovisual optimizations and adaptive quantization try to preserve energy in the source, because "detail" looks better than blurring, even if it's the wrong detail. SSIM is a widely-used visual quality metric. It's much more complex to compute than sum-of-squared-errors (PSNR). x264's tuning for max-SSIM (rather than the default max human visual quality) is --aq-mode 2 --no-psy, so SSIM still doesn't measure what psy optimizes for.

– Peter Cordes – 2016-01-29T03:57:23.873

x265's docs are good, and summarize what psy is doing there (similar to x264): psy = preserve the energy of the source image in the encoded image at the expense of compression efficiency. psy-rdoq = favoring higher energy in the reconstructed image (regardless of source energy). With these options at 0, the encoder mostly just blurs parts of frames that are hard to encode. (It's a video codec, so when they say "difficult motion", they mean big residuals that have lots of energy after motion compensation.)

– Peter Cordes – 2016-01-29T04:00:37.967

Is the term "objectively" defined anywhere here? It seems like an important part of the question. – bmm6o – 2016-01-29T16:38:58.487

@trichoplax I understand how the scoring is "objective", but I read this in the title as describing the program's implementation, not the scoring methodology. Also c.f. the comment to ndenarodev's answer: "for actually drawing objects, thus filling 'objectively' term in challenge", with 6 upvotes. It seems there's some agreement that his program is "objective", but it don't see an interpretation of the word that applies. – bmm6o – 2016-01-30T00:20:43.010

@bmm6o trichoplax has it right, it refers to the scoring method. I had recently posted a similar challenge (unfortunately closed) that used human judgement for the scoring, and I was afraid this would be seen as a duplicate if I didn't make the difference clear in the title. I guess I could remove the word "objectively" the next time I have a reason to edit the question. – Nathaniel – 2016-01-30T06:33:36.683

@bmm6o Ah I see - the comment on that answer does make it sound like objectively means "object based". I think the upvoters on that comment may simply be showing that they like the approach, rather than agreeing with the interpretation of the word. We'll never know though... :) – trichoplax – 2016-01-30T21:33:07.230

This TED-Ed lesson is highly relevant. – Digital Trauma – 2016-01-30T23:59:36.087

1Just to check, nneonneo's answer (score 5080) is currently the one to beat for the bounty, right? – 2012rcampion – 2016-02-01T03:10:16.690

@2012rcampion I believe so, yes. (That answer is definitely eligible for the bounty but it's been a busy weekend, so I didn't get a chance to check if any of the other new answers are. I think they aren't, though.) – Nathaniel – 2016-02-01T04:17:25.800

You won't be able to offer either 50 or 100 points on the next bounty. Bounty amounts have to (at least) double each time, so you won't be able to offer a bounty less than 200 rep. – Martin Ender – 2016-02-03T14:05:20.983

@MartinBüttner I didn't know that. Oh well, in for a penny, in for a pound - I've upped the amount. – Nathaniel – 2016-02-04T01:27:04.080

@Nathaniel: it seems it will be very hard to get any solution without built-in compression to beat BPG... – nneonneo – 2016-02-06T05:32:01.947

@nneonneo I agree, it will be very hard. – Nathaniel – 2016-02-06T10:04:31.337

1@Nathaniel you should update the current winner part (under the bounty part) with Strawdog's answer. – Rɪᴋᴇʀ – 2016-03-20T15:54:41.157

@Nathaniel: Necropost! I've added an optimization-based approach that blows the socks off my previous no-built-in-compression attempt, and also handily beats Strawdog's submission (which hadn't even seen, because I'd stopped looking at this thread ages ago). Anyway, although I'm pretty unlikely to be able to beat my own ZSH+BPG submission, I thought you might be interested: it beats a lot of other built-in compression schemes. – nneonneo – 2016-06-03T18:55:12.510

@nneonneo where's the new post? (If you can post a link it would help, this thread has got a little unweildy) – Nathaniel – 2016-06-03T23:53:15.383 – nneonneo – 2016-06-04T00:33:12.997



Pyth (no built-in compression), score 4695.07 4656.03 4444.82

Pyth’s only image-related functionality is a builtin to write a matrix of RGB triples as an image file. So the crazy idea here is to train a small deep neural network on the (x, y) ↦ (r, g, b) function representing the image, and run it on the coordinates of each pixel.

The plan

  1. Write a custom backpropagation loop in C++.
  2. Curse at how slow it is.
  3. Learn Tensorflow.
  4. Build a new desktop with a sick GPU using Black Friday deals.
  5. Scour the literature for ways to compress neural networks, and do that.
  6. Scour the literature for ways to avoid overfitting neural networks, and do the opposite of that.

The current network is built from 45 sigmoid neurons, with each neuron connected to the x, y inputs and to every previous neuron, and the last three neurons interpreted as r, g, b. It’s trained using the Adam algorithm without batching. The parameters weighting the 1125 connections are quantized to a range of 93 possible values (except the constant terms, which have 932 possible values) using a variant of stochastic quantization, the primary variation being that we set the gradient for quantized parameters to zero.

The result


The code

1023 bytes, encoded with xxd (decode with xxd -r). I used the 2016-01-22 version of Pyth that was current when this challenge was released. You can run the code directly in Pyth, but Pyth in PyPy3 (pypy3 pyth starry.pyth) runs it nine times faster, in about 3 minutes. The output image is written to o.png.

00000000: 4b6a 4322 05d4 7bb1 06f8 6149 da66 28e3  KjC"..{...aI.f(.
00000010: 8d17 92de a833 9b70 f937 9fc6 a74e 544d  .....3.p.7...NTM
00000020: 1388 e4e5 1d7e 9432 fe38 1313 3c34 0c54  .....~.2.8..<4.T
00000030: 89fe 553b 83a3 84bb 08c8 09fe 72be 3597  ..U;........r.5.
00000040: b799 34f8 8809 4868 feb8 acde 2e69 34e6  ..4...Hh.....i4.
00000050: 1c1a c49a 27f0 f06a 3b27 0564 178a 1718  ....'..j;'.d....
00000060: 1440 e658 e06a c46d aa81 ac3f c4b7 8262  .@.X.j.m...?...b
00000070: 398a 39e3 c9b7 6f71 e2ab 37e0 7566 9997  9.9...oq..7.uf..
00000080: 54eb eb95 0076 0adf 103c f34c 0b4e e528  T....v...<.L.N.(
00000090: a2df 6b4a 7a02 011a 10a9 2cf0 2edc 9f6f  ..kJz.....,....o
000000a0: 33f3 5c96 9e83 fadb a2fa 80fc 5179 3906  3.\.........Qy9.
000000b0: 9596 4960 8997 7225 edb1 9db5 435e fdd8  ..I`..r%....C^..
000000c0: 08a6 112f 32de c1a5 3db8 160f b729 649a  .../2...=....)d.
000000d0: 51fa 08e8 dcfa 11e0 b763 61e6 02b3 5dbb].
000000e0: 6e64 be69 3939 b5b2 d196 5b85 7991 bda5  nd.i99....[.y...
000000f0: 087a f3c0 6b76 b1d0 bb29 f7a4 29a3 e21a  .z..kv...)..)...
00000100: 3b1b 97ae 1d1b 1e0f f3c7 9759 2458 c2db  ;..........Y$X..
00000110: 386f 5fbb a166 9f27 2910 a1b5 cfcc d8db  8o_..f.').......
00000120: afaf bdb4 573d efb1 399b e160 6acf e14b  ....W=..9..`j..K
00000130: 4c6b 957a 245a 6f87 63c7 737d 6218 6ab2  Lk.z$Zo.c.s}b.j.
00000140: e388 a0b3 2007 1ddf b55c 7266 4333 f3a2  .... ....\rfC3..
00000150: d58f d80b a3a6 c6c1 d474 58f3 274b 6d32  .........tX.'Km2
00000160: 9d72 b674 7cc4 fdf6 6b86 fb45 1219 cc5c  .r.t|...k..E...\
00000170: 7244 396d 1411 d734 a796 ff54 cf1f 119d  rD9m...4...T....
00000180: 91af 5eab 9aad 4300 1dae d42e 13f8 62a1  ..^...C.......b.
00000190: a894 ab0b 9cb1 5ee2 bb63 1fff 3721 2328  ......^..c..7!#(
000001a0: 7609 34f5 fcfe f486 46e9 dfa8 9885 4dac  v.4.....F.....M.
000001b0: f464 3666 e8b9 cd82 1159 8434 95e8 5901  .d6f.....Y.4..Y.
000001c0: f0f5 426c ef53 6c7e ad28 60f6 8dd8 edaa  ..Bl.Sl~.(`.....
000001d0: 8784 a966 81b6 dc3a e0ea d5bf 7f15 683e  ...f...:......h>
000001e0: 93f2 23ae 0845 c218 6bdc f47c 08e8 41c2  ..#..E..k..|..A.
000001f0: 950e f309 d1de 0b64 5868 924e 933e 7ab8  .......dXh.N.>z.
00000200: dab7 8efb b53a 5413 c64b 48e6 fc4d 26fe  .....:T..KH..M&.
00000210: 594a 7d6b 2dd0 914e 6947 afa7 614d b605  YJ}k-..NiG..aM..
00000220: 8737 554e 31bc b21c 3673 76bf fb98 94f8  .7UN1...6sv.....
00000230: 1a7d 0030 3035 2ce6 c302 f6c2 5434 5f74  .}.005,.....T4_t
00000240: c692 349a a33e b327 425c 22e8 8735 37e1  ..4..>.'B\"..57.
00000250: 942a 2170 ef10 ff42 b629 e572 cd0f ca4f  .*!p...B.).r...O
00000260: 5d52 247d 3e62 6d9a d71a 8b01 4826 d54b  ]R$}>bm.....H&.K
00000270: f26f fe8e d33d efb5 30a8 54fb d50a 8f44  .o...=..0.T....D
00000280: a3ac 170a b9a0 e436 50d5 0589 6fda 674a  .......6P...o.gJ
00000290: 26fb 5cf6 27ef 714e fe74 64fa d487 afea  &.\.'
000002a0: 09f7 e1f1 21b6 38eb 54cd c736 2afa d031  ....!.8.T..6*..1
000002b0: 853c 8890 8cc0 7fab 5f15 91d5 de6e 460f  .<......_....nF.
000002c0: 4b95 6a4d 02e4 7824 1bbe ae36 5e6c 0acd  K.jM..x$...6^l..
000002d0: 0603 b86c f9fd a299 480f 4123 627e 951f  ...l....H.A#b~..
000002e0: a678 3510 912c 26a6 2efc f943 af96 53cd  .x5..,&....C..S.
000002f0: 3f6c 435c cbae 832f 316c e90e 01e7 8fd6  ?lC\.../1l......
00000300: 3e6d d7b4 fffb cd4a 69c7 5f23 2fe7 bf52  >m.....Ji._#/..R
00000310: 3632 3990 17ed 045a b543 8b79 8231 bc9b  629....Z.C.y.1..
00000320: 4452 0f10 b342 3e41 6e70 187c 9cb2 7eb5  DR...B>Anp.|..~.
00000330: cdff 5c22 9e34 618f b372 8acf 4172 a220  ..\".4a..r..Ar. 
00000340: 0136 3eff 2702 dc5d b946 076d e5fd 6045  .6>.'..].F.m..`E
00000350: 8465 661a 1c6e b6c8 595f 6091 daf2 103b  .ef..n..Y_`....;
00000360: 23ab 343a 2e47 95cf 4218 7bf5 8a46 0a69  #.4:.G..B.{..F.i
00000370: dabb 4b8d 7f9b b0c1 23b1 c917 839c 358c  ..K.....#.....5.
00000380: b33c de51 e41c e84d 12bf 8379 f4c5 65fa  .<.Q...M...y..e.
00000390: 0b65 7fe7 e1a0 fb0e 30f4 a7d2 b323 3400  .e......0....#4.
000003a0: 15e8 8a48 5d42 9a70 3979 7bba abf5 4b80  ...H]B.p9y{...K.
000003b0: b239 4ceb d301 89f8 9f4d 5ce6 8caa 2a74  .9L......M\...*t
000003c0: ca1b 9d3f f934 0622 3933 2e77 6d6d 2b4a  ...?.4."93.wmm+J
000003d0: 4b73 4d3e 332e 574a 615a 6332 3536 685e  KsM>3.WJaZc256h^
000003e0: 3463 732a 4c2d 3436 2e29 4a5a 3138 3739  4cs*L-46.)JZ1879
000003f0: 5b32 3739 6b33 6429 3338 3620 3332 30    [279k3d)386 320

How it works

  C"…"     convert the long binary string to an integer in base 256
 j    93   list its base 93 digits
K          assign to K

.wmm+JKsM>3.WJaZc256h^4cs*L-46.)JZ1879[279k3d)386 320
  m                                               320  map for d in [0, …, 319]:
   m                                          386        map for k in [0, …, 385]
     JK                                                    copy K to J
                                      [279k3d)             initialize value to [3*93, k, 3, d]
           .WJ                                             while J is nonempty, replace value with
                         *L      Z                           map over value, multiplying by
                              .)J                              pop back of J
                           -46                                 subtract from 46
                        s                                    sum
                       c          1879                       divide by 1879
                     ^4                                      exponentiate with base 4
                    h                                        add 1
                c256                                         256 divided by that
              aZ                                             append to value
         >3                                                last three elements of the final value
       sM                                                  floor to integers
.w                                                     write that matrix of RGB triples as image o.png


During my final training run, I used a much slower quantization schedule and did some interactive fiddling with that and the learning rate, but the code I used was roughly as follows.

from __future__ import division, print_function
import sys
import numpy as np
import tensorflow as tf

NEURONS, SCALE_BASE, SCALE_DIV, BASE, MID = 48, 8, 3364, 111, 55

def idx(n):
    return n * (n - 1) // 2 - 3


sess = tf.Session()

with open('ORIGINAL.png', 'rb') as f:
    img =, channels=3))
y_grid, x_grid = np.mgrid[0:img.shape[0], 0:img.shape[1]]
x = tf.constant(x_grid.reshape([-1]).astype(np.float32))
y = tf.constant(y_grid.reshape([-1]).astype(np.float32))
color_ = tf.constant(img.reshape([-1, 3]).astype(np.float32))

w_real = tf.Variable(
    np.random.uniform(-16, 16, [WEIGHTS]).astype(np.float32),
    constraint=lambda w: tf.clip_by_value(w, W_MIN, W_MAX))

quantization = tf.placeholder(tf.float32, shape=[])
w_int = tf.round(w_real)
qrate = 1 / (tf.abs(w_real - w_int) + 1e-6)
qscale = 0
for _ in range(16):
    v = tf.exp(-qscale * qrate)
    qscale -= ((1 - quantization) * WEIGHTS - tf.reduce_sum(v)) / \
        tf.tensordot(qrate, v, 1)
unquantized = tf.distributions.Bernoulli(
    probs=tf.exp(-qscale * qrate), dtype=tf.bool).sample()
num_unquantized = tf.reduce_sum(tf.cast(unquantized, tf.int64))
w = tf.where(unquantized, w_real, w_int)

a = tf.stack([tf.ones_like(x) * 256, x, y], 1)
for n in range(3, NEURONS):
    a = tf.concat([a, 256 * tf.sigmoid(
        tf.einsum('in,n->i;', a, w[idx(n):idx(n + 1)]) / SCALE)[:, None]], 1)
color = a[:, -3:]
err = tf.reduce_sum(tf.square((color - 0.5 - color_) / 255))

train_step = tf.train.AdamOptimizer(0.01).minimize(err, var_list=[w_real])

count = 0
quantization_val = 0
best_err = float("inf")

while True:
    num_unquantized_val, err_val, w_val, _ =
        [num_unquantized, err, w, train_step],
        {quantization: quantization_val})
    if num_unquantized_val == 0 and err_val < best_err:
        print(end='\r\x1b[K', file=sys.stderr)
            'weights', list(w_val.astype(np.int64)),
            'count', count, 'err', err_val)
        best_err = err_val
    count += 1
        '\r\x1b[Kcount', count, 'err', err_val,
        'unquantized', num_unquantized_val, end='', file=sys.stderr)
    quantization_val = (1 - 1e-4) * quantization_val + 1e-4


This picture shows the activations of all 45 neurons as a function of the x, y coordinates. Click to enlarge.

neuron activations

Anders Kaseorg

Posted 2016-01-23T15:16:57.587

Reputation: 29 242

Have you considered trying to add a convolutional layer or two to the network? I think it'd be better at getting the noisier aesthetic. Alternatively, you could try additional manual-crafted features like [x^2,y^2] or [x%5,y%5] to get those lined patterns. – Steven H. – 2017-12-04T05:51:33.730

4@StevenH. Maybe? I haven’t developed much intuition for convolutional networks, but my assumption is that, although something along those lines could probably improve the result visually, to improve the pixel-difference error metric used in this challenge you’d need to encode a lot more information to line up the noisy patterns fairly precisely with the original. – Anders Kaseorg – 2017-12-04T07:15:29.630

Hm... Yeah, I'd still recommend considering adding cyclic features like [x%5,y%5], since they're a) easy to represent in Pyth code and b) hard to attain using a vanilla neural network. – Steven H. – 2017-12-04T08:14:07.270

9Oh my god this is amazing! I was hoping someone would use a deep neural network approach, and you really went the extra mile to do it. This is also, without a doubt, the coolest looking of all the answers. I'm giving you the green check mark, at least temporarily, to draw attention to your answer. – Nathaniel – 2017-12-04T10:02:49.930

What are the updates? Are you just continuing to train the same network, or are you tweaking the code? (I'm just curious.) – Nathaniel – 2017-12-13T00:54:32.110

2@Nathaniel For now I have been training the same unquantized network (which currently scores about 4350), but working on tweaking the quantization strategy. The bulk of the improvement in this update came from allocating two digits instead of one to the constant term of each neuron. – Anders Kaseorg – 2017-12-13T01:19:09.687

2How long did the training take for this (disregarding quantization)? – orlp – 2018-06-25T14:29:31.337


Mathematica, score 14125.71333


Saves this image:

to a.png.


Posted 2016-01-23T15:16:57.587

Reputation: 15 731

7Can you give us some details? Did you brute force the color to minimize the score? Or is there some simple math involved to find out that color? Will any color do? – Thomas Weller – 2016-01-24T22:34:27.490

28@ThomasWeller I just took the mean of each channel. – LegionMammal978 – 2016-01-24T22:54:34.340


Per the Help Center, all submissions must be serious contenders for the winning criteria in use. This is clearly not a serious submission. Joke answers are not welcome on this site.

– Alex A. – 2016-01-27T00:23:23.963

33@AlexA. There were no other answers, so I decided to create a simple reference solution to get started. A non-serious submission would be my original all-white-pixels idea... – LegionMammal978 – 2016-01-27T01:06:43.120

63I hope people know that a lower score is better in this challenge. – Calvin's Hobbies – 2016-01-27T05:24:33.137

47@AlexA. I disagree. This is a valid, if simplistic solution. I doubt there would be any call to remove it if it wasn't so highly upvoted or if it were only one of a couple answers. – Calvin's Hobbies – 2016-01-27T05:30:13.303

20@AlexA. Hum. I would have naïvely thought that this answer will actually be quite close to the best. Looking at the other answers I can see that I was somewhat wrong but it doesn’t invalidate the approach and it’s most certainly not a joke answer. In fact, it’s a close illustration of how many simple yet practical statistical fitting methods work. Furthermore, the fundamental approach is very similar to other, better answers: compress the image by a factor N. – Konrad Rudolph – 2016-01-27T13:04:19.887


This reminds me of my submission to the TopCoder Enigma match, which just returned a string of es with some as (for input e) for the decoded string. I beat half of the other submissions.

– MooseBoys – 2016-01-27T20:59:38.547

35Personally I think this is a fine answer, well within the spirit of the question. – Nathaniel – 2016-01-28T10:13:19.563

1If you use #4B6081 instead, this might score slightly better. That's the average color of the image. – ArtOfCode – 2016-02-07T21:59:35.760

@ArtOfCode How did you calculate that? – LegionMammal978 – 2016-02-08T00:12:13.480

@LegionMammal978 JS colorThief library. Google it, should come up pretty high. – ArtOfCode – 2016-02-08T00:14:09.907


Java, 7399.80678201

This reminded me of a project I had in my numerical computations class a few semesters back, which was to draw a silhouette of Mount Everest using polynomial interpolation. That was done in MATLAB, but I'm not very fond of MATLAB, so I decided to work in Java. The basic idea behind it is that I chose "smart" points (read here as "random") for the polynomial interpolation. With the few bytes I had left, I created a way for the stars to be drawn, which happens before the drawing of the mountain. It may be possible to condense the code and add another polynomial for the bottom to help better the score.

Edit: I've made added and changed some of the polynomials and added all of the stars. My previous score was a 9807.7168935, so as you can see it is a very big improvement. Unfortunately the code took a hit in its readability because I had to squeeze out the last few bytes to get all of the stars, and give them groups.

import java.awt.image.*;
public class Y{public static void main(String[]a)throws Exception{
int w=386;int[]s={5819,18,5530,9,8644,7,11041,16,21698,14,22354,40/**/,4326,4,29222,14,40262,9,56360,8,59484,12,65748,24};
int o=p.length;BufferedImage b=new BufferedImage(w,320,1);
int i=0;for(;i<o;i++)
{if(i==o-2)for(int j=0;j<s.length;j+=2)for(int l=0;l<w*320;l++)if((l%w-s[j]%w)*(l%w-s[j]%w)+(l/w-s[j]/w)*(l/w-s[j]/w)<s[j+1]*s[j+1])

b.setRGB(l%w, l/w,j<s.length/2+1?j==10?-5788556:-8944525:-7036782);

for(int l=0;l<w*320;l++){int m=0;for(int y=0;y<p[i].length;y++)m+=Math.pow(l%w,y)*p[i][y];if(l/w>m)b.setRGB(l%w,l/w,c[i]);}

9807.7168935 points:
submission image
7399.80678201 points:
New submission image


Posted 2016-01-23T15:16:57.587

Reputation: 1 191

5This is neat! Welcome to PPCG! – Mego – 2016-01-26T05:25:33.740

4If you converted this into Groovy, you could strip some of the boilerplate and get a few more operations in. – chrylis -on strike- – 2016-01-27T02:06:52.093

3That looks a lot like it's from the intro of good old Day of the Tentacle. – moooeeeep – 2016-01-27T18:23:38.613

13+1 for actually drawing objects, thus filling 'objectively' term in challenge – prototype – 2016-01-28T04:08:03.753

3From the drawing objects solutions this is actually the nicest one. – Trilarion – 2016-01-28T12:01:00.927


Python3.4+, 4697.26

I used the same method as in my ImageMagick answer, but with the following parameters:

convert ORIGINAL.png -filter Lanczos2 -resize x32 - | pngquant --speed 1 -f 20 > i

Using these parameters I generated the following 1003 byte Python program (I didn't find any improvements over @kennytm's output method):

import base64,io,PIL.Image'iBL{Q4GJ0x0000DNk~Le0000d0000W2m=5B0H%16zW@LLJWxzjMIIp@MPHIFJ#$E2f;B{2CoMBscAryil4g95I81|Zh@Ln<KPWOvf|rw@ter+yW3aV*evEZoY;v=uM0+bp*#H0nK}keGRCobZkq5FQAq+ze25eH(F!#UfO3bFOD)N&<AyN0%G0qy;d5u*~Ym6yks#+BaTB}6=d_2Z;Vzlk&;0~~Hy_i{>+fAa)ZE?UH1H4;-#jy7d<b%%Vecw3+`%q(!07-C(^lrsAlm_hcKIHhG&w-n|TvQA6gffYd`#$#lIaq_aH#b${0Rr~{_dXIXm(EsD=KF%BDY~EjjR!sA$`(cljOEjUsu$E%b-uF%pXA;tc+?QOHOg1TfuI?XhSeXYZ9>3XUzpreH6$%YFH{Ofn-d0cv*IqjHQQU+M=7PX5(65sP+;Sbo9=+q2n+8p!-Vw8kW>i%<+}8cQmp`7FKsM?maF*)_e&%vv>nKK;O9GnTVux79!MU;XJ(P4OxFsdP$0<JBn1vPFS#C^=MtEd4K#KWfKgNUS4R-<e)-d?qU?a%g3wBDo2O@CR|#C_9XJLv7TEYbh}4|ri%<P>OPRyX9b--+5K2xN0yhh}oR+t?_naKqIwRUjV<bqt&A9{``7{7G;88q{R8t-~qEriF@LeuTcV}g0x*{|NCUjH^IW_2VS#ngo0;)A8{!h?a6_~1|sx=8kcHC;BrkG+))LLgje9A;921P%7)U^m<Ugv<kAte9fZUa4$W^i)!NGB(M$&qu%TOOqPB^PW9T<Y?FV(GmvdNZJ&G2hY#uH^@ah#eF6sjt;LYY<+_4}trV444*z;!N%*2#R9%)CbWUy<g$^9|zqjA?NsQ`UPFmBW7cjo=pG%002ovPDHLkV1f'))).convert().resize((386,320),1).save('o.png')

Which in turn generates this image:

starry night, compressed


Posted 2016-01-23T15:16:57.587

Reputation: 37 067

3Surely it would be better to store the data using all 256 bytes instead of base 64, 85 or whatever. – feersum – 2016-01-23T23:08:56.433

@feersum I don't know how to store binary data inside Python files in a better way than base encoding. – orlp – 2016-01-23T23:09:54.587

Score confirmed and validated - this is the winner for now. – Nathaniel – 2016-01-24T02:06:05.903

13@Nathaniel You only asked this today so I suggest you don't mark this answer (or any answer) the accepted answer yet. When people see an answer has been accepted they may assume the contest is over and won't participate. I think it's best to wait at least a week or two before accepting any answer. – Calvin's Hobbies – 2016-01-24T07:01:15.473

@calvin'shobbies alright, that makes sense, I've unaccepted for now. (Sorry orlp) – Nathaniel – 2016-01-24T08:16:11.010

@orlp possible idea (no idea if it is reasonably golf-able): store it at the end of file inside a tripe-quoted variable and use slicing on the read? eg: r=open(__file__).read();f=r[r.index('a="""',75)+5:-3];RESTOFCODE; a="""(BINDATA)""". I am away for a while though from any decent python interpreter to test with though. Some work would be needed to slightly convert the binary to something one of pythons accepted source code encodings would allow... Probable still that this would take more bytes than you would gain... – admalledd – 2016-01-25T22:52:32.690

Python allows Windows CP-1252 as an encoding, which is pretty dense, so it should be possible to do a lot better base 85. Say, base 240-ish or more. But like admalledd says, there's the issue of whether the code to decode that is sufficiently golfable to fit in the space freed up by the more compact encoding. – Steve Jessop – 2016-01-27T13:24:44.617

@feersum, admalledd: See my answer for a technique that allows you to embed arbitrary binary data in a triple-quoted string which is directly usable by the program. The only characters that need to be escaped are the backslash and null bytes (and quote characters, if you might have 3 in a row).

– nneonneo – 2016-01-28T08:08:40.043

@nneonneo Yes, I have used this technique before. Looks like you could save 2 bytes from it.

– feersum – 2016-01-28T08:17:08.760

1Yes, I can remove 1 from latin1 and save a whitespace from import *, at least. But golfing that wasn't a priority (since it is less than 1024 bytes). – nneonneo – 2016-01-28T08:18:57.493


Python 3, score 5701.31

import base64,io,PIL.Image'iBL{Q4GJ0x0000DNk~Le0000I0000D2m$~A04pr1P5=M`lSxEDRCrzu%wJF2MgRctyYrpz&VS-04oPU$02O1RYA2$KKm9YcYEUcDJyh+N==-F7oxW_3ecQvNY1*VQwC-UKjIkjQ8wn&8)Ukt&?VNpg?HBmL%~#(%>G5yhoON#6r~Pv6ekHzgk~o3d4gyuJL`u_K5Ca>QWmKD%@0G*T5|2ap)6YJolVN{skh#D2-J*z9sSwJGT&-<XKaADxALs3bTgxl>{_=mG9vyzZf!umCPV=nDpY_PnlC(U%vg$wXwH`cZEO(oS$M*5D=xu#wxzx3!2Zo>s9W*kYzx=A=N>*_{sW%pHRqD>^@zKSEq$TGjvKY#BrP)@HRR163l}43&{y(F((e1rtc2!^X2mO6y&o}IoM9kipbs;pk<JuNalW`QGD9ugFJ{UH3_MAMEM)%9DOccA@Z&M+Kv__Mexy}qD>itE08H7@&wK|if_I*zRFxSRUz9?^ZFg`m^?krP_gDeQ=7dy{4cvhr8><E*y5|gu_xM_1{!!*9Udz<+_jw1Qn_8z|8D0lw?VUSJ>bv`&qy7v&oe)U74a}D9r0<?Kbh$WO6X2sFFvtPgeQELMW#a8ZE``e1>Z20f*zxY<|S}V_D!tCjlc()f7Q<cT<xpz%u5MkbZ`g)qD@ZpQEom%VU&+p<WAef3W^P20qpiYIF)WfEkB@uUOn2ZrL7{m3tn}q|oboY^-PGxZcw3I>uj1fd$ZJs(Vr6?~D<kd795dbx^Ypn(<BaJBRu5S{v9QF?lgFv8^lE5$vMH@2~1P&$UXmDz~jMc9@hDb6+sUt-RA3a?K08cLC;5>#H%1mH_FaU0|12sV9)OKBnxI$()5d@YBM2xYIb)G!>9@_oL00000NkvXXu0mjf'))).resize((386,320),1).save('a.png')

Simply rescale from a 18×13 PNG image.

enter image description here


Posted 2016-01-23T15:16:57.587

Reputation: 6 847


Java, 8748.95

Another approach:

I created a class that computes a Voronoi Diagram from a given set of points. This set of points is used as the parameter set that serves as the input for the Apache BOBYQAOptimizer. The evaluation function of the optimizer takes the points and creates a voronoi diagram from them. The voronoi regions are colored with the average color of the corresponding region of the original image.

The optimization process is shown here:


The final image is this one:


which achieves are score of 8748.95

(This was measured with my own function, but should be the same as with the evaluation script)

The result of this process is only a set of 8 points and the corresponding colors. (A higher number of points caused worse results, but I didn't try this out really extensively).

The resulting code is shown here (sorry, I had to golf it a little to squeeze it into the 1kB limit) :

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.List;
import javax.imageio.ImageIO;

class V { public static void main(String[] args) throws Exception {
BufferedImage i = new BufferedImage(386,320,2); f(i, Arrays.asList(
)); ImageIO.write(
i, "png", new File("x.png"));}static void f(BufferedImage i, List<Integer> p, 
List<Integer> c){int x,y,r=0; Point q = new Point(), a=new Point(); for (y=0; 
y<i.getHeight(); y++) { for (x=0;x<i.getWidth(); x++) { q.x=x; q.y=y; double 
d = 1e10;; for (int j=0; j<p.size(); j+=2) { a.x=p.get(j); a.y=p.get(j+1);
double s = q.distance(a); if (s < d) { d = s; r = j/2; } } i.setRGB(q.x, q.y, 

(I know, there are some easy ways that could have achieved better results, but ... this somehow was fun...)


In response to the comment, regarding the artistic style of such a voronoi image with a larger number of points: This indeed looks interesting, and some imaging tools actually offer this as a "Mosaic Filter" - for example, the Mosaic Filter in GIMP (although this offers options to emphasize the edges etc.).

Here is an example of the Starry Night image, with 256 points. (These are selected randomly, but with a larger number of points, the improvement that could be achieved by an optimization will vanish).

This is not part of the contest (as it does not fit into 1kB), just for the curious:



Posted 2016-01-23T15:16:57.587

Reputation: 1 131

5+1 Voronoi is a good idea, with more points I think the image will look pretty good subjectively speaking (at least the art style should be interesting). I'll probably try it out in a bit if you don't mind. – neocpp – 2016-01-26T20:28:50.783

@neocpp I added a short paragraph and an example image with a larger number of points. – Marco13 – 2016-01-28T18:09:40.863

You can squeeze a few more bytes using import java.util.*; In fact change all import classes to asterisks. – Chloe – 2016-01-29T00:53:23.380

@Chloe I tried this, to some extent: Increasing the number of points to 10 or 12 caused a lower score in the end. It's certainly possible to tweak the optimizer to achieve a higher score there, or golf the code even more and then use maybe 14 points (and then possibly achieve a higher score with the current optimizer configuration), but there are far too many degrees of freedom to explore this exhaustively. I just thought that this Voronoi thing may be a neat idea, and tried to achieve a "good" result with reasonable effort. – Marco13 – 2016-01-29T10:18:52.923

I don't quite know how the optimization works (it looks deterministic to me), but it may help to run a few cases with random starting vectors if it's fast enough. At least when I ran my GA with 8 points it consistently found solutions which were a few hundred points below what was posted, and increasing the number of points seemed to produce strictly better solutions. – neocpp – 2016-01-29T13:10:00.807

@neocpp There are different optimizers in this package. I tried others, some use only some gradient-descent, and some use GA-like approaches as well (at least that's what I guess, when they have a parameter called "population size"). And that's the problem: The algorithms have many parameters, and their meaning is not always obvious. (I even considered writing a meta-optimizer, that tries out different parametrizations for the optimizers, to see which parametrization brings the best end result ;-)). It's a complex topic, and I'm no expert. BOBYQUA worked ok-ish, so I used it – Marco13 – 2016-01-29T14:43:36.720

You should make a library for this – DeveloperACE – 2016-02-13T22:21:09.063

@ACE What exactly should this library do? For the "fine grained" voronoi pattern, there already are sophisticated image manipulation programs. The optimizer was straight from the apache library. And beyond that, I hardly see a realistic application case for "creating a voronoi diagram (with few points) that resembles an image". – Marco13 – 2016-02-15T20:04:50.993

@Marco13 what app did you use to write this code in? – DeveloperACE – 2016-02-19T02:56:53.930

@ACE I'm not sure what the term "App" refers to here, but I assume that the answer is "Eclipse" ... – Marco13 – 2016-02-19T09:56:44.567

Yeah, I'm just trying to find out how I can run this on my computer... – DeveloperACE – 2016-02-19T13:51:37.230

@ACE I see. If you have Java, you can just type javac to compile it, and java V to run it, and it will create the image. (But to avoid misunderstandings: Of course this does not do the Voronoi computation or this animation thingy. It just runs the image generation program, using the Voronoi points that I computed with a (larger) program that employs the techniques that I described). Maybe I can throw this on GitHub as well, but it's rather messy, it would take some time to clean it up, and I'm rather busy with other stuff ATM). – Marco13 – 2016-02-19T13:59:31.320

If you could put it on github that would be great! I'm in no hurry so take your time. – DeveloperACE – 2016-02-19T14:01:24.313

You could golf a lot though removing whitespace so you can try to optimize further. – Mega Man – 2016-08-07T08:19:28.763

@MegaMan I only had to golf it a little, to obey the 1024 bytes limit. Golfing it more could allow me to use maybe 9 or 10 points instead of 8, but I tried this, and the results had not been better then. Again, this was mainly recreational/experimental to try out the path of Voronoi+Optimizer, knowing that there are other approaches that could yield a higher score. – Marco13 – 2016-08-07T14:53:35.620


AutoIt, 9183.25 7882.53


So it turns out that redrawing the image like a (drunk) toddler is more effective than storing any version of the image. (More effective than my old solution anyway).

Every line that draws an element is crucial to decreasing the score. I suspect this program is capable of achieving scores well below 7000 with very minor modifications, because every single change has huge (~20 to 100 points) effects on the score. The program uses my processing graphics library that provides terse function names for drawing using GDI.

Since this solution involves randomness, we seed the PRNG using the constant value 0 using SRandom(0). Why 0? Because it is up to 50 points better than any other n<=100 I tried.

The canvas starts out as a blank #587092.

Generating the floor

The lower part of the image (which it turns out begins at exactly 233px [again, because of points]) is filled with exactly int(1e4*2.9) ellipses. Changing the factor here (or a decimal place of the factor) can de- and increase the score by hundres of points. I settled for 2.9 after a few tries. Naturally this will take some time (few seconds).

A five-color palette is supplied:

Dim $3=[0x202526,0x48555A,0x394143,0x364458,0x272E3A]
For $n=1 To 1e4*2.9

Blobs on the floor

Four ellipses are used to set contrasty accents inside the floor area ($4 is a function pointer to ellipse()):


Generating accents in the sky

Some lines are drawn using a thicker pen to represent significant color areas inside the sky that a stretched too much for ellipses:


Weighing the pixels

After the above, all is rinse and repeat until we run out of bytes. Then a blur is applied to trick the validation method. By brute force, it has been determined that a radius of exactly 20 provides the best result. This improves the score by round about 1.5k(!).

Final image

toddler drawing

Code, 985 bytes

Dim $3=[0x202526,0x48555A,0x394143,0x364458,0x272E3A]
For $n=1 To 1e4*2.9


This stores 80 color values which make up a 10x8 px picture. This raw picture has a score of 10291. Since 10x8 is a pixelation factor of 40px, a Gaussian blur is applied using a radius of 40px to lower the score. This is how the script achieves 9183.25.

This is the stored data:

raw data

The file produced is True.png:


The program is 998 bytes long:

For $3=0 To 7
For $2=0 To 9


Posted 2016-01-23T15:16:57.587

Reputation: 7 398

8Hope you haven't tested the drunk toddler part of the answer. – mgarciaisaia – 2016-01-26T23:30:54.573

5+1. I don't see the challenge in using a tool to base64-encode the original; to me that seems like a lazy solution. This, I give points to! – Chris Cirefice – 2016-01-27T13:47:31.447


Also, I made an autotracer unrelated to this challenge. The output is quite good for this picture, and afaik it is the smallest possible SVG without compromising quality. If any can minify this SVG any further, please ping me.

– mınxomaτ – 2016-01-28T18:26:54.547

1It's only a three-byte savings, but isn't 1e4*2.9 equal to 29e3? – 2012rcampion – 2016-02-01T05:21:13.027

@2012rcampion It's not code golf :) . Anyway, I wanted to leave that there so one could easily change the second factor to improve the score, I might return to that answer at some point. – mınxomaτ – 2016-02-01T05:27:39.900

1Well, every byte you save gets you closer to adding another primitive. Using it for optimization is a good point though. – 2012rcampion – 2016-02-01T05:32:05.833


Windows BAT file, score 4458.854

echo QlBH+yAAgwKCQAADkkdARAHBcYMSAAABJgGvBVKInJKSe4D9mGo5+oRwrhSlmmqeYK22qun5kDzV+UZhRPdtXWSME8ABlNItkdoM5b0O7jO01KMnUbhSa4GAKq6U/AWBh8J4Od/O0RKwm2Bj1lAWi3yfWb9AB14B9/aml7juRU0fQTVS9LUQxE1eXTfp6f2SdBh9Ibyk3CNjdpEGdZLsuSPaQUP0vWnqtxyBsYQW1orBqzSh4zWFscTx5OMxA4FAw1/Y+/xx+TEUkogp4oykebVfCTFJYFRW6KZ+tvUOb5nFgrIuYbNrZcnWehWOK3rL7i2qCYJ2TnSlwKt4WL04zXve3ggGxAWlD/N6YCchdgS8zaZfVxouhwjbwc1Pb/KAajfGQlv7xHhj42ClMPGeqEZrriJTlLX8GUXpt8RP0LlbVR+PtgPRFerFRzJwTB5ThASKsaKt4LLSQqCXjgJvL2epSQaxq2IJkLelVTqate10dIngfVJqUL2r7omvwQ6N9DWi3ZiF6cRc4PMdPp4Ovo7nM/dlOn1CQ1sOp3mrP12EhGdiGvRsEqdt/jHC1roK5yJVv/L2bAOxK1EJ8qJqaApF7W1VY5htmci8C10UE5iTiBYcPzh8oxPUqXp9+wXgRsDY2sdIo6hOvp8IC9jQXkbDK2lJZjJvcwklTSFah/yqf6biaIOLTtHpEonH1jYXOS4dPzt6oNExlmJztgVFjbqlnB7k3i/mm2UL4+IPjgMMOmH+fwluY+cDA2zG+QtVDGwhSaEEvS9B7L2VkayIuEXKpk9pu/58xwlw4/xQOJE1QGVvSi6uL7DEH4qsumBOGAs50DRu2pCSGfa4c+wn3M0DuFM1p/iRFnq+aNk2PsPc00k8EI5i4QOt/+ac+zKmgEztiz7yI+FzIVKGnn32wLkHVu1ei3VhkWhMy8xlUTAq+fBreWQY > s.b64
certutil -decode s.b64 s.bpg
bpgdec.exe -o stnight.png s.bpg

Program size is 1024 bytes.
Converts from base64-encoded BPG image to PNG.
Uses certutil.exe (standard Windows utility) and bpgdec.exe image decoder as libraries.


  1. The original image was blurred (Gaussian Blur filter, radius 2 pixels).
  2. The result was encoded as BPG with bpgenc.exe (Quantizer = 50).
  3. Binary image converted to Base64.

Starry Night


Posted 2016-01-23T15:16:57.587

Reputation: 551

2Looks like we have a new winner! (This one indisputably does meet the spec.) – Nathaniel – 2016-01-27T13:31:57.423

9At this point it looks as though winners consist of finding good image compressions of the original (by whatever methods), with the program that's subject to the golf rules being trivial. That is to say, nobody here has any better ideas about the Kolmogorov complexity of this particular image, than what already exists in the best available image formats. Although it's interesting that this answer "looks less like" the original (to me) than Adam's does. – Steve Jessop – 2016-01-27T13:32:54.290

5@SteveJessop yeah, it's a shame, not really what I'd imagined the challenge would be like. Mostly it's because I wrote a rule about not using off-the-shelf compression methods but forgot to paste it into the challenge. I'm really interested in coming up with challenges that admit algorithmically interesting answers, and I've learned my lesson from this one. – Nathaniel – 2016-01-27T13:37:24.550

3@Nathaniel: yeah, I think the issue is that BPG (or any image library) contains k's and k's of code implementing useful operations for image de/compression, all of which is available for free per the rules of this question. Beating that expertise in 1k of code, starting from scratch, is a big ask. But it's still interesting to learn that, as it turns out, the advantage of only needing to handle one particular image doesn't outweigh that. – Steve Jessop – 2016-01-27T13:44:35.850

1@SteveJessop it's also interesting that of the entries that don't use built-in compression, the best ones just store a low resolution raster image and then blur it - the interesting optimisation-based solutions can't really compete. However, all of this may in fact be a property of the source image after all. Starry Night contains many large areas of fairly uniform colour, and is already somewhat blurred in appearance, making it ideal for such techniques. If I'd chosen an image with lots of high-contrast detail, different solutions might have been more effective. – Nathaniel – 2016-01-28T09:48:04.777


C++11, 7441.68126105 6997.65434833 5198.16107651

More Updates

I liked the ellipses from Perl so much I had to try them in C++11. I used the raw string to shove bytes into there, but for a while I was getting a slight discrepancy with the score I expected and the generated code. It turns out that you actually can't put a raw 0x0d (Carriage Return), since g++ will convert this to 0x0a (New Line). I'm honestly not sure how legitimate this generated source is, but it compiles and works on a couple of my machines.

I also tried out another algorithm, Adaptive Dimensional Search after the GA looked like it stalled, just to try to polish off the local minimum and maybe get lucky and fall into another well.

With this, C++11 gives a surprisingly competitive score (far better than I would have initially guessed)... I'm pretty surprised that it can do this with fstream as the only include.

Text (yes, the newlines are in the actual source... I suppose I could remove them):

#include <fstream>
#define U unsigned
int main(){
auto *d=reinterpret_cast<const U char*>(R"(<<gibberish>>)");
U a=320,b=386,n=*d++;
char m[a*b*3]{0};
for(U i=0;i<n;i++,d+=7){long x=2*d[0],y=2*d[1],w=2*d[2],h=2*d[3];
for(U r=0;r<a;r++){for(U c=0;c<b;c++){long u=c-x,v=r-y;
if((w*w*v*v+h*h*u*u)<=w*w*h*h){auto *p=m+3*(r*b+c);*p++=d[4];*p++=d[5];*p=d[6];}}}}
std::ofstream f{"e.ppm",std::ios::binary};f<<"P6\n386 320\n255\n";for(U i=0;i<a*b*3;i++){f<<m[i];}
return 0;}


00000000: 2369 6e63 6c75 6465 203c 6673 7472 6561  #include <fstrea
00000010: 6d3e 0a23 6465 6669 6e65 2055 2075 6e73  m>.#define U uns
00000020: 6967 6e65 640a 696e 7420 6d61 696e 2829 main()
00000030: 7b0a 6175 746f 202a 643d 7265 696e 7465  {.auto *d=reinte
00000040: 7270 7265 745f 6361 7374 3c63 6f6e 7374  rpret_cast<const
00000050: 2055 2063 6861 722a 3e28 5222 2851 1274   U char*>(R"(Q.t
00000060: 5134 8c86 6c7f 2ea0 3638 4c8b c001 c126  Q4..l...68L....&
00000070: 6e84 9500 480b 2964 778f 0196 5c09 353d  n...H.)dw...\.5=
00000080: 346f 476e 6433 4581 0f02 0509 9798 4d12  4oGnd3E.......M.
00000090: 0110 0362 7482 6300 4d1f 2631 645b 213d[!=
000000a0: 187e 835c 6f84 333d 2c3e 4f9d 71bb 1e22  .~.\o.3=,>O.q.."
000000b0: 2d3d 1f4f 0248 2424 235f 577e 1f71 8990  -=.O.H$$#_W~.q..
000000c0: b314 3a89 404a 5920 1202 0c23 242a 8e01  ..:.@JY ...#$*..
000000d0: 6d30 3645 7145 86b0 082c 3543 4d42 1f52  m06EqE...,5CMB.R
000000e0: 6879 7c7a 336d 1a37 4c82 b876 b606 3146  hy|z3m.7L..v..1F
000000f0: 70a1 015e 0b38 4b7f 0e46 a916 4360 8550  p..^.8K..F..C`.P
00000100: 1623 0930 407c bf13 6e73 4556 6252 9837  .#.0@|..nsEVbR.7
00000110: 4326 2c31 7d81 3303 2e3c 526c 4123 4b37  C&,1}.3..<RlA#K7
00000120: 4758 bd6f 8b0a 2d3c 6000 0006 1b2c 3a6b  GX.o..-<`....,:k
00000130: a83a 134f 4254 6649 590e 174a 6986 3833  .:.OBTfIY..Ji.83
00000140: 0a29 3245 8695 1d27 583e 507f 963c 2b33  .)2E...'X>P..<+3
00000150: 2f3d 6fb6 191f 6752 5f63 b09e 5b0c 3239  /=o...gR_c..[.29
00000160: 4021 4b20 1941 5c87 ab18 1c1e 4a5f 8c35  @!K .A\.....J_.5
00000170: 9d19 311d 211e af4b 3327 4f64 986c 2712  ..1.!..K3'Od.l'.
00000180: 573b 4b73 b733 a718 5f76 9ca9 2919 2163  W;Ks.3.._v..).!c
00000190: 7e9e 8147 8914 8996 726b 1c17 1670 807b  ~..G....rk...p.{
000001a0: 5038 930e 6279 94b0 351d 3086 9b8e ba40
000001b0: c10e 3449 6721 4002 232f 394e 22a0 0e74  ..4Ig!@.#/9N"..t
000001c0: 2b2f 2c09 3d0e 1666 7e97 0570 2e05 526d  +/,.=..f~..p..Rm
000001d0: 8a68 1e2f 0a40 5586 bf5d 150c 2022 2e5e  .h./.@U..].. ".^
000001e0: 260e 4b3a 4a7d a368 3807 4c63 972b 5707  &.K:J}.h8.Lc.+W.
000001f0: 2e41 5a79 865e 3c06 2326 3927 9d0e 411d  .AZy.^<.#&9'..A.
00000200: 211d c030 9b16 657f 9666 2434 0a5f 7592  !..0..e..f$4._u.
00000210: 873b 0a1d 8895 89a9 432e 0aa2 aa95 af1d  .;......C.......
00000220: 1212 aab1 7c80 5833 162c 3758 834d 3117  ....|.X3.,7X.M1.
00000230: 718b 9579 2a06 163e 5381 8439 3b0c 5172  q..y*..>S..9;.Qr
00000240: 9d54 3a16 1538 4e73 8c4f 1f0e 8fa2 9ab0  .T:..8Ns.O......
00000250: 200b 07b8 a946 5e40 1e19 5971 9457 5028   ....F^@..Yq.WP(
00000260: 125b 779b bb49 1a07 a1ad a022 7b0a 421f  .[w..I....."{.B.
00000270: 231f 585e 200f 5f77 8a41 5b0e 136a 8089  #.X^ ._w.A[..j..
00000280: 9ca0 9d01 5648 3a40 550c 0c9f a89e 7841  ....VH:@U.....xA
00000290: 2a19 566f 9429 2229 3b0a 5520 613d 3332  *.Vo.)");.U a=32
000002a0: 302c 623d 3338 362c 6e3d 2a64 2b2b 3b0a  0,b=386,n=*d++;.
000002b0: 6368 6172 206d 5b61 2a62 2a33 5d7b 307d  char m[a*b*3]{0}
000002c0: 3b0a 666f 7228 5520 693d 303b 693c 6e3b  ;.for(U i=0;i<n;
000002d0: 692b 2b2c 642b 3d37 297b 6c6f 6e67 2078  i++,d+=7){long x
000002e0: 3d32 2a64 5b30 5d2c 793d 322a 645b 315d  =2*d[0],y=2*d[1]
000002f0: 2c77 3d32 2a64 5b32 5d2c 683d 322a 645b  ,w=2*d[2],h=2*d[
00000300: 335d 3b0a 666f 7228 5520 723d 303b 723c  3];.for(U r=0;r<
00000310: 613b 722b 2b29 7b66 6f72 2855 2063 3d30  a;r++){for(U c=0
00000320: 3b63 3c62 3b63 2b2b 297b 6c6f 6e67 2075  ;c<b;c++){long u
00000330: 3d63 2d78 2c76 3d72 2d79 3b0a 6966 2828  =c-x,v=r-y;.if((
00000340: 772a 772a 762a 762b 682a 682a 752a 7529  w*w*v*v+h*h*u*u)
00000350: 3c3d 772a 772a 682a 6829 7b61 7574 6f20  <=w*w*h*h){auto 
00000360: 2a70 3d6d 2b33 2a28 722a 622b 6329 3b2a  *p=m+3*(r*b+c);*
00000370: 702b 2b3d 645b 345d 3b2a 702b 2b3d 645b  p++=d[4];*p++=d[
00000380: 355d 3b2a 703d 645b 365d 3b7d 7d7d 7d0a  5];*p=d[6];}}}}.
00000390: 7374 643a 3a6f 6673 7472 6561 6d20 667b  std::ofstream f{
000003a0: 2265 2e70 706d 222c 7374 643a 3a69 6f73  "e.ppm",std::ios
000003b0: 3a3a 6269 6e61 7279 7d3b 663c 3c22 5036  ::binary};f<<"P6
000003c0: 5c6e 3338 3620 3332 305c 6e32 3535 5c6e  \n386 320\n255\n
000003d0: 223b 666f 7228 5520 693d 303b 693c 612a  ";for(U i=0;i<a*
000003e0: 622a 333b 692b 2b29 7b66 3c3c 6d5b 695d  b*3;i++){f<<m[i]
000003f0: 3b7d 0a72 6574 7572 6e20 303b 7d         ;}.return 0;}

C++11 Ellipses

This answer combines several approaches from previous answers, which I'll explain below, unfortunately I ended up having to somewhat golf the program to fit in 944 949 chars (according to wc -c), so it doesn't look much like C++ anymore (apologies if this is against the rules of the challenge, I'm going to try for some improvements shortly). I didn't plan on this at first so it still isn't completely indecipherable and there's still plenty of low-hanging fruit.

Updated Results

Merely running the genetic algorithm for longer has produced a slightly better result; however, given that the convergence has slowed significantly I'd say that this particular method is probably starting to top out (or I've fallen into some deep local minimum). I golfed the final program some more to squeeze in a couple more rectangles (the generator remains the same, except the maximum genome size was increased).

Implementing the crossover between individuals will help if the problem is a deep local minimum, but given that it's stayed in the same range for a while, I'm starting to think this is about as good as it gets for the number of rectangles.

#include <fstream>
#include <vector>
#define q operator
#define s struct
#define k return

using o=std::ofstream;using i=int;s C{unsigned char r,g,b;};void q<<(o &z,C &c){z<<c.r<<c.g<<c.b;}s R{i x,y,w,h;C c;};s P{P(i a,i b):w(a),h(b),p(w*h){}C &q()(i x,i y){k p[y*w+x];}i w,h;std::vector<C> p;};void q<<(o &z,P &p){z<<"P6\n"<<p.w<<" "<<p.h<<"\n255\n";for(i n=0;n<p.w*p.h;n++){z<<p.p[n];}}i main(){R a{0,0,386,320,{73,87,116}};P p(386,320);for(auto r:
){for(i x=0;x<r.w;x++){for(i y=0;y<r.h;y++){p(r.x+x,r.y+y)=r.c;}}}o f{"a.ppm",std::ios::binary};f<<p;k 0;}

enter image description here

Voronoi Version, 7331.92407536, 989 chars

I used Marco13's Voronoi Idea with my GA code. This actually didn't work as well as I was hoping. I could only squeeze in a few more points than rectangles. I think the potentially disjoint nature of the rectangles due to overlapping helps the score a bit. Regardless, I actually like the way this looks significantly better, despite the similar score to my first entry.

#include <fstream>
#include <vector>
#define q operator
#define s struct
#define k return
using i=int;using o=std::ofstream;s C{unsigned char r,g,b;};void q<<(o &z,C &c){z<<c.r<<c.g<<c.b;}s P{i x,y;C c;P q-(P r){k {x-r.x,y-r.y,{0,0,0}};}i q*(P r){k x*r.x+y*r.y;}i q^(P r){P d=(*this-r);k d*d;}};s X{X(i a,i b):w(a),h(b),p(w*h){}C &q()(i x,i y){k p[y*w+x];}i w,h;std::vector<C> p;};void q<<(o &z,X &p){z<<"P6\n"<<p.w<<' '<<p.h<<"\n255\n";for(i n=0;n<p.w*p.h;n++){z<<p.p[n];}}i main(){P a{101,108,{72,89,122}};X p(386,320);for(i y=0;y<p.h;y++){for(i x=0;x<p.w;x++){P c(a),d{x,y,{0,0,0}};for(auto g:{a,{0,314,{48,56,58}},{182,135,{89,112,144}},{108,262,{34,39,41}},{357,221,{64,78,102}},{251,289,{50,60,75}},{129,161,{108,128,142}},{375,1,{83,104,137}},{44,161,{95,120,144}},{316,254,{53,65,85}},{47,161,{37,43,41}},{373,37,{159,167,121}},{313,138,{87,115,152}},{264,0,{71,88,130}},{314,141,{128,148,153}}}){i m=c^d;i n=g^d;if(n<m){c=g;}}p(x,y)=c.c;}}o f("v.ppm",std::ios::binary);f<<p;k 0;}

voronoi GA

Old Results, 7441.68126105, 944 chars

#include <iostream>
#include <fstream>
#include <vector>
#define q operator
#define s struct
#define k return

using o = std::ostream; using i = int; s C{i r;i g;i b;}; o &q<<(o &z,C &c){z<<(char)c.r<<(char)c.g<<(char)c.b;k z;} s R{i x;i y;i w;i h;C c;};s P{P(i a,i b):w(a),h(b){p.reserve(w*h);}C &q()(i x,i y){k p[y*w+x];}i w;i h;std::vector<C> p;}; o &q<<(o &z,P &p){z<<"P6\n"<<p.w<<" "<<p.h<<"\n255\n";for(i n=0;n<p.w*p.h;n++){z<<p.p[n];}k z;} i main() { R a{0,0,386,320,C{89,109,129}}; P p(386,320); for (auto r:
) { for(i dx=0;dx<r.w;dx++){for(i dy=0;dy<r.h;dy++){p(r.x+dx,r.y+dy)=r.c;}} } std::ofstream f("a.ppm"); f << p; k 0; }

Much like some of the other entries, the program just draws overlapping rectangles. It uses binary PPM since the format is simple (the output is a.ppm, but I uploaded a png version since SE didn't like the PPM), and is completely deterministic.

PNG version of my output


Generating the PPM took up a good chunk of boilerplate code, which meant I couldn't have too many rectangles even after golfing a bit. A few more can probably be squeezed in here to improve the score further.

The real magic is the list of rectangles. Similar to Wolfgang's answer I used a genetic algorithm to find these. Actually the implementation is largely incomplete, since recombination between individuals does not occur yet, but mutation still happens and the tournament-style ranking by fitness keeps the best organisms in the next round. Elitism is also used, as a copy of the best individual from the last round is kept into the next round, so the most fit organism is always at least as fit as in the previous round.

I did not look too closely at Wolfgang's code since I had started this yesterday, but it appears that he allows the color to vary as well, which may explain the score difference.

To keep the search space smaller, I only looked at rectangle position; the color is calculated by the per-channel average of the visible pixels from that rectangle since we have the source image (I don't think we can do any better than this for that particular rectangle since this minimizes the squared-distance).

I will put up a github repository in the next couple of edits if I keep working on it, but for now the (single-file) code is on pastebin. Compile it in C++11 mode, (side-note, I'm pretty embarrassed about how messy it is even for a one-off).

You will also need a P3 PPM image of starry night named ORIGINAL.ppm for this to work. You can download the file from this GitHub Gist.


Posted 2016-01-23T15:16:57.587

Reputation: 631

Welcome to PPCG! I went ahead and uploaded a P3 PPM version of the original image to a GitHub Gist, so I'll edit the link into your post for you. – Mego – 2016-01-26T05:23:56.627

3+1 for using a genetic algorithm. I'm hoping that a GA will eventually win this challenge. – Nathaniel – 2016-01-26T05:34:45.683

You can get a couple more bytes by losing the "#include <fstream>" and using "std::cerr << p" at the end. – Eponymous – 2016-01-26T17:17:44.023

@Eponymous Thanks, yea there are other things that can really tighten it up, (e.g. the color structure can be declared i r,g,b instead of separately, and a lot of spaces can be discarded). I wasn't sure if the program should generate the file directly or if piping to stdout/stderr was okay. – neocpp – 2016-01-26T17:20:42.010

Why a space in #include <fstream>? Also, drop the newlines and put it all on one line since C++ needs semicolons anyways – cat – 2016-03-27T00:42:34.620

@tac Thanks, to be honest I didn't even notice the include could be shortened. I actually have a slightly better-scoring version with some changes (dropping all pretense of portability and readability), but it wasn't worth posting yet. Maybe I'll do another one later. – neocpp – 2016-03-27T07:44:20.060


ImageMagick, 4551.71

Uses the ImageMagick 'programming' language, using the following options (you might have to escape the !):

convert i -resize 386x320! o.png

Assuming the following 968 byte source file (given as hexdump):

00000000: 89 50 4E 47 0D 0A 1A 0A - 00 00 00 0D 49 48 44 52 | PNG        IHDR|
00000010: 00 00 00 30 00 00 00 28 - 08 03 00 00 00 8F 59 89 |   0   (      Y |
00000020: 43 00 00 00 3C 50 4C 54 - 45 1E 22 1F 5D 73 88 3B |C   <PLTE " ]s ;|
00000030: 51 8A 38 48 60 51 6F 9C - 30 41 7C 38 40 41 49 63 |Q 8H`Qo 0A|8@AIc|
00000040: 8E 27 2E 3A 63 7C 9B 42 - 56 79 29 36 5C 74 8C A1 | '.:c| BVy)6\t  |
00000050: 74 89 8B 8D A0 97 4E 5C - 62 A4 AF 9E B0 B4 79 84 |t     N\b     y |
00000060: 8F 70 B3 A3 3C C0 50 80 - E6 00 00 03 47 49 44 41 | p  < P     GIDA|
00000070: 54 78 01 65 91 07 16 C3 - 28 0C 44 25 9A A8 C6 71 |Tx e    ( D%   q|
00000080: EE 7F D7 FD C2 5B B3 F3 - 1C EA 7C 15 22 A3 EB D2 |     [    | "   |
00000090: 5E 47 D4 38 46 B0 A5 21 - E7 AE AA 9D 79 8C D1 63 |^G 8F  !    y  c|
000000a0: 8E 21 F4 6E 66 81 2F 77 - 19 F6 7C 9F 36 46 7E 94 | ! nf /w  | 6F~ |
000000b0: 61 3D D0 28 58 CE 39 F8 - 22 9C B1 63 D7 B6 D4 0C |a= (X 9 "  c    |
000000c0: 60 3D 4F CB 98 63 1E 99 - 33 7D 1E DC 28 70 A1 21 |`=O  c  3}  (p !|
000000d0: E4 90 C1 75 5E D7 E7 BA - 96 4A 0E AD B5 90 47 CD |   u^    J    G |
000000e0: 88 70 39 E8 66 66 63 00 - CB CE CA 6C 5D 1F 04 22 | p9 ffc    l]  "|
000000f0: 83 5B 0E EB 01 88 F6 8A - 08 2F D0 07 9B D4 A6 FB | [       /      |
00000100: BF DF CF 0D 10 5E A0 EC - DD F3 2B 8A 3B 4B 53 35 |     ^    + ;KS5|
00000110: C0 9C 5F E0 0B 03 40 82 - 03 EC E7 BB 33 EA 96 30 |  _   @     3  0|
00000120: D2 B9 33 DE 01 B2 34 4F - 7C 74 49 78 55 F7 F7 FB |  3   4O|tIxU   |
00000130: 30 9B 29 2F 35 E7 4C AD - A5 30 3A 70 4B 29 5D FF |0 )/5 L  0:pK)] |
00000140: 00 BC FB C9 D0 55 3D 54 - C2 AF A8 29 61 5B 1F FA |     U=T   )a[  |
00000150: 5C 0B 62 DE 10 C8 81 44 - 5F 00 99 E0 44 55 5D 8B |\ b    D_   DU] |
00000160: 55 27 AE 13 59 79 CC 99 - AC AD FB 00 F4 D0 74 69 |U'  Yy        ti|
00000170: 0F 21 96 86 56 6B B3 47 - 32 21 6C D6 CC D6 9A 64 | !  Vk G2!l    d|
00000180: A0 D0 CF E9 79 49 9A 6B - E2 53 51 CC 30 71 D0 86 |    yI k SQ 0q  |
00000190: 61 61 6F 88 25 40 48 EB - BA EE EB 9A 4D D4 9A 6B |aao %@H     M  k|
000001a0: CB 66 D4 67 C5 B3 4F 7C - 73 B6 D0 ED 34 C6 07 37 | f g  O|s   4  7|
000001b0: FD 58 3C 0E A5 14 D9 73 - 1A 80 01 BC 57 3C 55 07 | X<    s    W<U |
000001c0: 20 87 85 03 1C 4E A6 4B - 15 80 37 31 3D FE A4 CD |     N K  71=   |
000001d0: 12 00 0E 83 41 E9 55 37 - 7D 01 2D 55 A2 F1 B6 39 |    A U7} -U   9|
000001e0: 78 25 FA BE 18 94 CE B5 - DA 89 7E D4 AE 5B 3C 77 |x%        ~  [<w|
000001f0: 33 11 29 3D C6 11 0E 00 - 11 08 6E AA 2C F5 64 82 |3 )=      n , d |
00000200: 98 B4 7D DF 72 35 F2 6C - 41 31 52 30 51 D0 3C A5 |  } r5 lA1R0Q < |
00000210: E0 A7 1A 42 E0 C6 8E FB - 05 4C 3F 4F 71 A0 73 3C |   B     L?Oq s<|
00000220: 79 39 E4 98 92 83 EC 6F - 2F 13 EF 91 50 2D FF 89 |y9     o/   P-  |
00000230: 03 C5 D2 89 32 79 75 A2 - 78 A7 86 19 86 F9 3E F5 |    2yu x     > |
00000240: 30 0A 55 EB F3 54 CF E0 - 3D DD 9F FB B6 DC 9B BF |0 U  T  =       |
00000250: 7F 27 03 52 0D CD BD 73 - AE 5D 8A 84 4E A8 2E 28 | ' R   s ]  N .(|
00000260: 9A 5E B7 FF FD 6A 28 99 - 03 CA 13 AB AE FB C2 DF | ^   j(         |
00000270: 1A 75 54 21 77 B6 28 A8 - F4 3E 4F 5A 7A 34 3E 6B | uT!w (  >OZz4>k|
00000280: 58 2F E4 5A 6B EE 5A 85 - 6F AD 65 2F 50 63 57 F7 |X/ Zk Z o e/PcW |
00000290: 2F 7C 48 DD 06 30 8F D8 - D7 51 91 34 CE 1A 00 8A |/|H  0   Q 4    |
000002a0: B1 37 6E 9E 67 BD C3 0B - CE A9 AA BD D4 3A A2 4B | 7n g        : K|
000002b0: B4 11 69 0B 22 DF 6E C4 - C4 89 D5 5D 7D 8C 41 DE |  i " n    ]} A |
000002c0: 1E BD 98 68 C9 EB 14 55 - 1E FA 2F 40 88 D9 68 55 |   h   U  /@  hU|
000002d0: 4D 93 B9 3D 43 54 9F 79 - CC 23 93 40 6F 4D E5 25 |M  =CT y # @oM %|
000002e0: 44 76 37 9C 91 21 C6 9C - 63 1C D5 CD C1 F8 52 6B |Dv7  !  c     Rk|
000002f0: 9E A1 1B E1 1A 56 E4 23 - 36 A2 7A D0 DE F3 89 DD |     V #6 z     |
00000300: 51 EC D1 BC C8 BD A5 12 - 20 47 F9 47 E3 6D 0F 20 |Q        G G m  |
00000310: E2 27 4B 89 85 FD 8E BA - 11 40 C5 21 FF 52 45 A5 | 'K      @ ! RE |
00000320: 6F 9E 6C 13 D9 75 8C 3E - E9 01 D0 2F 80 89 A2 08 |o l  u >   /    |
00000330: 0A 30 4A 2D C0 F8 B5 E3 - 2F DC 93 42 FE 8D D4 81 | 0J-    /  B    |
00000340: CB 0B E1 02 23 33 16 F2 - BD 59 A4 94 01 20 3F 39 |    #3   Y    ?9|
00000350: 64 97 B2 2B D1 11 0E 47 - F6 AE 85 E6 C4 C7 5F 80 |d  +   G      _ |
00000360: 8F 42 36 76 21 60 F5 64 - 7E 72 24 67 2F BF 44 45 | B6v!` d~r$g/ DE|
00000370: EE 78 B7 91 74 A7 95 4D - 06 2E E0 7F 45 A0 78 10 | x  t  M .  E x |
00000380: D6 83 9A CA 8E 75 17 9C - 00 05 FD 1F 70 95 57 70 |     u      p Wp|
00000390: B4 79 BA 97 53 1B AA BF - 39 DC 56 98 10 AF 73 DA | y  S   9 V   s |
000003a0: 06 72 B7 50 9D 0B E2 5F - 10 6E 54 DF 5F 8C 4C 48 | r P   _ nT _ LH|
000003b0: 3C E9 FE 03 71 28 35 5B - 5B 36 D8 64 00 00 00 00 |<   q(5[[6 d    |
000003c0: 49 45 4E 44 AE 42 60 82 -                         |IEND B` |

Producing this image:

starry night - compressed

You're probably wondering how I generated the input file, and the answer is rather simple. Resize to 48x40 with the Lanczos filter, use a 20 color indexed palette, and optimize the resulting PNG.

convert ORIGINAL.png -filter Lanczos2 -resize x40 - | pngquant --speed 1 -f 20 > i
optipng -o7 -strip all i && advdef -z -4 -i 1024 i

Uses convert, pngquant, optipng and advdef.


Posted 2016-01-23T15:16:57.587

Reputation: 37 067

Comments are not for extended discussion; this conversation has been moved to chat.

– Doorknob – 2016-01-27T00:35:17.037


FYI, using the =(tail +2 $0) trick from my answer, you can create a single ZSH script which contains both the ImageMagick script and the PNG input file.

– nneonneo – 2016-01-28T08:06:31.707


Python 2, 4749.88

1018 bytes

Everybody has probably forgotten about this problem by now, except me....

This problem interested me far too much, especially as it quickly became apparent that approaches using image compression algorithms were definitely in the lead, but yet somehow unsatisfying from an aesthetic standpoint. Approaches based on optimizing a set of drawing primitives were somehow more pleasing from a code aesthetic point of view, but seemed blocked just above the 5000 score.

nneonneo's method that did not use an off the shelf image compression beats the 5000 mark, but does so by encoding a tiny image and upscaling it.

Here is a program that uses drawing primitives only, is generated automatically by an optimization method, and manages to get a score of 4749.88.

import cv2 as v,numpy as n,struct
s='v\n#{~X©s"¾|ën²ºÉ¬ª·.v"4Á=ð.yv>ä;¦>t?ÞC°GòS¾ó[pQ¹,g]xgQÒWµló´:eX K² }w¯hVZ[ÂbD¹t¦\n§1±"}¼e®`h¸B½qò¥èJÕN©²f­J¦ü   ³|©t|   \rÕ5SO©¾zP±¤Od\rÆ¥L©|¸B{I¯Öb~¯½ÒdzQ½}}D«s\x8ewxK    ^pMz2L5`mce|ÙvlRcnJqw3|ÿGZ:s4\r]r.  ÝX,(\n*÷¹òW@Àà´IºäQ,pfuQhØvTzDÂ\\NnbSbº |!1o0»5,fSñ8¿-VÇ4}¡$y   ­S(Y³ek.MÌ  wdvB\n r³UƨJÒ^<©èf#}<©lux6º}\0SÑP{\0TBÏx°A~w00ÃU)\x8e\n½Iá\0TòKUVmWOTæ¢ynLrXYKº\npkJWÀw«g"Sh4kIg"|[pÞ££ì$OH\\³>°nu9|6Õ¼¡.A2qrÀ\\ZýzE{mwG]+YHÃèrälT·¥DNN\0T'
for a,b,c,d,r in[struct.unpack('HHBBB',s[7*i:7*i+7])for i in range(92)]:
 for k in range(h),(a%m-h+x*k/h,b%m-h+y*k/h),r,n.clip((.9*c+.9*d-120,c-.3*d+41,.9*c-.6*d+80),0,255),-1)

which looks like this:

drawing result

and the hexdump of the code:

0000000: efbb bf69 6d70 6f72 7420 6376 3220 6173  ...import cv2 as
0000010: 2076 2c6e 756d 7079 2061 7320 6e2c 7374   v,numpy as n,st
0000020: 7275 6374 0a7a 3d6e 2e6f 6e65 7328 2833  ruct.z=n.ones((3
0000030: 3230 2c33 3836 2c33 292c 2775 3127 290a  20,386,3),'u1').
0000040: 7a5b 3a5d 3d31 3435 2c31 3036 2c38 310a  z[:]=145,106,81.
0000050: 7a5b 3232 363a 5d3d 3830 2c36 372c 3536  z[226:]=80,67,56
0000060: 0a73 3d27 8076 5c6e 0523 8414 9d7b 7e58  .s='.v\n.#...{~X
0000070: a973 22be 7ceb 6eb2 8416 ba05 c9ac aa8b  .s".|.n.........
0000080: 13b7 2e76 0522 8434 c13d f08a 2e95 0e79  ...v.".4.=.....y
0000090: 763e e43b a60b 3e74 3fde 43b0 0b9e 9247  v>.;..>t?.C....G
00000a0: f253 be0e 0cf3 5b70 51b9 1a2c 675d 7889  .S....[pQ..,g]x.
00000b0: 850f 6719 51d2 57b5 116c 05f3 909a 940c  ..g.Q.W..l......
00000c0: b405 3a65 58a0 114b b220 7d77 af0b 6856  ..:eX..K. }w..hV
00000d0: 835a 5bc2 1562 44b9 749b a65c 6e90 0701  .Z[..bD.t..\n...
00000e0: a731 9807 9107 b122 7dbc 1265 05ae 6068  .1....."}..e..`h
00000f0: b80c 42bd 71f2 9ca5 01e8 4ad5 4e8d a90e  ..B.q.....J.N...
0000100: 1291 b266 93ad 0c4a a6fc 9e9a 8d09 b37c  ...f...J.......|
0000110: a974 937c 095c 72d5 3553 4fa9 1d9d 94be  .t.|.\r.5SO.....
0000120: 7a50 b10e a44f 8064 851e 5c72 8fc6 a54c  zP...O.d..\r...L
0000130: 8ba9 0f81 7cb8 421c 7b07 49af d662 7eaf  ....|.B.{.I..b~.
0000140: 1abd d264 7a51 bd0b 7d87 7d44 ab73 0b8c  ...dzQ..}.}D.s..
0000150: 5c78 3865 7778 874b 095e 704d 7a91 3207  \x8ewx.K.^pMz.2.
0000160: 4c35 606d 0b63 0865 7cd9 769e 6c0b 5263  L5`m.c.e|.v.l.Rc
0000170: 8b6e 9a82 0b4a 7113 7733 9c0b 9f7c ff86  .n...Jq.w3...|..
0000180: 479b 0b5a 143a 7334 9d5c 7282 965d 722e  G..Z.:s4.\r..]r.
0000190: 9509 dd58 2c9d 288c 5c6e 2a1f f796 938c  ...X,.(.\n*.....
00001a0: 07b9 f20f 5792 8907 40c0 e0b4 49ba 02e4  ....W...@...I...
00001b0: 9c51 9d2c 8a0e 7066 7551 1168 03d8 7654  .Q.,..pfuQ.h..vT
00001c0: 7a88 4406 c25c 5c4e 6e95 0101 6253 1c8f  z.D..\\Nn...bS..
00001d0: 62ba 097c 2131 6f30 8803 bb35 2c8b 6685  b..|!1o0...5,.f.
00001e0: 0753 f138 bf2d 9306 56c7 347d 909c 0295  .S.8.-..V.4}....
00001f0: a124 8b79 a009 ad53 2885 1c59 01b3 656b  .$.y...S(..Y..ek
0000200: 892e 860b 4dcc 9988 8da0 099e 7764 769f  ....M.......wdv.
0000210: 425c 6ea0 8572 86b3 550b c6a8 904a 908a  B\n..r..U....J..
0000220: 05d2 5e92 9a3c a905 e866 237d 3ca9 0b6c  ..^..<...f#}<..l
0000230: 7578 3681 ba07 7d80 9304 5c30 5302 97d1  ux6...}...\0S...
0000240: 507b 5c30 5401 4280 cf78 b083 0696 417e  P{\0T.B..x....A~
0000250: 7730 920b 30c3 5589 295c 7838 655c 6ebd  w0..0.U.)\x8e\n.
0000260: 49e1 865c 3054 01f2 984b 5501 5602 086d  I..\0T...KU.V..m
0000270: 574f 0154 03e6 a279 828c 9d07 926e 4c72  WO.T...y.....nLr
0000280: 8a58 038c 8459 884b ba5c 6e70 846b 4a57  .X...Y.K.\np.kJW
0000290: c008 9077 9180 ab67 0b22 5368 9734 8911  ...w...g."Sh.4..
00002a0: 1a6b 4967 2284 067c 815b 8570 9306 de9c  .kIg"..|.[.p....
00002b0: a38a 8ba3 08ec 244f 485c 5cb3 083e b06e  ......$OH\\..>.n
00002c0: 7539 7c0c 36d5 bc86 94a1 042e 4132 859c  u9|.6.......A2..
00002d0: 9a01 7172 c05c 5c04 5a01 fd7a 457b 6d91  ..qr.\\.Z..zE{m.
00002e0: 0611 7747 8d5d 9708 2b59 9548 c38c 01e8  ..wG.]..+Y.H....
00002f0: 72e4 6c54 b706 a544 4e4e 5c30 5401 270a  r.lT...DNN\0T.'.
0000300: 6d2c 683d 3531 322c 3632 0a66 6f72 2061  m,h=512,62.for a
0000310: 2c62 2c63 2c64 2c72 2069 6e5b 7374 7275  ,b,c,d,r in[stru
0000320: 6374 2e75 6e70 6163 6b28 2748 4842 4242  ct.unpack('HHBBB
0000330: 272c 735b 372a 693a 372a 692b 375d 2966  ',s[7*i:7*i+7])f
0000340: 6f72 2069 2069 6e20 7261 6e67 6528 3932  or i in range(92
0000350: 295d 3a0a 2078 2c79 3d61 2f6d 2d68 2c62  )]:. x,y=a/m-h,b
0000360: 2f6d 2d68 0a20 666f 7220 6b20 696e 2072  /m-h. for k in r
0000370: 616e 6765 2868 293a 762e 6369 7263 6c65  ange(h)
0000380: 287a 2c28 6125 6d2d 682b 782a 6b2f 682c  (z,(a%m-h+x*k/h,
0000390: 6225 6d2d 682b 792a 6b2f 6829 2c72 2c6e  b%m-h+y*k/h),r,n
00003a0: 2e63 6c69 7028 282e 392a 632b 2e39 2a64  .clip((.9*c+.9*d
00003b0: 2d31 3230 2c63 2d2e 332a 642b 3431 2c2e  -120,c-.3*d+41,.
00003c0: 392a 632d 2e36 2a64 2b38 3029 2c30 2c32  9*c-.6*d+80),0,2
00003d0: 3535 292c 2d31 290a 762e 696d 7772 6974  55),-1).v.imwrit
00003e0: 6528 2761 2e70 6e67 272c 762e 626c 7572  e('a.png',v.blur
00003f0: 287a 2c28 392c 3929 2929                 (z,(9,9)))

It uses a number of tricks used previously here:

  • Using a blur to push the final score up a bit
  • Cramming raw bytes into python code (which is not as simple as suggested earlier in this thread, more characters need to be escaped than just backslashes and nulls, dig into the code here for details).
  • Compressing the color space into two dimensions. Fascinatingly, this particular starry night image is nearly a plane in RGB space.

As a first primitive, I place a horizon line, splitting the image into two different colored blocks. Afterwards, as a basic primitive I used a circle dragged between two points. This looked vaguely like a brushstroke to me, but was possible to express in 7 bytes. For the search process, I used a guided pattern search. It proceeds by alternately adding a primitive, and optimizing its parameters. The primitive is added on the point where the blurred signed error is highest. The parameters are optimized by exhaustive line optimization over a small domain, one after the other. Forty to fifty primitives are added, and optimized individually. Then, the list of primitives is pruned down to size by throwing away the primitives that help the score the least.

This still does not beat nneonneo's score. To beat that score, a second stage optimization was required, which goes again through the process of adding primitives at each of several filtering levels, and throwing primitives away to trim the generated program down to size. What was really interesting to me was the idea of applying this to other images. I applied it to a couple of other images, and provide further details and animation of the primitives being drawn in my blog here.

The two programs used to generate this won't actually fit in the space allowable on Stack Exchange posts, but they are on github:

starrynight is run first, followed by stage2optimization. The resulting program is also there, in the same directory.


Posted 2016-01-23T15:16:57.587

Reputation: 611

4Excellent answer, I'm very happy to see this! According to the bonus rules I posted, a 200 point bounty will be winging its way to you in about a week, assuming no other no-built-in-compression answer beats your score before then. – Nathaniel – 2016-03-20T13:43:15.200

3Welcome to CG.SE! Nice first post! – Arcturus – 2016-03-20T14:24:01.150

This is excellent. I'm somewhat surprised the blur works so well; I tried a few different blur parameters on my outputs and it's possible for my perl solution to go under 5k, but unfortunately I don't have an easy way to apply it. – neocpp – 2016-03-27T07:43:56.887

1The blur is on during the optimization (its radius is an optimizable parameter), so each primitive that is selected is found with the blur. This may make the blur appear more important than it is. If it was reoptimized with the blur forced to be off, it would probably recover some of that value. But scores are definitely better overall with the blur than without - I tried without it for a while. – Strawdog – 2016-04-03T12:32:03.803


Matlab, score 5388.3

Without any built in compression. The colour depth is reduced such that each pixel can be represented by one printable character. And the resolution is reduced. This is then hardcoded as a string. The code itself reverses the whole process. The resizing operation is using a Lanczos 3 interpolation kernel.


enter image description here


Posted 2016-01-23T15:16:57.587

Reputation: 40 560


zsh+bpgdec, 4159.061760861207

Yes, another BPG solution. I think this basically serves to prove that BPG is the best image compression utility currently available. Consider it an improvement over yallie's original BPG solution.

The file is 1024 bytes long, right at the limit. It consists of the line

exec bpgdec =(tail -n+2 $0)

followed by the raw BPG output from

bpgenc -c ycbcr -f 444 -q 48 -m 12 -e jctvc ORIGINAL.png -o 1.bpg

In hex, this is the script:

0000000: 6578 6563 2062 7067 6465 6320 3d28 7461  exec bpgdec =(ta
0000010: 696c 202d 6e2b 3220 2430 290a 4250 47fb  il -n+2 $0).BPG.
0000020: 7000 8302 8240 0003 9242 5003 9242 5044  p....@...BP..BPD
0000030: 09c1 9095 8112 0000 0001 4401 c190 9581  ..........D.....
0000040: 1603 7000 0001 2609 ae0b 30e7 6016 6a97  ..p...&...0.`.j.
0000050: 9ad3 4192 8fd0 7000 0003 0000 0300 0003  ..A...p.........
0000060: 0000 0300 0003 0000 04cc 0000 0126 01af  .............&..
0000070: 0598 fd99 91f9 e8bf 2220 79ef 4ad2 83ea  ........" y.J...
0000080: 517b d6ec e17c d59d 4b3d ea16 82a3 bfa3  Q{...|..K=......
0000090: b8f6 5c75 1c55 c959 d1e2 cf13 e10c 183f  ..\u.U.Y.......?
00000a0: 2495 60c0 5b65 971f 8e7c 453d b2e4 fa80  $.`.[e...|E=....
00000b0: 89dc f5e4 0010 8347 4d3a bb07 5baa 95f3  .......GM:..[...
00000c0: ac52 eca1 4e2a 3452 1493 b896 e9fb 4d5f  .R..N*4R......M_
00000d0: 4605 0bbf 14f6 ec00 4291 05d6 263b f524  F.......B...&;.$
00000e0: a321 613c ad89 06d7 4983 29d9 f1d2 7acc  .!a<....I.)...z.
00000f0: 5550 65d3 f33b d195 eedd a509 9750 f9ae  UPe..;.......P..
0000100: bcbc f3b5 3380 c8db 0c1b e932 1a52 2d10  ....3......2.R-.
0000110: f77a f967 5e62 a766 7ee4 a076 a85b dacf  .z.g^b.f~..v.[..
0000120: 4177 3136 0a73 62b5 76d2 efc4 5de0 f9a6]...
0000130: ea4a d15a 7e7b 0e31 7f06 851d a2cf 0680  .J.Z~{.1........
0000140: 114f 57bb 7477 4217 34b6 afae 71c0 020e  .OW.twB.4...q...
0000150: b4ea 0725 348e 7dd6 00f7 adbb f7d5 c2fc  ...%4.}.........
0000160: 3e36 8138 2420 1751 cf5a cb8a 6fb1 0e26  >6.8$ .Q.Z..o..&
0000170: d5f8 5df6 cdc3 07b5 76dd 2593 170f e9b7  ..].....v.%.....
0000180: 07db ad63 3746 9639 f707 8581 2a16 b9a1  ...c7F.9....*...
0000190: 3563 c292 a112 d7c1 2d25 9461 99c4 990e  5c......-%.a....
00001a0: f917 2346 dc6f 51a5 fdc0 3a44 2f4f b0c9  ..#F.oQ...:D/O..
00001b0: 15e9 7d88 d386 47aa b705 e97c f2ee c419  ..}...G....|....
00001c0: e078 9aa3 b574 645a 631a 678a b7c7 6e69
00001d0: 4bd4 e8df b657 d56e 9351 8750 63c2 141c  K....W.n.Q.Pc...
00001e0: e3bb 8305 33ad 3362 08e8 d4b0 c5a8 af67  ....3.3b.......g
00001f0: 9695 63a0 ae96 a6fd 00a1 0105 eca5 db9e  ..c.............
0000200: 27ce d2fb c8ea 7457 2f38 5fd0 080a 2ac7  '.....tW/8_...*.
0000210: 4919 6b6a 424d ef1e 02c4 3607 de31 7c0f  I.kjBM....6..1|.
0000220: 7cb0 c90a 609b bbc1 7ae5 8d17 7fd3 406e  |...`...z.....@n
0000230: 8df7 81f8 fb51 7366 beb2 fb62 51e3 58ce  .....Qsf...bQ.X.
0000240: 55d5 8a28 a63b 7b31 0ede bdc2 9d13 04a2  U..(.;{1........
0000250: c039 de93 638d 6c68 c3d3 e762 36ed 4ae2  .9..c.lh...b6.J.
0000260: a3be 781b 150a 7b82 9f0b 0a14 17b7 ade1  ..x...{.........
0000270: 687a c84f 5a2f 88d1 a141 76fe bf7b c220  hz.OZ/...Av..{. 
0000280: 6189 8424 d7e3 3595 882f 1ec9 a363 3501  a..$..5../...c5.
0000290: 3056 f6f9 dced 2b37 733b 8659 f5e9 93f9  0V....+7s;.Y....
00002a0: fa5b 419a cb78 e0ef d7b4 1e83 7fce 4383  .[A..x........C.
00002b0: 7eee 10af 2baa 1445 eb06 d75c 4220 53f9  ~...+..E...\B S.
00002c0: 34fd 76c0 2117 f916 f3b7 f599 0977 2562  4.v.!........w%b
00002d0: 085d a2d4 74c1 2e6c 0a21 5ccf 6a9f c045  .]..t..l.!\.j..E
00002e0: 91e0 de66 29af de27 af2b f673 8cb5 b2ea  ...f)..'.+.s....
00002f0: b070 31fd b81f 8db1 8e25 3243 31a0 ca08  .p1......%2C1...
0000300: e801 e4b6 df72 4029 16b2 a712 7ee4 c2e6  .....r@)....~...
0000310: acaa f84c d17d 3d46 65d5 8226 bd65 da45  ...L.}=Fe..&.e.E
0000320: 3cac 95d8 ed0e 1153 7587 09ec d745 4f50  <......Su....EOP
0000330: ba4c 314b 4ac3 b6b7 4964 1ee8 e321 c029  .L1KJ...Id...!.)
0000340: 7ae2 4630 fe05 ddd1 f68e 5646 857d e8fb  z.F0......VF.}..
0000350: 601e 453f e53e fe0d 0c5e 5da6 4a03 f6d9  `.E?.>...^].J...
0000360: c59b 0b7f b2de f354 21bb c0c5 8bb9 dfa1  .......T!.......
0000370: f3e5 76a7 bbce 175e cc27 125f dd9b adc2  ..v....^.'._....
0000380: cd79 d2c0 43f1 6df4 203a d3c4 9b25 7fea  .y..C.m. :...%..
0000390: 1905 7620 01bf a477 8c0e 9145 1d30 86d5  ..v ...w...E.0..
00003a0: 598d 7f40 ad72 603e c90f 5a62 db09 1161  Y..@.r`>..Zb...a
00003b0: a36d bbfc 020a 9835 7fc7 a468 4c36 5120  .m.....5...hL6Q 
00003c0: 01fc 705e 64d4 4e62 3c52 48a5 42fb 6361  ..p^d.Nb<
00003d0: 2496 21ff 321b 2b7b 3016 7a56 1ea6 18f9  $.!.2.+{0.zV....
00003e0: e52f 318a 80cb 237c f3c8 a46c b747 794e  ./1...#|...l.GyN
00003f0: e8c1 77c2 7eb3 ef5b 60fb ad03 a4e6 ee40  ..w.~..[`......@

The resulting file is out.png (the bpgdec default location), which looks like this:

starry night approximation from bpgdec

I find it rather amazing that bpg, in a mere 996 bytes, has accurately reconstructed the sharp contours of the tree, on the left, and the hills on the right. It even has a passable approximation for the church steeple! The level of detail is very impressive (to me) for the small filesize. Of course, bpgdec itself is not a small program, but it's clear to me that BPG is an order of magnitude better than JPEG for image compression.

Because this uses bpgdec, this answer is obviously not eligible for the bounty.

EDITED: Added -n argument to tail to make it compatible with GNU tail.


Posted 2016-01-23T15:16:57.587

Reputation: 11 445

That's neat. Nice one! – bjornl – 2016-01-28T05:52:29.813

Doesn't work for me, errors out with tail: cannot open ‘+2’ for reading. On Ubuntu it needs -n +2, which puts it at 1025 bytes =/ – orlp – 2016-01-28T09:35:03.900

Wow! I had no idea BPG was so awesome. – None – 2016-01-28T10:21:03.777

@orlp: I believe you should be able to use -n+2 which puts it at exactly 1024 bytes - try it and let me know if that works. I will change my answer for compatibility. – nneonneo – 2016-01-28T14:45:33.987

@nneonneo Seems to work. – orlp – 2016-01-28T15:25:11.407

@orlp: OK, amended to make my program compatible. Thanks for pointing it out! – nneonneo – 2016-01-28T18:02:53.003

I'm curious what technique you used to choose the encoding parameters. – 2012rcampion – 2016-01-29T10:23:40.373

1@Lembik, The BPG's awesomeness basis is HEVC (H.265). Actually using older H.264 (using maximum encoding settings) for static images also produce good results while also enabling using already available hardware decoding. In video there is more need in good compression, so it's not stalled and settled like with JPEG and MP3. – Vi. – 2016-02-01T00:31:26.983

HEVC is sorcery! – curiousdannii – 2016-02-01T03:32:42.930


C, 6641

999 bytes, using only stdio.h and math.h.

I made a filled-circle function d() that draws concentric RGB colored circles over radius values r..0. 21 circles are used here. I could squeeze in a few more if I stripped out more whitespace, but I like the relative readability as it stands.

I figured out rough circle placement using Gimp layers in Difference mode. Look for the bright spots, add a circle, repeat. Used Histogram tool on selection to determine initial colors to use.

I got a score of around 7700 using the above, but figured I could do better by tweaking the color and radius values, so I wrote some scaffolding code to brute-force optimize each value by modifying it -10..+10, re-rendering, running the validator (which I rewrote in C for speed), and saving the value that produced the lowest score. At the end it dumps the value array, which I paste it back into the code and recompile. I ran a few passes, and it brought the score down by about 1000. Then I stripped out the scaffolding code.

69930 image using circles enter image description here


#include <stdio.h>
#include <math.h>
#define W 386
#define H 320
#define SZ (W*H)
unsigned char I[SZ*3];
void d(int R,int G,int B,int x,int y,int r)
{while (r) {
float p;
for (p=0;p<6.3;p+=(1/(6.3*r))) {
int xo=r*cos(p);
int idx=x+xo+floor(y+r*sin(p))*W;
if ((x+xo<W)&&idx>0&&idx<SZ){I[idx*3]=R;I[idx*3+1]=G;I[idx*3+2]=B;}
int v[] = {
int main(){
int i;for(i=0;i<(21*6);i+=6){d(v[i],v[i+1],v[i+2],v[i+3],v[i+4],v[i+5]);}
FILE *f=fopen("o.ppm","wb");fprintf(f,"P6\n386 320\n255\n");fwrite(I,sizeof(I),1,f);fclose(f);


Posted 2016-01-23T15:16:57.587

Reputation: 381

7Kudos for doing actual programming! – oligofren – 2016-02-01T17:54:06.870

I really like this answer, but is there a reason you used this particular circle drawing function? The obvious version (check if ((x-xo)*(x-xo) + (y-yo)*(y-yo)) <= (r*r)) seems like it would be shorter and take out the dependency on math.h. With this size of image I don't think anything has a chance of overflowing either. – neocpp – 2016-02-14T06:26:23.380

2@neocpp I thought there might be a simpler way to draw a circle, but I wanted to code this without googling anything, and using the trig functions was the best I could come up with. The algorithm you suggest is great, and probably faster, but it was not obvious to me. – jamieguinan – 2016-02-15T07:50:16.383

1That's interesting, as I wouldn't have been able to come with with an algorithm like yours on my own. It somewhat reminded me of the scan line circle fill algorithm (the name escapes me right now, but it's useful to avoid checking many points), but since it worked on decreasing radii I was curious if there was some special property to this one. – neocpp – 2016-02-15T07:59:56.373


I tried using a filled-circle function based on the one you suggested (see also). It runs 170x faster, and with extra code space I was able to add another circle and optimize the score down to below 6500. Fun stuff.

– jamieguinan – 2016-02-16T20:36:36.513


Python 3, score 5390.25, 998 bytes

I used a simulated annealing program to fit rectangles into the shape of Starry Night. Then it uses a Gaussian blur to smooth out the straight rectangular edges.

To save some bytes, I compressed the rectangle data into base 94.

from PIL import Image as I,ImageDraw as D,ImageFilter as F
def V(n,f,t):
    z=0;s='';d='''0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_-+={[}]|:;"',<.>/?`~ '''
    for i in n:z=z*f+d.index(i)
    while z:z,m=divmod(z,t);s=d[m]+s
    return s'RGB',(386,320))
K=V("""12XsPc/p^m(:,enp:SN8brwj%!iChDHKj"*445Z.;/8Xj408fV9a7:v$N#cj_WNW7p#t9:](i?S!}yg*D4u$RfpU=}=@Ft^v7$N5O?8eeN%.bT:Q>+AOd3E*R/1PXq.IO,ur3h<`dS)V;e/lj6q'p4s|m>fkk<!jx`EGU~38(0h!(I6P.<[G;m_c^x{kE^hYQUV9kIiS'T:GDRQz -ISW6@cLKz4!e&8LT]kH3'Hj=Zl]rEOyrXlmfG51.K1(5l{:GPb1PL5%.gMmLy;pU3h+zDxpSn@)nJ*#'EOt=Pt.t9z,;D.[r|Prpeu=0%WN+A~KSb(E:gd%o2QfB_K-!xLAN+jXicd**bk'WDq,ue&z]Rb>;DBCFif{zJEDfx3FKqB*?2Qti:(pYSa-uZU,M!^N =bRbZ`}j}P-u-n>lGH|pv>#r"}Eg&c6J&fi.IC@2:L""",94,10)[1:]
for X in range(0,len(K),21):
    for z in range(0,21,3):x+=[int(y[z:z+3])]

enter image description here


Posted 2016-01-23T15:16:57.587

Reputation: 1 322

7+1 for drawing objects vs just a byte array from compressing the image – prototype – 2016-01-28T04:09:33.930

Looks like you've got space for several more rectangles. Does the blur really help the scoring? I wouldn't have thought so, but cool if it does! – curiousdannii – 2016-01-28T06:32:47.270

@curiousdannii Several other answers, usually compression ones, use blurring to spread out a strong signal in one area. This is not always the best way, but thank Gogh it is. The blurring actually decreases the score by ~200. – Magenta – 2016-01-28T08:03:56.863

This is a brilliantly bad answer :) – None – 2016-01-28T10:19:30.997


Python 2, 5238.59 points

It is probably time to post my own answer. Here's the image

enter image description here

The code looks like this

import Image,ImageDraw as D"RGB",(386,320),"#4b5b6e")
s="[string containing unprintable characters]"
for i in range(95):
        x,y,w,h,r,g,b,a=[ord(c)-9for c in s[i::95]]

Or as a hex dump:

0000000: 696d 706f 7274 2049 6d61 6765 2c49 6d61  import Image,Ima
0000010: 6765 4472 6177 2061 7320 440a 6d3d 496d  geDraw as D.m=Im
0000020: 6167 652e 6e65 7728 2252 4742 222c 2833"RGB",(3
0000030: 3836 2c33 3230 292c 2223 3462 3562 3665  86,320),"#4b5b6e
0000040: 2229 0a64 3d44 2e44 7261 7728 6d2c 2252  ").d=D.Draw(m,"R
0000050: 4742 4122 290a 733d 2228 356e 5220 1c5e  GBA").s="(5nR .^
0000060: 2c7e 451a 7f42 0f4b 261d 5f56 265f 333a  ,~E..B.K&._V&_3:
0000070: 391e 1652 4812 7b79 1b7b 547b 7a58 4f47  9..RH.{y.{T{zXOG
0000080: 1e28 767f 4b1e 344d 244c 7e7e 677e 1a3d  .(v.K.4M$L~~g~.=
0000090: 6355 6968 103a 581e 367d 7e2b 552f 7524  cUih.:X.6}~+U/u$
00000a0: 0e0e 706d 6765 551f 7f2f 616b 6533 4a16  ..pmgeU../ake3J.
00000b0: 7272 7e13 421f 157f 674f 231e 6311 2b1b  rr~.B...gO#.c.+.
00000c0: 5d2e 7353 1425 5b5b 2f11 130f 1146 1166  ].sS.%[[/....F.f
00000d0: 3370 1c43 1339 260e 7f15 1c37 773d 4243  3p.C.9&....7w=BC
00000e0: 6921 1642 721f 5a1b 5a38 5727 1b1c 692d  i!.Br.Z.Z8W'..i-
00000f0: 6028 324f 7f19 4430 7254 6942 1726 5520  `(2O..D0rTiB.&U 
0000100: 1b1a 5441 6037 4651 5948 0e1c 4a4a 202f  ..TA`7FQYH..JJ /
0000110: 2c5a 2d68 4b76 5e35 2320 5b6e 1762 2e78  ,Z-hKv^5# [n.b.x
0000120: 727d 385b 7747 2c17 4f1e 5529 354d 763d  r}8[wG,.O.U)5Mv=
0000130: 504e 4f60 485b 1063 6028 4c58 7473 1d31  PNO`H[.c`(LXts.1
0000140: 543d 364e 494c 1721 6358 3a1f 577f 3f5b  T=6NIL.!cX:.W.?[
0000150: 6452 5a60 3a1a 444e 604f 207a 3d29 357f  dRZ`:.DN`O z=)5.
0000160: 6e75 3946 5b1b 233f 444b 3121 4f20 455b  nu9F[.#?DK1!O E[
0000170: 7f28 6c1d 6655 581d 6415 493d 1e7a 3574  .(l.fUX.d.I=.z5t
0000180: 5b1e 2f34 7e66 7f34 1817 4b2a 2446 624f  [./4~f.4..K*$FbO
0000190: 4162 431e 4d2e 657f 2826 2c3d 2e53 6224  AbC.M.e.(&,=.Sb$
00001a0: 1f1d 363d 2b16 3f1e 107d 3421 354f 1873  ..6=+.?..}4!5O.s
00001b0: 5421 7f15 6b62 3c18 523e 1971 5333 273c  T!..kb<.R>.qS3'<
00001c0: 311d 7347 681c 1713 294c 3d11 6b21 235d  1.sGh...)L=.k!#]
00001d0: 7e49 6212 3d1a 2923 450e 0f50 1936 5114  ~Ib.=.)#E..P.6Q.
00001e0: 3753 5217 1211 0e7a 7f33 7e15 190e 1a0f  7SR....z.3~.....
00001f0: 3a0e 5a6c 1721 1863 623b 5853 1715 7268  :.Zl.!.cb;XS..rh
0000200: 117b 4c24 793f 6929 3c7b 1020 1f2b 4253  .{L$y?i)<{. .+BS
0000210: 4e10 0e0e 1720 3020 0e0e 5613 270f 4c2e  N.... 0 ..V.'.L.
0000220: 630f 3229 420e 561a 0e64 547b 2825 0f44  c.2)B.V..dT{(%.D
0000230: 1f19 7e71 1f3f 3054 0e21 4a38 4556 2044  ..~q.?0T.!J8EV D
0000240: 5761 181e 110e 7e7f 2178 211a 0f11 0f41  Wa....~.!x!....A
0000250: 0e66 6d23 272a 5563 3b50 6e13 167b 6f2b  .fm#'*Uc;Pn..{o+
0000260: 6550 3477 5571 2e50 650e 292b 2055 5d62  eP4wUq.Pe.)+ U]b
0000270: 1425 0f33 1e40 1b11 0e5d 1134 105b 3566  .%.3.@...].4.[5f
0000280: 1242 0e4b 0e5f 2818 685f 753c 3d0e 571c  .B.K._(.h_u<=.W.
0000290: 1e73 7b13 5045 5730 4673 6252 5510 7952  .s{.PEW0FsbRU.yR
00002a0: 6e30 4d0e 3949 4c0f 5b44 1620 1753 7e2a  n0M.9IL.[D. .S~*
00002b0: 7a54 512a 4f0e 6031 5d70 0f16 525e 4c4e  zTQ*O.`1]p..R^LN
00002c0: 534a 1443 5e13 6311 361a 4f10 5a60 6e0f  SJ.C^.c.6.O.Z`n.
00002d0: 3e19 4b2c 5e1d 2d43 5b1d 5441 5f4e 5221  >.K,^.-C[.TA_NR!
00002e0: 520f 6719 5657 5851 3d51 5463 0e60 1912  R.g.VWXQ=QTc.`..
00002f0: 5162 727f 4d70 4c4f 7f1f 5233 4d4c 7d6d  Qbr.MpLO..R3ML}m
0000300: 574f 7f3c 4f4d 4e68 6f7d 3950 513a 695e  WO.<OMNho}9PQ:i^
0000310: 547f 7e2e 7f4c 517a 3a54 6f40 5f6f 3457  T.~..LQz:To@_o4W
0000320: 656d 307f 5e7e 564a 5a7a 3060 7b5b 5d45  em0.^~VJZz0`{[]E
0000330: 7374 4076 786d 7e6d 7f6d 2f62 5373 7e75  st@vxm~m.m/bSs~u
0000340: 607f 767e 7d35 7e4f 767e 7a7f 7b4c 7f7f  `.v~}5~Ov~z.{L..
0000350: 1d22 0a66 6f72 2069 2069 6e20 7261 6e67  .".for i in rang
0000360: 6528 3935 293a 0a09 782c 792c 772c 682c  e(95):..x,y,w,h,
0000370: 722c 672c 622c 613d 5b6f 7264 2863 292d  r,g,b,a=[ord(c)-
0000380: 3966 6f72 2063 2069 6e20 735b 693a 3a39  9for c in s[i::9
0000390: 355d 5d0a 0978 2c79 3d33 2a78 2c33 2a79  5]]..x,y=3*x,3*y
00003a0: 0a09 642e 656c 6c69 7073 6528 5b78 2d77  ..d.ellipse([x-w
00003b0: 2c79 2d68 2c78 2b77 2c79 2b68 5d2c 6669  ,y-h,x+w,y+h],fi
00003c0: 6c6c 3d28 322a 722c 322a 672c 322a 622c  ll=(2*r,2*g,2*b,
00003d0: 322a 6129 290a 6d2e 7361 7665 2822 612e  2*a))"a.
00003e0: 706e 6722 29                             png")

It simply unpacks that long string into the parameters for drawing 95 translucent ellipses.

Like many of the other answers, the code is generated using a genetic algorithm. It uses a particular type of genetic algorithm that I invented, which I call a "gene pool algorithm", though it's entirely possible that someone else has also invented it and given it a different name. Instead of having a population of individuals, we have 95 "gene pools", one for each gene. Each gene pool contains 10000 different versions of the gene. A gene contains the parameters for one ellipse (position, shape, colour, alpha and its place in the z order). On each iteration we create two pictures by selecting one gene from each of the 95 pools, and the genes from the lowest-scoring picture replace the genes from the worst-scoring picture, with a little mutation.

I ran it until around the 378000th iteration, which took a couple of days. At that point the score was still going down, but really really slowly, so I doubt it will get much better than this without some changes to the algorithm.

Here's the genetic algorithm code:

import Image,ImageDraw as D
import random
import shutil

# note that the below constants have to match "magic numbers" in the rendering code
# that gets output.
gene_length = 8
n_genes = 95

pool_size = 10000

=import numpy as np

orig ="ORIGINAL.png")
orig = orig.convert("RGB")
orig_pix = orig.load()

def new_pool():
    g = np.random.random((pool_size, gene_length+1))
    for i in range(pool_size):
        x = (ord(encode_char(g[i,0]))-9)*3
        x = np.clip(x,0,387)
        y = (ord(encode_char(g[i,1]))-9)*3
        y = np.clip(y,0,319)
        R, G, B = orig_pix[x,y]
        g[i,4] = R/255.
        g[i,5] = G/255.
        g[i,6] = B/255.
    return g

def mutate_genome(g):
    def mutations():
        return np.random.standard_cauchy(g.shape[0])/50000.

    if np.random.random()<0.1:
        return g

    g[:,4] += mutations() # r
    g[:,5] += mutations() # g
    g[:,6] += mutations() # b
    g[:,7] += mutations() # a
    g[:,8] += mutations() # z order

    # this business is about having mutations that change the left, right, top and
    # bottom of the rectangle, rather than its centre position and size.
    L = g[:,0]*3-g[:,2] + mutations()
    R = g[:,0]*3+g[:,2] + mutations()
    T = g[:,1]*3-g[:,3] + mutations()
    B = g[:,1]*3+g[:,3] + mutations()
    g[:,0] = (L+R)/6 # x
    g[:,1] = (T+B)/6 # y
    g[:,2] = (R-L)/2 # w
    g[:,3] = (B-T)/2 # h

    if np.random.random()<0.15:
        i = np.random.randint(0,n_genes)
#       if np.random.random()<0.5:
        g[i,:] = np.random.random(gene_length+1)
        x = (ord(encode_char(g[i,0]))-9)*3
        x = np.clip(x,0,387)
        y = (ord(encode_char(g[i,1]))-9)*3
        y = np.clip(y,0,319)
        R, G, B = orig_pix[x,y]
        g[i,4] = R/255.
        g[i,5] = G/255.
        g[i,6] = B/255.
    # the Cauchy distribution is heavy-tailed, so this mostly causes very small changes
    # (less than one character), but it can cause very large ones
    g = np.clip(g,0,1)
    return g

def encode_char(a):
    n = int(round(a*113))+14
    if n==ord('"'): n=ord('"')-1
    if n==ord('\\'): n=ord('\\')-1
    return chr(n)

def encode_genome(g):
    # this reorders the genome such that gene i can be accessed with g[i::n_genes]
    # (for golfing purposes in the output code) and makes it a string
    output = [0]*(n_genes*gene_length)
    for i in range(n_genes):
        for j in range(gene_length):
            output[j*n_genes+i] = encode_char(g[i,j])
    output = ''.join(output)
    return output

def fitness(genome, save_filename=None): # actually inverse fitness (lower is better)
    order = np.argsort(genome[:,8])
    genome = genome[order,:]
    s = encode_genome(genome)
    # this is the same image drawing code that appears in the final program"RGB",(386,320),"#4b5b6e")
    for i in range(n_genes):
        x,y,w,h,r,g,b,a=[ord(c)-9for c in s[i::n_genes]]
    # this is the same code that appears in the scoring/validation script:
    img = m
    if img.size != orig.size:
        print "NOT VALID: image dimensions do not match the original"
    w, h = img.size
    img_pix = img.load()
    score = 0.0
    for x in range(w):
        for y in range(h):
            orig_r, orig_g, orig_b = orig_pix[x,y]
            img_r, img_g, img_b = img_pix[x,y]
            score += pow((img_r-orig_r)/255.,2)
            score += pow((img_g-orig_g)/255.,2)
            score += pow((img_b-orig_b)/255.,2)
    if save_filename:
    return score

# hex escape function from
import string
printable = string.ascii_letters + string.digits + string.punctuation + ' '
def hex_escape(s):
    return ''.join(c if c in printable else r'\x{0:02x}'.format(ord(c)) for c in s)

def make_full_program(genome):
    source = '''import Image,ImageDraw as D"RGB",(386,320),"#4b5b6e")
    source += encode_genome(genome)
    source += '''"
for i in range(95):
    x,y,w,h,r,g,b,a=[ord(c)-9for c in s[i::95]]
    return source

# the genetic algorithm code begins here

pool = [new_pool() for i in range(n_genes)]

best_fitness = 10000000
iteration = 0
fittest_genome = None
while (True):
    print iteration
    for iter in range(1000):

        samples = np.random.choice(pool_size, n_genes), np.random.choice(pool_size, n_genes)

        genomes = [0,0]
        for k in [0,1]:
            genome = np.zeros((n_genes, gene_length+1))
            for i in range(n_genes):
                if np.random.random()<0.00002:
                    # very occasionally, draw from the "wrong" pool, so that genes can
                    # be copied across pools
                    genome[i,:] = pool[np.random.randint(0,n_genes)][samples[k][i],:]
                    genome[i,:] = pool[i][samples[k][i],:]
            genomes[k] = mutate_genome(genome)

        fitnesses = fitness(genomes[0]), fitness(genomes[1])

        if fitnesses[0]<fitnesses[1]:
            winner = 0
            loser  = 1
            winner = 1
            loser  = 0

        new_fitness = fitnesses[winner]
        new_genome = genomes[winner]

        for i in range(n_genes):
            pool[i][samples[loser],:] = new_genome[i,:]

        if new_fitness<best_fitness:
            print iteration, new_fitness
            best_fitness = new_fitness
            # this is just so you can watch the algorithm at work
            fitness(genomes[winner], "best_so_far.png")
            best_genome = genomes[winner].copy()
            with open("",'w') as file:

        if iteration%100==0:
            # this is just so you can watch the algorithm at work
            new_fitness = fitness(genomes[winner],"latest.png")
        if iteration%1000==0:
            shutil.copy("best_so_far.png", "frames/" + str(iteration) + ".png")

        iteration += 1

Finally, here is an animation, showing the algorithm at work. It shows the best image generated so far after every 1000 iterations. (The gif file was much too large to embed in this post.)

It can probably be improved by (1) using nneonneo's encoding trick to cram more data into the string; (2) adding a Gaussian blur to the end of the rendering code (but that will make it slower) and (3) improving the algorithm still further. At the moment it reaches a decent score very quickly but then changes really slowly after that - if I slow down the initial convergence somehow, it might find a better result in the end. Perhaps I will implement these things at some point.


Posted 2016-01-23T15:16:57.587

Reputation: 6 641

2Very interesting. I wonder if some of the other box based answers would also be improved by painting transparently. – curiousdannii – 2016-02-03T13:31:31.693

Huh, I've just noticed my code has a terrible bug - the best set of genes makes 95 copies of itself every generation instead of one. (I had thought it was odd that it converged so quickly with populations of 10000 but I couldn't see the bug before.) I'm running a slightly improved version with the bug fixed - let's see if it reaches a better score. – Nathaniel – 2016-02-07T01:46:37.387


Starry, 11428.1894502 10904.3079277 10874.1307958

Starry might not be the best language to do this however it is certainly the most fitting.

You can Try It online however it seems that the output is truncated so you will not get the full image.

This program outputs an uncompressed ppm files to standard out.

          + +* +* +* +* .        +.          + +* + .        + + +.          +*. +*. + . + +* +* +* +* +*. + .      + + +* +* +* +* +* +* +* +*  + *. + .
 +         + +* +* +* +*        +  *      +* +* +* +*  * +   + `  +         + +        +*.. + . + + +** + +**. + .      + + + + +.*.*. + .  +      + * + ''
  + +   +  `  +       + + + +**. +* +*. + .          + +        +*.. + .         + +* +* +* +* +*. + .  +      + * +  ''
  + +   +   `  + + + +** + +**. + .      + + + +..*. + .      + + +. +* +*.. + .  +      + * +   ''
  + +   +    `  +       + + +* +*. + +**. + . + +  *         + +**. + .      + + +. + +**. +*. + .  +      + * +    ''
  + +   +     `  + + +  *        + +  * *. + .      + + +... + .         + +* +* +* +* +*      + *. + .  +      + * +     ''
  + +   +      `  +            + +.. + .          + +         +*.. + . + + +  **      + *. + .  +      + * +      ''
  + +   + +*       `  +         + + +..  + + .  + + + +.*.  + + .  + + +.*. + .  +      + * +       ''

Here is the program output:

enter image description here


In order to make the program output all 123,520 pixels required I divided the image into 8 horizontal bands and created 7 loops the first 6 each print a band while the last one prints two bands of the same color. The code consists of a header, which tells the ppm file how to format itself and the 7 aforementioned loops.

Post Rock Garf Hunter

Posted 2016-01-23T15:16:57.587

Reputation: 55 382

25Congratulations, you've found a solution where the source code looks more like the original image than some of the image approximations here (including, unfortunately, your own). – ceased to turn counterclockwis – 2016-10-17T22:41:03.867


Python 2, 4684.46

1021 bytes.

This uses a very similar decode method to a couple of other answers, but it is in Python 2 so it's base64 encoded data instead of base85.

The encoded data is a 64x48 WebP format image.

import base64,io,PIL.Image'UklGRqQCAABXRUJQVlA4IJgCAACwDgCdASpAADAAPq1Em0mmI6KhNVYMAMAViWIAuzPZOwHN7eu7dJRv7H7zarBrCdDdER6XhybkFwT3wIGHlB1lUUaJZ57w+Ci3Z2w0PE+D9tZzFgHZn9+j+G1LIP++1WTWsLyD/6BI8VTX65vjcr4wuRD+hALdiK+qZ2uGKsAA/sJyKN4OBmJNGqjinqa8bVjXkcGP9zkVighf75VJT80vMeQrM+pbt3sCEa5W8IkgtQD+65nTwFfzVVylNlvc5LM5iC7pQ675eXJzzfdVZHahQf/RVXIT70DP9mLjG6XCpDGKVGd2k2w4Y//xNFvuDF6W1/Y1BhCeY60/1EPcFJcYPqH8AqaD7gLd0v8U6DjG6OGyFXME33IbTThiRYfs0fLUrOgw6EW52O0VW+TIo5ADqnoup7svrnSY/JykVO2VaVtr2nMc1FHGFxiNEux7NkoYeIwjpxA1hTbOwiEO02fXZGNAS0EfJ1f2jPtjyVbZvia+v3hVR4zWVkDp8+reHS4xMy4KHLPl1TNXtdxxJ+P5rW1mZcg9PqJrN1zafhRdVkFKSiU1+SigOtXZ0Ge5r8lte/uaGImm6FYQH/0g4rMPUh4As/5APXi/+rBu3ULEPu57ELp2ed8zLPPIMdqDHNSNZDPvzVQU2tkJ3RIW4fb7cw4fuqXHSGrRJ3jg70zSutBnPRZIERKti27+8g7QCLdAHlSbnz9Rrrf+N6k9AuUm/T1T0+Hc48A3D/hWbfADPWTK32pUz+9OaI7zF4yIx2rRPd3mRWYPgqKF1pD6pJu5FEj9jowD+9Hy8Jn2yd6WwqWgJY2m+crrZqY4GkqNdJX1DWYgRFJbMCsJxtrGkDEx3SIZyIyNRMIEKvpOrkDJkWAqZ+jXAAAA'))).resize((386,320),1).save('a.png')

enter image description here

Here's the code I used to find the best image size and quality setting. I restricted the search space so it doesn't take more than a few minutes to run.

import base64,io,PIL.Image

def score(orig, img):
    w, h = img.size
    img = img.convert("RGB")

    orig_pix = orig.load()
    img_pix = img.load()

    score = 0

    for x in range(w):
        for y in range(h):
            orig_r, orig_g, orig_b = orig_pix[x,y]
            img_r, img_g, img_b = img_pix[x,y]
            score += (img_r-orig_r)**2
            score += (img_g-orig_g)**2
            score += (img_b-orig_b)**2

    return (score/255.**2)

original ='ORIGINAL.png')
original = original.convert("RGB")

lowest_score = 1000000

file_format = '.webp'

for width in range(16, 96, 8):
  for height in range(16, 80, 8):
    small = original.resize((width, height), 2)
    for q in range(70, 50, -1):
        tempFileName = 'a' + file_format;, quality=q)
        file = open(tempFileName, 'rb')
        data =
        data64 = base64.b64encode(data)
        bytes = len(data64) + 109   # Decoding code is 109 bytes
        if (bytes <= 1024):  # Size limit
            decoded =
            cur_score = score(original, decoded.resize((386,320), 1))
            if (cur_score < lowest_score):
              lowest_score = cur_score
              best_q = q
              best_w = width
              best_h = height
              print 'Best %d x %d q %d (%d) : %.2f' % (best_w, best_h, best_q, bytes, lowest_score)

best_image = original.resize((best_w, best_h), 2)
finalFileName = 'best' + file_format;, quality=best_q)

file = open(finalFileName, 'rb')
data =
data64 = base64.b64encode(data)

script = open('', 'wb')
script.write('import base64,io,PIL.Image\n')
script.write('\'' + data64 + '\'))).resize((386,320),1).save(\'a.png\')')


Posted 2016-01-23T15:16:57.587

Reputation: 281


Python 2, 5098.24 5080.04 4869.15 4852.87 4755.88589004

No built-in decompression used! Just PIL's resize utility and a manually-decoded 16-color image. Therefore it should be eligible for the bounty.

The program contains embedded non-ASCII chars. It is 1024 bytes long and looks like this:

from PIL.Image import*
frombuffer('RGB',(40,41),''.join(')9„ ˜§˜ qˆš Vn• 8OŠ Ql‘ §§g ¶¤4 w‡v M]j 8FR AG8 )4V ! 7Jr ).0'.split()[int(c,16)]for c in'»«»ÝýÝßÝßûûÿ¿úª»ÿÿÿºÿûÿÝÝÝÝÝݺÿýÿú™¬ÏÌÿÿû»ýÝÝÝÝÝÝÿÿû»¿üüÌê­ÿÿ¿ÝÝÝÝÝÝÝÿªûÿýʬ©ú»ú¯«ÝÝÝÝÝýÿÿúÿýÝ߯™ú©®üªÝÝÝÝÝÝÿûÚ¬ýÿÿ«ÿÿÌϺÏÝÝÝÝÝßÿû¹¬¯ÿʯÿšüÌÿÌßßÝÝÝßÌúª¯Î¬ÏüΙš™üÌßÝÝÝÝÿί̮îªÿÊîåššÿÿýÝÝÝÝüÿ©®™žª©™ž™™™þLÏÝÝÝÝÿüž®ìî©©™™•U?ÝÝýßìÌÌäîÌäéîž•™©C3=ÝßýþYÌåîîîÌDDDS3TS2Ýßý’5UU9îîÏþÎDS352Ýßù!5RUžÌÏÎÏÌã352ÝÚ©2†("U9™%žÏþUD!#­ÝÚã("&"""9¬Ïÿ’äíÝþ‘SS5!""ÿÿDDíÝþ‘3U4UR#2#­ÜDSÝó!^SEäS35Q+ÝE6oÝõ1N5DER32C)%VoÝù233#UR#"5!HÝÎU2#"3S3U32515SÝ®îE224äE%TR53!2"?ÿNÎE"%E3U2""523""9ÿ^Äå"4U3%S9US335Q"25ÿ#ã%S352"UNUU335U%S#ÿ"8eS233"^DUT5353S#2¯#3.ã233#DDC5S2"#2"2©###ÎU5S5US34S3^Å222.DE3E4X52fa4ÎNÄDS5"ES5R>!U!gwaTDNÉ•56““5"î6#SgwqDD@¦xDE224îS5SwfaDD\0ùiîUYYîîDäDSwÄD@þžîDîîîîãUÌî2gfÄDàüà@@Î8ˆìä3!fvå"PÌàäNI”Dî6hDîTQfÃf ÎîÄ(6„îàX…NND’#Ãf,ÉlĈ9î”îDîDDDTC#UÉ"œÉœä“NI•NìÎîäNUTTî'.encode('hex'))).resize((386,320),3).save('o.png')

and in hex:

0000000: efbb bf66 726f 6d20 5049 4c2e 496d 6167  ...from PIL.Imag
0000010: 6520 696d 706f 7274 2a0a 6672 6f6d 6275  e import*.frombu
0000020: 6666 6572 2827 5247 4227 2c28 3430 2c34  ffer('RGB',(40,4
0000030: 3129 2c27 272e 6a6f 696e 2827 2939 8420  1),''.join(')9. 
0000040: 98a7 9820 7188 9a20 566e 9520 384f 8a20  ... q.. Vn. 8O. 
0000050: 516c 9120 a7a7 6720 b6a4 3420 7787 7620  Ql. ..g ..4 w.v 
0000060: 4d5d 6a20 3846 5220 4147 3820 2934 5620  M]j 8FR AG8 )4V 
0000070: 1d21 1e20 374a 7220 292e 3027 2e73 706c  .!. 7Jr ).0'.spl
0000080: 6974 2829 5b69 6e74 2863 2c31 3629 5d66  it()[int(c,16)]f
0000090: 6f72 2063 2069 6e27 bbab bbdd fddd dfdd  or c in'........
00000a0: dffb fbff bffa aabb ffff ffba fffb ffdd  ................
00000b0: dddd dddd ddba fffd fffa 99ac cfcc ffff  ................
00000c0: fbbb fddd dddd dddd ddff fffb bbbf fcfc  ................
00000d0: ccea adff ffbf dddd dddd dddd ddff aafb  ................
00000e0: fffd caac a9fa bbfa afab dddd dddd ddfd  ................
00000f0: ffff faff fddd dfaf 99fa a9ae fcaa dddd  ................
0000100: dddd dddd fffb daac fdff ffab ffff cccf  ................
0000110: bacf dddd dddd dddf fffb b9ac afff caaf  ................
0000120: ff9a fccc ffcc dfdf dddd dddf ccfa aaaf  ................
0000130: ceac cffc ce99 9a99 fccc dfdd dddd ddff  ................
0000140: ceaf ccae eeaa ffca eee5 9a9a ffff fddd  ................
0000150: dddd ddfc ffa9 ae99 9eaa a999 9e99 9999  ................
0000160: fe4c cfdd dddd ddff fc9e aeec eeee aaba  .L..............
0000170: a9a9 9999 9555 3fdd ddfd dfec cccc e4ee  .....U?.........
0000180: cce4 e9ee 9e95 99a9 4333 3ddd dffd fe59  ........C3=....Y
0000190: cce5 eeee eecc 4444 4453 3354 5332 9ddd  ......DDDS3TS2..
00001a0: dffd 9235 5555 39ee eecf fece 4453 3335  ...5UU9.....DS35
00001b0: 3211 9ddd dff9 2113 3552 559e cccf cecf  2.....!.5RU.....
00001c0: cce3 3335 3212 9ddd daa9 3286 1228 2255  ..352.....2..("U
00001d0: 3999 259e cffe 5544 2123 addd dae3 1128  9.%...UD!#.....(
00001e0: 2226 2211 1212 2222 39ac cfff 92e4 eddd  "&"...""9.......
00001f0: fe91 1112 5353 3521 2211 1111 221a ffff  ....SS5!"..."...
0000200: 4444 eddd fe91 1111 3355 3455 5223 3211  DD......3U4UR#2.
0000210: 1123 addc 4453 9ddd f321 1611 5e53 45e4  .#..DS...!..^SE.
0000220: 5333 3551 1112 2bdd 4536 6fdd f531 1111  S35Q..+.E6o..1..
0000230: 4e35 4445 5233 3243 1111 1129 2556 6fdd  N5DER32C...)%Vo.
0000240: f932 1112 3333 2355 5223 2235 2111 1111  .2..33#UR#"5!...
0000250: 1348 8fdd ce55 3223 2233 5333 5533 3235  .H...U2#"3S3U325
0000260: 3111 1111 3553 9ddd aeee 4532 3234 e445  1...5S....E224.E
0000270: 2554 5235 3321 1111 3222 3fff 4ece 4522  %TR53!..2"?.N.E"
0000280: 2545 3355 3222 2235 3233 2211 1222 39ff  %E3U2""523".."9.
0000290: 5ec4 e522 3455 3325 5339 5553 3333 3551  ^.."4U3%S9US335Q
00002a0: 2232 35ff 23e3 2553 3335 3222 554e 5555  "25.#.%S352"UNUU
00002b0: 3333 3555 2553 23ff 2238 6553 3233 3322  335U%S#."8eS233"
00002c0: 5e44 5554 3533 3533 5323 32af 2333 2ee3  ^DUT5353S#2.#3..
00002d0: 3233 3323 4444 4335 5332 2223 3222 32a9  233#DDC5S2"#2"2.
00002e0: 2323 23ce 5535 5335 5553 3334 5311 1113  ###.U5S5US34S...
00002f0: 335e 04c5 3232 322e 4445 3345 3458 1235  3^..222.DE3E4X.5
0000300: 3266 6112 34ce 4ec4 4453 3522 4553 3552  2fa.4.N.DS5"ES5R
0000310: 3e21 1255 2167 7761 5444 4ec9 9505 3536  >!.U!gwaTDN...56
0000320: 9393 3522 ee36 2353 1167 7771 4444 40a6  ..5".6#S.gwqDD@.
0000330: 7844 4532 1212 3234 ee53 3553 1177 6661  xDE2..24.S5S.wfa
0000340: 4444 5c30 f969 04ee 5559 59ee ee44 e444  DD\0.i..UYY..D.D
0000350: 5311 7716 11c4 4440 fe9e ee44 eeee eeee  S.w...D@...D....
0000360: e355 ccee 3211 6766 11c4 44e0 fce0 0440
0000370: 0e0e 40ce 3888 ece4 3321 6676 11e5 2250  ..@.8...3!fv.."P
0000380: cce0 e44e 4994 44ee 3668 44ee 5451 1666  ...NI.D.6hD.TQ.f
0000390: 11c3 6620 ceee c428 3684 eee0 5885 4e4e  ..f ...(6...X.NN
00003a0: 4492 1111 23c3 662c c96c c488 39ee 94ee  D...#.f,.l..9...
00003b0: 44ee 4444 4454 4323 55c9 229c c99c e493  D.DDDTC#U.".....
00003c0: 4e49 954e ecce eee4 4e55 5454 ee27 2e65  NI.N....NUTT.'.e
00003d0: 6e63 6f64 6528 2768 6578 2729 2929 2e72  ncode('hex'))).r
00003e0: 6573 697a 6528 2833 3836 2c33 3230 292c  esize((386,320),
00003f0: 3329 2e73 6176 6528 276f 2e70 6e67 2729  3).save('o.png')

and generates this picture:

program output

This program abuses the fact that you can basically shove raw bytes into Python source code as long as you escape NULs and backslashes.

The program itself consists of a 16-entry palette (the | separated string) and a 40x41 16-color image (encoded with 4 bits per pixel, and decoded by abusing .encode('hex')). The image is resized to the appropriate size with a bicubic filter, and that's it.

The actual image was generated with ImageMagick:

convert -filter Cosine -resize 40x41\! ../../ORIGINAL.png +dither -alpha off -colors 18 -compress none im.bmp

and the palette and image data were extracted from the resulting BMP. (Note that we request 18 colors from ImageMagick, since IM automatically inserts some unused entries).

The palette was rearranged slightly to reduce the number of escaped characters in the binary data, and the final binary data was edited a bit by hand to get the whole thing to fit in 1024 bytes.

EDITED: Golfed the code a bit, and improved the accuracy by requesting 17 colors from ImageMagick.

EDITED: Disabling dithering produced a huge improvement in score. It now scores well under 5000, and is becoming competitive with off-the-shelf compression algorithms!

EDITED: Adding -filter Cosine gives another sizeable improvement. Aggressive golfing, with thanks to @primo for the UTF-8 BOM trick, allowed me to tack another row onto the picture, further improving the score.


Posted 2016-01-23T15:16:57.587

Reputation: 11 445

2+1 for nice abuse of the python encoding to store binary data. – Brian Minton – 2016-01-28T15:57:55.870

What color is the ! in the pallete encoding? Or is it unused? – 2012rcampion – 2016-02-01T01:02:12.337

2@2012rcampion: The ! is actually flanked on both sides by non-printable characters. The full color is #1d211e, which is a slightly bluish dark grey. – nneonneo – 2016-02-01T06:15:30.520

Oh duh, I should have checked the hexdump. – 2012rcampion – 2016-02-01T06:19:04.093

2Congrats, this wins the bounty! – Nathaniel – 2016-02-02T12:35:15.770

3If it helps any, the #coding:latin line can be replaced by a UTF-8 byte order mark:  (0xEF,0xBB,0xBF). – primo – 2016-02-03T04:39:49.743

1@primo: Wow, I'm really surprised that works - I would have assumed Python would reject the malformed Unicode. I'll see if this change (saving 11 bytes!) will allow for a better score. – nneonneo – 2016-02-03T05:23:08.123

2@primo: great, thanks to your tip and a bit more aggressive golfing I was able to shove in an extra row, improving the score by about 40 pts. Combined with an improved ImageMagick command, it's an improvement of nearly 100 points. – nneonneo – 2016-02-03T06:18:27.233


zsh + FLIF + ImageMagick, 4358.14

With BPG stealing the spotlight as a lossy codec, I've refined my lossless upscale approach to use FLIF instead of PNG, using @nneonneo's zsh trick. ImageMagick is only used here as an upscaler.

The hexdump (this time with xxd, I didn't realize hexdump was non-standard in my last answer):

00000000: 666c 6966 203d 2874 6169 6c20 2d6e 202b  flif =(tail -n +
00000010: 3320 2430 2920 6f2e 706e 670a 6578 6563  3 $0) o.png.exec
00000020: 2063 6f6e 7665 7274 206f 2e70 6e67 202d   convert o.png -
00000030: 7265 7369 7a65 2033 3836 7833 3230 2120  resize 386x320! 
00000040: 6f2e 706e 670a 464c 4946 3331 0044 0038  o.png.FLIF31.D.8
00000050: e113 7e24 321e fb1e 4df6 d7e0 cfa8 f513  ..~$2...M.......
00000060: e1fa 32fb cf01 c186 dc85 efb3 2ea7 9415  ..2.............
00000070: d1de e100 680a e7c9 455c 42c6 2283 9d32  ....h...E\B."..2
00000080: b06c b863 71ce 7c2b 9cd6 be17 3610 0ebd  .l.cq.|+....6...
00000090: 01ed c8c5 7b9b d687 3821 e3a5 6e47 846c  ....{...8!..nG.l
000000a0: 12b6 9346 d2a6 2760 eef0 f558 caea 260d  ...F..'`...X..&.
000000b0: c8d5 b0e0 f09c 53a1 df70 7277 9b79 02a9  ......S..prw.y..
000000c0: 2813 2292 4f65 8fbc 97cc ea65 51ea d933  (.".Oe.....eQ..3
000000d0: 3989 4efe 2d86 23cd 1142 8f02 ff29 edd1  9.N.-.#..B...)..
000000e0: 3f5d ae15 a973 0cc9 3750 f55c ec0b 2870  ?]...s..7P.\..(p
000000f0: c292 7085 8a38 1a5c d525 aa82 3a70 cb89  ..p..8.\.%..:p..
00000100: 0513 0a8a 7bba cfb7 461c ff14 c160 06b6  ....{...F....`..
00000110: 67ae 3570 a2d1 d056 83e2 36b7 3ca4 d3c4  g.5p...V..6.<...
00000120: 46a0 b5ca 4722 848a 2328 2f25 95b3 2cde  F...G"..#(/%..,.
00000130: 8c0a 9acb dee4 5ef3 9693 e1ef cf7d 0578  ......^......}.x
00000140: abb3 c853 f6f0 29e4 2d25 cf80 ec3a e91e  ...S..).-%...:..
00000150: 3863 5401 26e3 af1c 3691 15b2 a0b8 fc16  8cT.&...6.......
00000160: c773 ffdc bbac 078d c4ea 8b9a 2763 29a8  .s..........'c).
00000170: 1faa 598d eeff 3492 45eb c79d c014 b75c  ..Y...4.E......\
00000180: 61dd 1cf4 64d6 ebe8 9c9a 2825 ed65 aa94  a...d.....(%.e..
00000190: 2b86 d197 233d b45c 5f8a cc52 1752 7357  +...#=.\_..R.RsW
000001a0: e508 fa96 cb9d cab5 e4fa 02d9 0290 4aec  ..............J.
000001b0: 0173 3520 b9b0 a9ac 4d59 23c7 7dac e26d  .s5 ....MY#.}..m
000001c0: 4140 9bb6 f32a 795f 3ff1 2808 1718 0ba0  A@...*y_?.(.....
000001d0: ceae b37b de22 cee7 8c34 0fb3 b8ef 081d  ...{."...4......
000001e0: 9baa 29c8 341c 6f71 a0d4 4bc7 0699 fdb0  ..).4.oq..K.....
000001f0: 08a7 372b 65c1 a57f 6600 edd7 dc4a a698  ..7+e...f....J..
00000200: 102d 06ea 7c07 b5de b187 8d03 27a0 7fe9  .-..|.......'...
00000210: 1820 4409 d0d1 a939 4fb7 8697 18ed 5de0  . D....9O.....].
00000220: 4015 57ba d209 1620 648f 6aff bbbc b010  @.W.... d.j.....
00000230: a957 3c54 9a2e e9bb d552 9436 e73a 216f  .W<T.....R.6.:!o
00000240: 7e14 945c 9af0 49ef 29db c559 1184 b29c  ~..\..I.)..Y....
00000250: b0bc 8838 2c6d c695 e68e 0857 5998 8580  ...8,m.....WY...
00000260: 720d 0b19 dd46 929b e327 e6ee e182 b52e  r....F...'......
00000270: 09d6 b06d c8e5 fd3c 862b e729 eccd 52d6  ...m...<.+.)..R.
00000280: 0300 cacc 5cbb e08f 9314 75df 8576 410c  ....\.....u..vA.
00000290: 6c7d 49bc fab2 a130 da4a ca40 ae1d 2677  l}I....0.J.@..&w
000002a0: 3f5e c6a4 3bf1 5d1e 4819 0015 e2ca b349  ?^..;.].H......I
000002b0: 9b90 783c 8e33 4571 4b5d c436 45b6 d20b  ..x<.3EqK].6E...
000002c0: cdf2 7fcc 6a24 f2d9 82b3 8740 26a1 f6ec  ....j$.....@&...
000002d0: e134 00e1 5ef0 a519 b6a9 055a b0d6 6e10  .4..^......Z..n.
000002e0: 7330 cb51 7042 a472 c3f1 3f70 e161 fde7  s0.QpB.r..?p.a..
000002f0: 4cd0 4dd6 a887 a977 9cab 11a3 5860 b88c  L.M....w....X`..
00000300: 6c26 75f3 fa55 802a a38c 81e0 7519 8233  l&u..U.*....u..3
00000310: 0e86 f5db 4c70 7c22 9c4c 5ba1 602a 530d  ....Lp|".L[.`*S.
00000320: 5b74 9c67 718e 471f e69a 2258 d207 cd93  ["X....
00000330: 0c92 0c1f 1aa1 2201 7906 d3dd 4e58 ab9d  ......".y...NX..
00000340: e13e 3b9f 870c a69d 5cb2 80d9 6b83 6cd0  .>;.....\...k.l.
00000350: e3df 8a96 7217 0e07 e654 0633 5e52 fb5d  ....r....T.3^R.]
00000360: 76a4 6e05 33c8 bc5b 7bf1 9819 5c05 3705  v.n.3..[{...\.7.
00000370: 3ea6 cf51 3bcf 031a d103 9117 4622 da77  >..Q;.......F".w
00000380: 6018 ddbf fd6f 5a17 989b 1938 2a37 a326  `....oZ....8*7.&
00000390: 0fa1 1507 9d1f 8fee 6116 2dc6 653b ed48  ........a.-.e;.H
000003a0: 3543 4ff8 77b3 d1c7 41b3 0fc2 a6d6 7bee  5CO.w...A.....{.
000003b0: a2dc f047 fae4 da02 c055 25b6 2cd1 0e51  ...G.....U%.,..Q
000003c0: b382 fede ab22 1927 ac66 b8a4 8cf1 094d  .....".'.f.....M
000003d0: e0cb 9288 a105 cb3e dbb0 4e04 e110 68fb  .......>..N...h.
000003e0: 78d0 c36f 390a db12 ba16 b055 a367 bacf  x..o9......U.g..
000003f0: 20                                        

starry, compressed with FLIF

I've generated the script using... another script:

convert ORIGINAL.png -filter Lanczos2 -resize x56 - | pngquant --speed 1 -f 10 > i.png
flif -N i.png o.flif
echo 'flif =(tail -n +3 $0) o.png' >
echo 'exec convert o.png -resize 386x320! o.png' >>
cat o.flif >>


Posted 2016-01-23T15:16:57.587

Reputation: 37 067


I'd object to make such a distinction between lossy and lossless codecs: even if png itself is lossless, your whole encoding is most definitely lossy. It does work really well, though; in fact I'd say this looks rather better than the top-scoring BPG with its localised artifacts.

– ceased to turn counterclockwis – 2016-01-29T23:59:35.833


HTML/JavaScript, 10855.83 8000.55 (±~5, based on the browser)

Scores may vary slightly due to browser or GPU differences.

You have to right-click > Save Image As to save the canvas data as an image, but that's the only interaction that's required.

I used GIMP to select certain areas and find their average. In particular, the color picker tool and "layer difference" feature were very helpful.

Attempt #1 (10855.83)

<canvas width="386" height="320" id="c">
var canvas = document.getElementById("c");
var context = canvas.getContext("2d");


context.moveTo(33, 319);
context.lineTo(63, 26);
context.lineTo(97, 200);
context.lineTo(179, 319);



Attempt #2 (8000.55)

enter image description here

<canvas width="386" height="320" id="c">
var canvas = document.getElementById("c");
var context = canvas.getContext("2d");

function f(x,y,w,h,c)

//Top area

//Bottom area


//Sun outer


//Foreground mountain


Posted 2016-01-23T15:16:57.587

Reputation: 251

20If you used a single letter instead of "context" and removed unnecessary whitespace you might have room to add additional detail to the image. – trichoplax – 2016-01-29T14:04:51.650


Mathematica, 5076.54

Weighing in at exactly 1024 bytes, I finally managed to beat nneonneo's score... until he improved it an hour ago =(

Uses no "off-the-shelf" compression algorithms.

f=IntegerDigits[FromDigits[ToCharacterCode@#-32,95],#2]~Partition~#3&;Image[Array[Nearest[f["GYWh6t@/0EwgZTWL9+IfA51 Qn0&q3k2eb[cFp{iDJp\\8:_I9v~0-035)!z^br^=,Jy.0X.wnXr\"&A'l3N$\"rHVD]ANb<[c-HyQ3k]\\/F.L)^F[FsTe]>9=Y MBP@-Y7,U1n2PgeTYL|d^@s%)|vDUsI63?3+5zt`4;0}7 L )pg$G\"S=.e`n@d0Qpb-<L@zy'cH<KJhG4D0+DluH1hvFZ%6<>w,2uQJQhD\\@-bq=OChgV}r[^o\\h/1w!5_{SVjv0a1\"?j.z%tRxXX$cC2[@K){*eQ/|$W%[{kFXnmL'EnM`-zs$OyS]mnL$]Qu,AIN%~n}zG{SD[q<v%IP3Tp]\"1Gu0?|L=XB =6n+]mAU20rDZ|F&V#(h? xxJeK^}e}% n6MaNqA*\"vitzT8e=:>&YxNb'&Wiw\\yjJ#l^",409,2]-11->(#+{51,-41}&/@f["<Z? ZN7Mc{N{gJm}@.U'336)10$MTyi $D3Y@,r$g\"vk)~rU-]=G?dQJ0j*V~VTLz!yVCU~]=>VrrN<{ROjqTvLl!s)/8B{\\xpJ.8\"R~)A.1xA9{ ab8oq8bSoyJ:P_7OXdr@(&H>vp~sjV+M^1'Js;g&@2t/1Z)Xj=dwnLnm1Fd?`dpQ3AN>)n@!+cL'^j}N(c%~~F06||Vci{L_O*K>5i[>20mf8>WYKKuk\\T7d}L?xHDuS^GNr:o/(yq KvH=KEQX[e&faTh0&Ra+B0<9PLB)WPAe8\\#B$oo.AtrM\\*\"=1JZ0s/CBz{e9;8aH|w-#N_l>a.^@/M`[* };@#l)C(lXG=CVL:]?D@W=(3k{o.`jBG#g-33LX&lE+WHI",231,2]).{{0.479,0.574,0.664},{0.591,0.349,-0.727}},{##}][[1]]&,{320,386}],"Byte"]

enter image description here

  • The colors and centers of the Voronoi cells are base-95 encoded, using the full ASCII range.
  • The colors are expressed in a two-dimensional subspace of the RGB cube to save space.

(Better description later)


Posted 2016-01-23T15:16:57.587

Reputation: 1 319

Too bad you didn't win the bounty, I was really hoping an approach like this would take it - I've given you an "honourable mention" in the question. I'm looking forward to the explanation of how this was produced. – Nathaniel – 2016-02-02T12:47:06.667

@Nathaniel I may have just missed the bounty by hours; my optimizer is still running, and I just reached 5000. – 2012rcampion – 2016-02-02T13:02:09.477

Well, you might be in luck - I already announced a second bounty if an optimisation based approach takes first place among the non-compression based answers. (See the updated question text.) – Nathaniel – 2016-02-02T13:24:56.357

Heh, I was out to beat Sleafar (and he would have won anyway, since his score is lower than yours). Was quite surprised that the change I made (disabling dithering) made such a big impact. – nneonneo – 2016-02-02T16:16:38.090

+1 for Voronoi and for using a subplane of the RGB cube. Did you try using any of the answers from to optimise the Voronoi diagram? (Of course, those were aiming at visual similarity, not minimising an objective error, so YMMV, but I think some of them might yield pretty good results.) Also, have you tried using just a line through the RGB cube so you can use twice as many Voronoi cells?

– Martin Ender – 2016-02-03T14:07:59.243


Scala, 6003.56

993 characters. One import is scala image library. Second import is base 91 encoder.

object T extends App {
import, com.sksamuel.scrimage._
val a = "vuk:eJs4+BAAN/<MCG4DAA#TAAAA8FMAAA<cPjTTAAJ7oG]t>um^8Wm}ozBAn|m(qVi2Yt+j8GnHAD%FaO,BjL2c%w%z,M+OyQy9eR0wkSXUa1|1pm1$?XSrkFs(;9/]Vk3<%.^UVyt~_Pnh?n7;[v06@I`oB{.2OCGT/*v/pS|`XN5.rp1`5)M$wy49cuk0G=%lVCEbxW{^Wd*{JR]hZM>S0$&Eo1,wk6]/WkAK:{$}d__nf_YZ&qRlB;<S5T8OVF3C^}$*PYcqn$SvGU[8Q69kFgT#/l@+7)l><x&|XNO&eajx.0k^mY)MvyQ4sQoqvY7MpyaPJ@u_O&9[@$dr1c>(!QUN+:&F#ZZSX*LxcCIR)&,,0=T:1&IuG[r|yQ>[)oFJTvpvRaM5Z6#oGj^%6Xqqn[Uo2AoeoEuvt2A7_N7TL)9_+[oq^J_3gwqhg$^#+{n[cW(0H}cP\"ek=a34Cpt:u]Sab;~&;FlT_iy6fMw`F>z(MQ^}vvoAy?@XxV26Se8:FT)T]]N2KH`b4%l_Zuu@y=0fTH1WeQ58~~[(QAKYhf]^Bel^[Tb44/G96&^2O@_6L072:)lRpMDZYMB]i9GM]t?t0%Wq99/0Ti=gjDi6]P7b3:dU$N0e&1Z?PaY`Hb`h7l)%N`fsuzV;/x`Uce.8:?K[@0|ckpCe/emO7!8^~eZsN[$)iOZ0zYW4VE]K5?RbO|GYzx<a2C!:*]<PuzpsIie8#+x[5U6xZ\"e}k7y[5JVQ5z:]ZR2Gds&g^+U=LJ:hR*KFgJ[YF<<Av}L8WcAAA6yQMFGPe=hnB"
Image(new Base91().decode(a.getBytes)).scaleTo(386, 320, ScaleMethod.Lanczos3).output("a.png")

This is the base91 data:

enter image description here


Posted 2016-01-23T15:16:57.587

Reputation: 423

Yep just did that sorry :) – monkjack – 2016-01-23T19:45:38.020

This look like 993 chars – Downgoat – 2016-01-23T20:06:31.297

So it is. Updated. – monkjack – 2016-01-23T20:08:34.307

Just out of curiosity, it's there any significance of having the import inside of the object? – Carcigenicate – 2016-01-24T22:42:50.627

No, it was just easier to copy and paste I thought – monkjack – 2016-01-24T22:49:02.837

@monkjack oh, ok. I didn't even know that that was legal lol. – Carcigenicate – 2016-01-25T15:28:38.407


Java, score 12251.19

import java.awt.*;
import java.awt.image.*;
public class Y{
    static Graphics2D g;
    static void rect(int...p){g.fillRect(p[0],p[1],p[2],p[3]);}
    static void col(int...p){g.setColor(new Color(p[0],p[1],p[2]));}
    public static void main(String[]a)throws Exception{
        BufferedImage b=new BufferedImage(386,320,1);

Based on this Mathematica answer, but with more rectangles. I'll probably continue to modify this later.


enter image description here

Some previous versions:

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here


Posted 2016-01-23T15:16:57.587

Reputation: 11 342

11SE inserts the link so images can be clicked to view them individually at full size. – Calvin's Hobbies – 2016-01-24T07:04:12.000

@Calvin'sHobbies But these images are small enough that the full size fits in the post perfectly anyway. – SuperJedi224 – 2016-01-25T01:30:21.730

7Not necessarily for all screen/window sizes. – Calvin's Hobbies – 2016-01-25T01:54:23.697


So this is how this game's graphics were designed

– Luis Mendo – 2016-03-28T23:51:11.440


Python 2 (no built-in compression), score 4497.730

This uses the same manually-decoded 16-color image approach as my previous answer, but this time I actually applied a gradient-descent optimization strategy to significantly improve the score. The previous submission scored 4755.886 points, whereas the new submission scores over 250 points better, beating out a lot of built-in compression approaches in the process.

As before, the final program is exactly 1024 bytes long. In fact, the raw output of the optimization algorithm contained four bytes that were escaped (\0), and which I had to "fudge" in order to reduce the byte count to 1024 bytes. Without the fudge, the 1028-byte program would score 4490.685 - 7 points better.

The basic idea is to optimize both the palette and data jointly. In a single iteration, I search over all tweaks of the palette (basically, every modified palette which differs by 1 in some color component) and pick the modified palette which best improves the score. Then, I search over all tweaks of the data (every modified index array in which one pixel is changed to some other palette entry) and choose a modification which reduces the score (here I don't care about best, because I don't want to fruitlessly search the full space of over 25000 tweaks every iteration).

Finally, when generating the final program output, I run another optimization pass which rearranges the palette to minimize the number of backslashes required in the final output (e.g. for the program shown below, the palette was rearranged using the hex table "0e3428916b7df5ca").

This approach yielded both a significant numerical and perceptual improvement over the previous naïve ImageMagick approach. Previous submission output:

Old submission's output

And new submission output:

New submission's output

The new optimization-based approach has significantly more detail and accurate color reproduction.

Here is the hexdump of the final program:

0000000: efbb bf66 726f 6d20 5049 4c2e 496d 6167  ...from PIL.Imag
0000010: 6520 696d 706f 7274 2a0a 6672 6f6d 6275  e import*.frombu
0000020: 6666 6572 2827 5247 4227 2c28 3430 2c34  ffer('RGB',(40,4
0000030: 3129 2c27 272e 6a6f 696e 2827 b39b 2620  1),''.join('..& 
0000040: b4b9 7e20 2634 8120 5567 7520 3547 7320  ..~ &4. Ugu 5Gs 
0000050: 242e 5620 1c1f 1b20 7890 a420 4348 3d20  $.V ... x.. CH= 
0000060: 9fae a420 3a52 8e20 262b 3220 7d90 7f20  ... :R. &+2 }.. 
0000070: 3a49 5720 4a67 9720 5d79 9c27 2e73 706c  :IW Jg. ]y.'.spl
0000080: 6974 2829 5b69 6e74 2863 2c31 3629 5d66  it()[int(c,16)]f
0000090: 6f72 2063 2069 6e27 8388 88b6 86b6 6b66  or c in'......kf
00000a0: 6bb8 b8b8 8888 dd8b bbbb bb8d b688 bb66  k..............f
00000b0: 6666 6666 b68d bdb6 bb88 33bd 5b55 bb68  ffff......3.[U.h
00000c0: b888 b66b 6666 6666 66bb 6688 88d8 bbb5  ...kfffff.f.....
00000d0: 553d 868b bb8b 6666 666b 6666 66b6 3d68  U=....fffkfff.=h
00000e0: bb66 5dd4 8363 b8bd dbd8 6666 66b6 66b6  .f]..c....fff.f.
00000f0: bbbb bbb6 d666 6bd6 f3bd d3d4 b5dd 666b
0000100: 6666 6666 b668 63d5 66b8 5bd8 66bb 5b5b  ffff.hc.f.[.f.[[
0000110: 884b b66b 666b 666b 686d 8d85 dbb6 dd4b  .K.kfkfkhm.....K
0000120: 5b33 6db5 b5bd 6b6b 6b66 66bb d5b3 dddb  [3m...kkkff.....
0000130: bad4 58b5 d435 3b33 b555 6b66 6666 66bb  ..X..5;3.Ukffff.
0000140: 5346 db84 d45d bbbd 54d7 4d3b 5bbb b6b6  SF...]..T.M;[...
0000150: 6666 66b5 6bd3 d4f3 eddd 4333 c5d3 3c83  fff.k.....C3..<.
0000160: b2a5 5666 6666 66b6 b534 84d5 4444 b8b8  ..Vffff..4..DD..
0000170: b383 8333 3ffe 7666 66b6 6b42 2555 2eaa  ...3?.vff.kB%U..
0000180: 5b4a 4343 ad33 3388 43fe f666 b6d6 64e3  [JCC.33.C..f..d.
0000190: 564f d534 4455 aaea aaef ffee e737 3666  VO.4DU.......76f
00001a0: 6bb6 c73e ef3a f352 445b b25a eaee ffee  k..>.:.RD[.Z....
00001b0: ff79 3666 6b63 7c73 f3ec ee34 b55b 53b6  .y6fkc|s...4.[S.
00001c0: 5baf eee3 3717 3666 6dda 3fc1 ccc3 cc3a  [...7.6fm.?....:
00001d0: f333 7e34 bbb2 feea 7c7e d666 6ddf 99f3  .3~4....|
00001e0: c7c1 c717 c777 7f77 f35b b6bb 374a 4666  .....w.w.[..7JFf
00001f0: be49 9999 aeee fac9 cc99 997c c79b 5bbb  .I.........|..[.
0000200: a3ae 4666 bad9 9197 e7ae fae4 acf3 ef99  ..Ff............
0000210: 9cf7 d6b5 4afe 3666 bf79 9099 a5ef af4a  ....J.6f.y.....J
0000220: ee77 ce49 999f 7b66 aeec 1b66 be39 1999  .w.I..{f...f.9..
0000230: a474 a2ef a7ef fcaf 1979 997c 7ee1 1b66  .t.......y.|~..f
0000240: baf7 999f efff 7cee e77e 7ffa 7999 9999  ......|..~..y...
0000250: 1eac c666 d4fe f7ff 7fff feff eeff e7ef  ...f............
0000260: a919 1999 f3ae a6b6 d4a5 aef7 ecf4 44a3  ..............D.
0000270: 7aa4 a7fe fe79 9199 e777 76b6 e4b4 ae77  z....y...wv....w
0000280: 7f4e ffef 3977 9fee f7ee 7719 9777 f36b  .N..9w....w..w.k
0000290: e45e 4e7f fafe ff7a eec3 3eef feff eaa1  .^N....z..>.....
00002a0: f7f7 3f66 9f5f ceef fe7a e777 aeaa ee3e  ..?f._...z.w...>
00002b0: efee fefe caae c766 77fc 1aff f7ff 7f77  .......fw......w
00002c0: edea eeea faff faff ef9f e7d6 7fff c547  ...............G
00002d0: 37ef fef7 4eaa affe e7ff 77ff fcf7 c7dd  7...N.....w.....
00002e0: 7e9f ff64 e3ef a7fa faef fffa ee19 197f  ~..d............
00002f0: fcf5 2abf f7fc f774 aaa3 e74e 7a3c 77ee  ..*....t...Nz<w.
0000300: ff11 119c ca54 a2ba 2aef f3cc af3f 7ee7  .....T..*....?~.
0000310: f2c1 17ae f110 5c30 17ea aa2a b3c3 2eef  ......\0...*....
0000320: fc33 3fee f745 7ccf ef91 1001 09aa 2aaa  .3?..E|.......*.
0000330: bc0c 2a4e e717 97f7 fa5a affe ef91 1011  ..*N.....Z......
0000340: 1c2a aa2a 6c03 2a44 fea3 3444 24e4 4aa2  .*.*l.*D..4D$.J.
0000350: ee91 0111 195a aa4a 6434 222a 52d4 4422  .....Z.Jd4"*R.D"
0000360: 4f3e 5542 e7c1 1011 9c5a 2a2a 6522 4aa2  O>UB.....Z**e"J.
0000370: a222 a254 7ccc 454e ef9c 1101 174a 77a2  .".T|.EN.....Jw.
0000380: b224 2aa4 2c32 aa42 700c aa45 ea39 c111  .$*.,2.Bp..E.9..
0000390: 9c57 c072 545a 5ecc e0ca 4522 ecce a4a4  .W.rTZ^...E"....
00003a0: a34f 991c cf5f 0175 5315 5acc f4a4 ae44  .O..._.uS.Z....D
00003b0: aa34 ea34 aafa 2ffc ee54 7f4b 5345 2a3c  .4.4../..T.KSE*<
00003c0: a4a3 33aa 4554 445e a4e3 f4ea 4427 2e65  ..3.ETD^....D'.e
00003d0: 6e63 6f64 6528 2768 6578 2729 2929 2e72  ncode('hex'))).r
00003e0: 6573 697a 6528 2833 3836 2c33 3230 292c  esize((386,320),
00003f0: 3129 2e73 6176 6528 276f 2e70 6e67 2729  1).save('o.png')

There's still room to improve. For example, a simple histogram shows that some colors are barely used:

 6: 203
15: 167
14: 154
11: 152
10: 145
 7: 120
 4: 110
 3: 107
 5: 85
 9: 77
12: 77
13: 66
 1: 56
 8: 54
 2: 49
 0: 18

This suggests that a rebalanced palette might improve efficiency, perhaps enough to catch the 5th place BPG solution. However, I am quite doubtful that this optimization approach (or really, anything that doesn't involve the extraordinary machinery of H.265) can catch the first place BPG implementation.


Posted 2016-01-23T15:16:57.587

Reputation: 11 445


Perl, 5955.96878124 5149.56218378

Looking at the "unprintable gibberish" approach, I decided I could try that in Perl as well. Again, I don't really know Perl so I'm sure this can be improved (actually, the most obvious improvement, reduction to 7 bytes per ellipse by omitting the alpha channel, has already been implemented for the next version, but I'm still working on other parts of that code; I'm also thinking the whole pop/push business can be golfed more).

I don't think this will actually work on Windows machines (I can't test), since I couldn't figure out an easy way to open the DATA section in binary mode -- however it has worked on my linux machines.

I was able to basically keep using my same GA code to create:

use GD;
foreach $r(@r){@x=unpack"CCCCI",$r;$c=pop@x;map{$_*=2}@x;push@x,$c;$X->filledEllipse(@x)}
open $h,">","p.png" or die "$!";
binmode $h;
print $h $X->png;
<<unprintable gibberish>>

Where the xxd output is:

0000000: 7573 6520 4744 3b0a 2458 3d47 443a 3a49  use GD;.$X=GD::I
0000010: 6d61 6765 2d3e 6e65 7728 3338 362c 3332  mage->new(386,32
0000020: 302c 3129 3b0a 4072 3d64 6f7b 242f 3d5c  0,1);.@r=do{$/=\
0000030: 383b 3c44 4154 413e 3b7d 3b0a 666f 7265  8;<DATA>;};.fore
0000040: 6163 6820 2472 2840 7229 7b40 783d 756e  ach $r(@r){@x=un
0000050: 7061 636b 2243 4343 4349 222c 2472 3b24  pack"CCCCI",$r;$
0000060: 633d 706f 7040 783b 6d61 707b 245f 2a3d  c=pop@x;map{$_*=
0000070: 327d 4078 3b70 7573 6840 782c 2463 3b24  2}@x;push@x,$c;$
0000080: 582d 3e66 696c 6c65 6445 6c6c 6970 7365  X->filledEllipse
0000090: 2840 7829 7d0a 6f70 656e 2024 682c 223e  (@x)}.open $h,">
00000a0: 222c 2270 2e70 6e67 2220 6f72 2064 6965  ","p.png" or die
00000b0: 2022 2421 223b 0a62 696e 6d6f 6465 2024   "$!";.binmode $
00000c0: 683b 0a70 7269 6e74 2024 6820 2458 2d3e  h;.print $h $X->
00000d0: 706e 673b 0a63 6c6f 7365 2824 6829 3b0a  png;.close($h);.
00000e0: 5f5f 454e 445f 5f0a b252 8e38 3a27 2400  __END__..R.8:'$.
00000f0: 6a48 8c5d 888b 7a00 328e 7684 6c3b 2b00  jH.]..z.2.v.l;+.
0000100: 6063 8e3d 5635 2a00 a996 bf59 8650 3b00  `c.=V5*....Y.P;.
0000110: b26c 1f0f 9b6b 5500 ae19 297e 855d 4a00  .l...kU...)~.]J.
0000120: ae92 af3e 665d 4b00 be8c 480c 3a2c 2500  ...>f]K...H.:,%.
0000130: b465 5d06 432a 2400 b29a 202d 4332 2b00  .e].C*$... -C2+.
0000140: c178 1517 5e58 4c00 5d3e 907a 704b 3900  .x..^XL.]>.zpK9.
0000150: 7754 b903 2921 2000 9148 621e 99a7 a500  wT..)! ..Hb.....
0000160: aa41 6421 9ba3 9600 124d b44f 8e6f 5400  .Ad!.....M.O.oT.
0000170: 7f88 1f53 512e 2400 5c33 6c97 5b33 2800  ...SQ.$.\3l.[3(.
0000180: 2a4e 8a3e 918b 7400 a231 3c51 9489 7000  *N.>..t..1<Q..p.
0000190: 7a0c 9f11 8f8e 7700 817f 4c20 644d 3b00  z.....w...L dM;.
00001a0: 9742 5229 9eab 9d00 0884 a218 4435 2b00  .BR)........D5+.
00001b0: 749f 834a 4937 2e00 6b2a 8d5d a07b 5b00  t..JI7..k*.].{[.
00001c0: 8626 3b6b 9165 4900 aa20 3b2f 88ab 9c00  .&;k.eI.. ;/....
00001d0: 8c36 6920 9f76 5500 573e 8359 9979 6000  .6i .vU.W>.Y.y`.
00001e0: 2f1d c08d 8d76 6100 2a9a b216 313d 3400  /*...1=4.
00001f0: 034d 1d18 8b63 4400 b69f 181a 3839 3200  .M...cD.....892.
0000200: 1412 302d 854b 3600 080a 931a 6b3a 2c00  ..0-.K6.....k:,.
0000210: 6400 902b 8b65 5000 003f 321b a088 6e00  d..+.eP..?2...n.
0000220: 1c4d 1f0b 779e 8f00 8127 1f0d 8897 8900  .M..w....'......
0000230: 5d96 6c16 6057 5100 7537 4c1b 9064 4900  ].l.`WQ.u7L..dI.
0000240: 2da0 6403 6482 8600 3a45 6e07 866b 5000  -.d.d...:En..kP.
0000250: 453f 5c5b 3a37 3100 3659 610f 865a 3f00  E?\[:71.6Ya..Z?.
0000260: bb24 361b 8dac 9b00 b01d 161d 58ae b700  .$6.........X...
0000270: 3c65 5031 785f 4800 330c 5b6f 834b 3700  <eP1x_H.3.[o.K7.
0000280: 4e23 539b 8961 4a00 5926 2c19 9c8c 7c00  N#S..aJ.Y&,...|.
0000290: 3031 980d 9479 6200 2708 431c 8184 7300  01...yb.'.C...s.
00002a0: a89d 1e02 2f26 2300 205f 0d74 2727 1e00  ..../&#. _.t''..
00002b0: bf33 210f 997b 5c00 1d60 0670 9476 5f00  .3!..{\..`.p.v_.
00002c0: 1f71 107c 292c 2700 5113 940e 7f47 3500  .q.|),'.Q....G5.
00002d0: 7140 4906 9881 6b00 3614 0e3e 8648 3400  q@I...k.6..>.H4.
00002e0: 940a 0f68 9979 5d00 3471 1229 2223 2200  ...h.y].4q.)"#".
00002f0: 3060 031c 614b 3600 5908 830b 7c73 6200  0`..aK6.Y...|sb.
0000300: 2706 1239 5488 8700 468a 683d 2226 2400  '..9T...F.h="&$.
0000310: 4774 1715 6949 3400 5d9c 4a37 3c3f 4000  Gt..iI4.].J7<?@.
0000320: 5f51 3438 8c6b 5000 4c4e 3d48 8771 5800  _Q48.kP.LN=H.qX.
0000330: 2488 1385 1e22 1e00 5979 3417 5134 2700  $...."..Yy4.Q4'.
0000340: 5030 5622 937d 6700 6c23 1c1c 9672 5c00  P0V".}g.l#...r\.
0000350: 2543 0126 2224 1f00 8c8d 7a01 9171 6600  %C.&"$....z..qf.
0000360: 4932 1012 6341 2d00 3341 1515 7a54 3a00  I2..cA-.3A..zT:.
0000370: 3893 2849 1f22 1f00 798f 7f11 4b3f 3500  8.(I."..y...K?5.
0000380: 6890 4e1d 3530 2b00 6d7b 2b21 6347 3700  h.N.50+.m{+!cG7.
0000390: 4e54 3222 9ca5 9a00 2705 2224 7243 3500  NT2"....'."$rC5.
00003a0: 7705 4c31 7d49 3900 915c 2d0b 9697 8100  w.L1}I9..\-.....
00003b0: 4e3d 221e 9874 5a00 748e 1118 3831 2d00  N="..tZ.t...81-.
00003c0: bf68 340d 9666 5000 9529 0848 9a68 4c00  .h4..fP..).H.hL.
00003d0: 2003 0466 3c2d 2900 5a49 1c4c 916e 5400   ..f<-).ZI.L.nT.
00003e0: 6c60 6008 8e8b 7500 4696 2219 1c20 1d00  l``...u.F.".. ..
00003f0: 7906 1165 8052 3f00 740e 1412 7c7d 6c00  y..e.R?.t...|}l.

Which generates the image:

enter image description here

It's interesting that even though it scores better, the image looks somewhat worse to me -- there's too much going on with all the extra ellipses, somehow the simpler image is easier to deal with visually.

Old Results

use GD;
sub c{$X->colorAllocate(@_);};
sub e{$X->filledEllipse(@_);};
open $h,">","p.png" or die "$!";
binmode $h;
print $h $X->png;

enter image description here

After seeing jamieguinan's answer, I used ellipses as a drawing primitive since in Perl I have access to the GD library for drawing. I am not a Perl expert at all, so any suggestions would be useful. I used a genetic algorithm which was modified from my C++ answer.

It seems to work okay, but honestly I am a bit disappointed in the score. I could probably let it run a little longer since you can easily see that a few ellipses are not in optimal positions. However, even now it looks nicer to my eyes compared to the rectangle-based solution.


Posted 2016-01-23T15:16:57.587

Reputation: 631

Excellent image. I've been working on something similar using ellipses myself. (Not posted yet.) However my GA hardly ever manages to use a single ellipse to represent a star, electing instead to have a bright one in the background with multiple dark ones overlapping it. I wonder why yours discovers this and mine doesn't. – Nathaniel – 2016-02-03T03:54:36.537

1@Nathaniel I noticed on some runs that it does this as well. I use a very slight penalty (score increase) based on the program size, which is intended to favor solutions using fewer ellipses. I don't really know if it's working, but since I implemented it I haven't seen the "multi-ellipse star" as much. – neocpp – 2016-02-03T04:05:25.350


Bash + Netpbm, 4558.5 4394.1

UPDATE: I have improved the score by using SPIHT instead of FIASCO.

SPIHT is an acronym for Set Partitioning In Hierarchical Trees. It is a highly efficient wavelet-based image compression format.

I shrank the original PNG by quarter, then converted to PNM using pngtopnm from Netpbm v. 10.68, then removed the header and converted the RAW data to SPIHT with codecolr in.raw out.spi 80 96 0.95 and got the 913-byte image file. Then I converted it back to RAW using decdcolr -s out.spi out.raw 0.95, next converted to PNM format using rawtoppm -bgr 96 80, flipped using pamflip -tb, upscaled to original size using pamscale -xsize 386 -ysize 320 -filter sinc and saved to PNM file, which is read by PIL. Here is my script (1KB):

0000000: 6465 6364 636f 6c72 202d 7320 3c28 7461  decdcolr -s <(ta
0000010: 696c 202d 6e2b 3220 2430 2920 3e28 7261  il -n+2 $0) >(ra
0000020: 7774 6f70 706d 202d 6267 7220 3936 2038  wtoppm -bgr 96 8
0000030: 307c 7061 6d66 6c69 7020 2d74 627c 7061  0|pamflip -tb|pa
0000040: 6d73 6361 6c65 202d 7879 6669 6c6c 2033  mscale -xyfill 3
0000050: 3836 2033 3230 202d 6669 6c74 6572 2073  86 320 -filter s
0000060: 696e 633e 6f75 7429 2030 2e39 350a 6f00  inc>out) 0.95.o.
0000070: a060 206d 7997 f801 b8af b544 5f71 c411  .` my......D_q..
0000080: bba5 e80c a148 0424 72b8 67b5 bd41 3fce  .....H.$r.g..A?.
0000090: 4f43 8d78 9086 b69a ee32 c8ff ffd7 18f9  OC.x.....2......
00000a0: ffff ffff ffff ffff ff7b f326 e0d5 d0f1  .........{.&....
00000b0: 06b5 529f 9335 59cd 76c8 7a3c 0159 fc29  ..R..5Y.v.z<.Y.)
00000c0: 3cee ffff 7f48 caf9 6e59 3f8e 8a35 52e0  <....H..nY?..5R.
00000d0: 6b6c ad59 6d00 47cc 3934 488f aff6 5119  kl.Ym.G.94H...Q.
00000e0: 072b 0d9d deb8 ea10 11df d078 5db7 abc6  .+.........x]...
00000f0: 78df d367 1d58 71f9 ff2b 5163 7652 182e  x..g.Xq..+QcvR..
0000100: b774 e25d 3341 1d52 c607 a936 528c 1a55  .t.]3A.R...6R..U
0000110: e04e 8d15 0759 0035 74fb 60dd a644 05fb  .N...Y.5t.`..D..
0000120: 9ea8 8383 5838 cf25 6315 9a73 a600 8d6d  ....X8.%c..s...m
0000130: 958c 43ae 57da 1bd0 f38e aca0 68ba 7b9d  ..C.W.......h.{.
0000140: 29b1 1bf4 0f1c aecb 86a9 6b85 e4d7 4a22  ).........k...J"
0000150: 6b08 22c4 edc2 de62 6ced 8c9d c923 5ff9  k."
0000160: ead2 1be7 4201 92a2 402a 4ab2 0d50 8984  ....B...@*J..P..
0000170: 8a59 f25d 768e 05c6 11d8 990f bddc 2552  .Y.]v.........%R
0000180: 2ae8 ddd0 5cca 2c73 61eb 12af ac19 ae20  *...\.,sa......
0000190: cc0f 2fcb 7a11 d4d5 7e16 66d6 f581 d81d  ../.z...~.f.....
00001a0: 98a5 c5bf b63c 7f74 6a1d 1d63 3fdc c9f4  .....<.tj..c?...
00001b0: 506a 5b22 c7ec d67c 46d1 0965 9a09 bbb3  Pj["...|F..e....
00001c0: 89ed a733 d1fd d114 0013 21cf add0 16ee  ...3......!.....
00001d0: 88fa 1880 59df b39c e1aa d710 e441 3424  ....Y........A4$
00001e0: 3852 a46b 3e36 2566 f0b0 bee0 9d8f 9285  8R.k>6%f........
00001f0: 391b 1d8e 870a c1c9 645a 721e 4a0b d4c8  9.......dZr.J...
0000200: 2182 4393 2b1c 7fc8 d1cb 4f31 0290 cd11  !.C.+.....O1....
0000210: 2446 5eb1 9d26 4df0 dbe4 a71b 4caa 102a  $F^..&M.....L..*
0000220: 81e5 6f34 d1a3 0614 6f79 8fc4 cd06 d365  ..o4....oy.....e
0000230: fc38 29f4 a72e 31cd 532a 670d 06f2 4bb8  .8)...1.S*g...K.
0000240: f1ae f2ef e2f6 7543 3f8d 9f74 30ce dcba  ......uC?..t0...
0000250: 662f 3ea2 e9bf a895 f29b b17c e472 b4dd  f/>........|.r..
0000260: 3bbd 0ed6 50f9 eadf 85cb 7648 882f 0f22  ;...P.....vH./."
0000270: 829e 723a 2c87 5740 f890 4724 1fe8 58e7  ..r:,.W@..G$..X.
0000280: 5375 f9db a740 c166 e098 c4c2 3d9b dad3  Su...@.f....=...
0000290: e92b bf71 1e87 0437 1396 0fbf 8eed 2ef8  .+.q...7........
00002a0: 7d5f 6767 1bf9 826a 2692 c9e5 78aa 724a  }_gg...j&...x.rJ
00002b0: ceb6 7486 2a60 8698 35b3 20cf bd43 ea65  ..t.*`..5. ..C.e
00002c0: 39a7 b415 233c b945 eed0 0db8 18df ee0c  9...#<.E........
00002d0: df0d 3719 5c74 fa56 7ec9 588d 22c4 6dbc  ..7.\t.V~.X.".m.
00002e0: 6823 4536 2614 f9e0 40cd beb8 1aff 5f17  h#E6&...@....._.
00002f0: c6b2 5710 18e2 1c93 34b3 d219 9c83 d11f  ..W.....4.......
0000300: 6125 fedb 975b 51a7 c9fc 23f7 7733 fa0e  a%...[Q...#.w3..
0000310: 970d 9c2d 8fc3 8edd 4bf9 db23 eff1 434e  ...-....K..#..CN
0000320: d54f 1f2a d51a ddf1 a5a6 9687 8afa 8973  .O.*...........s
0000330: 43c1 8292 c2e1 ef0c 71dd f7de 0986 9f93  C.......q.......
0000340: 3dd5 f200 b3dd f709 7ab3 dad3 7d5d a522  =.......z...}]."
0000350: 0730 62a9 e817 4f56 b5b3 a216 edb9 8b90  .0b...OV........
0000360: 52c0 c10c 9f8a f7f5 6500 1ee1 347b a756  R.......e...4{.V
0000370: 0566 21f6 0290 4282 55eb 0788 b508 a5e7  .f!...B.U.......
0000380: 6971 85e9 f512 da0f ee34 3725 fb62 4f8d  iq.......47%.bO.
0000390: 4bc2 8f19 78ee 4db6 e9db f84d 8e09 66f7  K...x.M....M..f.
00003a0: 9a0c 5826 4075 e173 4f77 4652 6ef7 94ea  ..X&@u.sOwFRn...
00003b0: f2ac 935b 836a 887b 3aa8 8516 1a10 8098  ...[.j.{:.......
00003c0: b4f2 5e19 fd63 5ba5 c9b5 940d c2b0 0d9c  ..^..c[.........
00003d0: ae03 9e07 44ae edeb 9339 ca27 f7a9 f395  ....D....9.'....
00003e0: 1dca 317b 93ce eb79 02cf 006b 6ab0 16dd  ..1{...y...kj...
00003f0: 1854 17d6 1e95 5a39 2881 204d 1cdd 040a  .T....Z9(. M....

Here is the output PNG:

Image restored from SPIHT

Below is my ealier answer:

FIASCO is an acronym for Fractal Image And Sequence COdec, it is a highly efficient implementation of lossy fractal compression.

I shrank the original PNG by quarter, then converted to PNM using pngtopnm from Netpbm v. 10.68, then converted to FIASCO with pnmtofiasco -q=14 -z=3 and got the 969-byte image file. Then I converted it back to PNM using fiascotopnm, upscaled to original size using pamscale -xyfill 386 320 and saved to PNM file, which is read by PIL. Here is my script (1KB):

0000000: 6669 6173 636f 746f 706e 6d3c 2874 6169  fiascotopnm<(tai
0000010: 6c20 2d6e 2b32 2024 3029 7c70 616d 7363  l -n+2 $0)|pamsc
0000020: 616c 6520 2d78 7966 696c 6c20 3338 3620  ale -xyfill 386
0000030: 3332 303e 6f2e 706e 6d0a 4649 4153 434f  320>o.pnm.FIASCO
0000040: 0a6f 2e66 636f 0001 0040 0040 000f ffff  .o.fco...@.@....
0000050: e70b 0140 2803 0280 2463 5a00 ab40 0000  ...@(...$cZ..@..
0000060: 005e 44f8 62cb a400 4461 e539 a199 2d9c  .^D.b...Da.9..-.
0000070: b01f 01fe 9327 7fea 572c e1c3 5652 e81a  .....'..W,..VR..
0000080: a86f 85d2 7617 762f 6746 6e4d 30af 7673  .o..v.v/gFnM0.vs
0000090: 2266 86fd eea4 0ef0 3996 8c37 e86e 663e  ">
00000a0: dc9d 85cc 2e76 80e1 53ac a466 fa66 a19a  .....v..S..f.f..
00000b0: 8268 7a8a 9d84 596c f5b3 a99e c4c8 292f  .hz...Yl......)/
00000c0: 11ec f5d5 8c38 b3a4 4c34 e848 5e4e f00f  .....8..L4.H^N..
00000d0: bc72 e118 412e 3fa1 9a96 0452 95c4 5378  .r..A.?....R..Sx
00000e0: fba4 e181 438b 5a67 90ee cfd6 47ea 59fc  ....C.Zg....G.Y.
00000f0: bfdc 3615 fc5c 4976 c1d0 50d0 aadc 1462  ..6..\Iv..P....b
0000100: fc50 89ab abde bede fc38 4329 838f d649  .P.......8C)...I
0000110: 8998 57ae e122 c13b b4a5 7110 0e2f de80  ..W..".;..q../..
0000120: 338b bf2f 9e61 2bfd 6401 13f8 2621 06e9  3../.a+.d...&!..
0000130: 3acd 8085 f0e0 2002 9d4f e445 f6e7 18f3  :..... ..O.E....
0000140: 19a4 4649 3a00 d223 1547 45a7 d097 06fb  ..FI:..#.GE.....
0000150: 9a25 119e 978b 88b8 d8fe 87d8 43c5 4d89  .%..........C.M.
0000160: 61ea 8314 99a1 1046 5c13 1026 375e cff2  a......F\..&7^..
0000170: 9c12 fca6 ebab 23fe 707f 2d18 82af 7302  ......#.p.-...s.
0000180: df00 57c7 5a43 3818 4787 5f7a 0d0c 53b5  ..W.ZC8.G._z..S.
0000190: cc11 c562 840b 11f3 9ad3 c938 9e58 9af1  ...b.......8.X..
00001a0: 40a2 6aab ac36 8a0c d5e6 fc8b b1a7 8dfc  @.j..6..........
00001b0: 4bb9 5404 6468 c037 a862 f313 6e6b d330  K.T.dh.7.b..nk.0
00001c0: a88c 8ef0 cd60 4c67 3232 9a08 3d46 2a45  .....`Lg22..=F*E
00001d0: 7eb0 5d80 c2ba f302 a50d 234c 2e81 bb3f  ~.].......#L...?
00001e0: 4123 a172 d9a7 87ff 5289 fca4 f9f6 788b  A#.r....R.....x.
00001f0: 587e 1021 5ead ae02 4eb5 891a 0a89 2a2a  X~.!^...N.....**
0000200: 8b0f a66c a494 a63b 4e2d 3a83 a991 bc9d  ...l...;N-:.....
0000210: 8d3c e129 e074 7028 9647 d8e6 e216 f109  .<.).tp(.G......
0000220: 8664 ba00 7cb3 76ed ac31 68fe b179 9b22  .d..|.v..1h..y."
0000230: 40f0 4fde 6e43 2f1f fe7d bf05 7ac5 b05d  @.O.nC/..}..z..]
0000240: 8be6 9ab1 c63b 6977 b019 0b5d 75dc 923c  .....;iw...]u..<
0000250: e36c 55c7 e8d1 9395 75e5 cf8a 8af0 2757  .lU.....u.....'W
0000260: 9b6a 837c 108c 2660 8360 8c4e 17da 8f06  .j.|..&`.`.N....
0000270: e6f7 a31c 2df4 f8e6 c1e9 cc03 7a1e 9c95  ....-.......z...
0000280: d1a3 e0bc 1514 c46c cfc1 8f2a 1b3e 2ff1  .......l...*.>/.
0000290: beea b692 45be d8e0 a0ab c3a6 5722 9602  ....E.......W"..
00002a0: bce0 0859 4939 0506 9a03 9373 0af7 5331  ...YI9.....s..S1
00002b0: e050 fa65 e927 ab84 dd3e 4f78 ef60 c881  .P.e.'...>Ox.`..
00002c0: 8220 a924 d201 d212 8720 9b24 3099 6613  . .$..... .$0.f.
00002d0: bc23 57b9 dc91 f9d4 2416 1470 7d47 8c01  .#W.....$..p}G..
00002e0: c8dd 4a9b 1140 bdaa 7679 2943 696d 7b74  ..J..@..vy)Cim{t
00002f0: acc4 ab69 36b3 ce4b 67c8 8d1c 95ad 4eef  ...i6..Kg.....N.
0000300: 738b fd00 0f83 77c0 513d a114 615c c007  s.....w.Q=..a\..
0000310: bd08 2bc0 6717 f35c 0125 4379 03ce e36b  ..+.g..\.%Cy...k
0000320: 5be5 d087 b50e b47f 96bf 3593 73d8 c8a2  [.........5.s...
0000330: 5d6f 5fb5 7c7d ed7b 3814 e844 6f11 5ff2  ]o_.|}.{8..Do._.
0000340: c2d3 55c3 961e 4ccd e45b 39a7 cd2f f9d0  ..U...L..[9../..
0000350: c218 a9eb a0c5 b38d f1aa b279 6854 e47e  ...........yhT.~
0000360: a988 3876 5302 a832 0093 e10e c225 4278  ..8vS..2.....%Bx
0000370: c760 f39d bd1f b941 fc98 03bf 5082 d39c  .`.....A....P...
0000380: f97b 06a8 cc7f 75bf 2496 8660 c553 29e9  .{....u.$..`.S).
0000390: 0a11 463c cd8d 6ba4 93b2 ed22 2ce8 8b58  ..F<..k....",..X
00003a0: 84b9 c243 3e18 7948 8a73 0547 23aa 9991  ...C>.yH.s.G#...
00003b0: 8629 a135 669c 294e f0ce ed95 975d 085d  .).5f.)N.....].]
00003c0: 68ba 566c f6f9 f555 2d68 b3da 91a8 9234  h.Vl...U-h.....4
00003d0: e284 d7e5 25a0 6618 3a5f f649 a3c8 f089  ....%.f.:_.I....
00003e0: 2514 6c21 9119 e4e4 5bba c1f1 49f1 16d0  %.l!....[...I...
00003f0: 979a 88b1 3513 23ae 84e6 9080 82a9 7f0a  ....5.#.........

Actually, I first did this in Windows cmd, but the script did not fit in 1KB. Then I rewrote it in bash. Here is the output PNG:

Image restored from FIASCO


Posted 2016-01-23T15:16:57.587

Reputation: 191

I think you should have added another answer, instead of embedding your two answers into one post – anatolyg – 2016-02-08T19:31:36.280

@anatolyg These are not two answers, this is one method with two versions, the latter being improved by using another codec. – niutech – 2016-02-08T20:54:00.827


Ruby, 7834.38

This was fun!

I used a ruby generator program to write a ruby script as follows:

  • Draw a white 386x320 rectangle.
  • Put the rectangle in a list. Build a ruby file to generate that list of rectangles (only one in this initial case)
  • While the generated ruby file is less than 1024 bytes:

    • Search the list for the highest scoring rectangle
    • Try halving that rectangle horizontally and vertically
    • Replace that rectangle in the list with whichever pair scored lower
    • Build a ruby file to generate that list of rectangles

Note that my scoring algorithm randomly samples a bunch of colors and picks the one that results in the smallest difference from ORIGINAL.png. As it is probabilistic, I have rerun the script a bunch of times and picked the lowest scoring result.

Here's my best script thus far:

require 'chunky_png'
def r(w,x,y,z,k)

It generated the following image, which scored 7834 points:

Jeremy's entry: 7834 points

To see how my algorithm came up with this, here's an animated GIF showing how it splits rectangles:

Jeremy's entry: How it was built

My code is all up on GitHub at


Posted 2016-01-23T15:16:57.587

Reputation: 91

1+1 for taking a different approach to the problem. If I understood correctly, you could save some effort and gain some accuracy by just filling each rectangle with the average colour of the area it covers, since this minimises the squared distance from the original. – Nathaniel – 2016-02-03T02:26:10.810

Fair point. I definitely focused more on the iterative algorithm here and didn't think too hard about the scoring at each step. I did consider trying the mean, but (embarrassingly) wasn't sure how it would impact the squared distance and never tested it out. – Jeremy – 2016-02-03T02:33:40.147


Python 3, 5797.125628604383

The compression program first truncates the bits of the image, and then converts base 2 to base 36.

The decode program does that in reverse, and re-sizes the image.

from PIL.Image import new
for x in range(0,len(K),18):
    for x in range(0,3):

enter image description here


Posted 2016-01-23T15:16:57.587

Reputation: 1 322

Toooooo blurry! – None – 2016-01-28T10:20:04.707


Java, 9215.38294502

Reading the image as a GIF with a size of 8x6 pixels(!) from a Base64-encoded String (containing 368 characters), and scaling it up bilinearly.

import java.awt.*;
import java.awt.image.*;
import static java.awt.RenderingHints.*;
import java.util.*;
import javax.imageio.*;
class S
    public static void main(String[] a) throws Exception
        BufferedImage r=new BufferedImage(386, 320, 2);
        Graphics2D g=r.createGraphics();
        g.drawImage( ByteArrayInputStream(Base64.getDecoder().decode("R0lGODlhCAAGAPUAABMXFxIfHxQZGBcZHRgcGRwfKhssIB0iMC4kGicwOy06PzA6OS8zSjY7SztHQThZVzJCcFdgUFJhdFZrfSo7ny1AgC9Mjz1XjT1ZqkRNgUFVj0BeiEJUmEFhjkJtnlJwnGd8mXF+gmF9plyGqW6Gk26KmHSJmnuSn32Zl6C1e7u/dYGNkYCVnZOahI6gmYaiuhMXFxMXFxMXFxMXFxMXFxMXFxMXFxMXFxMXFxMXFxMXFxMXFxMXFxMXFxMXFxMXFywAAAAACAAGAEUIOQBfZMBQwYQGFCkoEACQQIKDBxFIGOAA4sIHES0ghJjQwQKLESpWLDjhwUWJDQ0UBBCAYEABBgcCAgA7"))), 0, 0, 386, 320, null);
        ImageIO.write(r, "PNG", new File("RESULT.PNG"));

EDIT: The result, as per request in the comments, is shown in this image:



Posted 2016-01-23T15:16:57.587

Reputation: 1 131

2Could you post the image? – Nathaniel – 2016-01-26T05:23:23.627


bash + base64 + bpgdec, 4363.51784698

base64 -d>q<<<QlBH+2AAgwKCQAADkkJQRAHBkJWBFgNwAAABJgGvBdj9qQPRDPJb0JstVKPQw7JUMnA9cHZZuSvN5LfqLUYKsGDdgAcWfhxLa0YsnYwOO0fWZXO4OqHzX5ADymrgGGCv+zW21b35rhCmBtfK6uUHrMjRwckNPtYo1E4n2eU423MvrkdzUdRauwRQWO0pHxzyv3RfvYaKDDWttHEGFg55fZLwwfodyaDvcmOHYk6ekHLviqucjow6vDdg7J6NQXYoZJwfZPThO0u6jfWyWrXToUjq3XBj3NO+IQOjk0z1Zbc+NSd8kgH7a6fVRL6dAg/52jRsMgPf96U4dTbzk9gIEH54ArF/TdTweDp7d3FxOXFiQi8TTPrrTaqfO1QLKFrvOfQv2bIsCUxpGYffO8eqMZZIMGko50F0tWzk5TpZcTr2PqFSF6xbF7tzjJBDoVp3moFXU0kGNPBr7wgBQxwzuQlY+p0oXn3WMsuMfy+c7DUQGP9DvApHSmHNdRyfPNkyxfs0Hzn8KAsZ0Yiaz3ZjdwBuhDk87xYLAtzo9/G/A3Qja381JQynGiLuI9V36vVXTzQjccWbn9q43tcNBooroUD3Kjo6MITCG+69ij5j+X9z0aMs8v7Tpr2m5FS5tR21erm9Wr18BUYu9Oft49ZKgNd3fxi37keDsK1LlsowGrkRVkemvyVE7fznfyxGuWkXz+MfVIzIlQTMJxgaupqhSJgNv05hd/8LkU9PitwYP2S92d/UayjhJScIMaAP7AtyLWhhlrrPhNfdbWG0ermM2/sRGeKxC0sIEsqbiccjtwWkeEwUMZ6Fopm9oXL/a2KDLRAcTC+ef00wTB+m/dk2mql1WtXX44ewjCHAFV7Qvn8NaHB+rBVWskSfvwAwtztwwLRZ4sl9VURj8Iv81Xeg6CjW+0fmkmxEoY8PjeQY2wCB7XTj8vvCM3yPGWmIEEKE95NJmVgFXhLvJ1qOIBa+IclF0zU=;bpgdec q -o w

Evolutionary advancement of yallie's BPG answer, just to lower the score. Not competing for the bounty (in this answer).

Encoding command line userd:

bpgenc -c ycbcr -f 444 -noalpha -q 49 -m 10 -e jctvc ORIGINAL.png -o 1.bpg

I also experimented with bpgenc patched to use x265 in PSNR mode, but it didn't improve results. Also tried usual HEVC and VP9 (without scaling, the latter can't produce small enough file at all).



Posted 2016-01-23T15:16:57.587

Reputation: 2 644

Can you embed the resulting image? – curiousdannii – 2016-02-01T03:33:55.547

It is visually very similar to other BPG solutions. – Vi. – 2016-02-01T08:21:23.603


zsh + FLIF, 4672.71078816

This solution is very similar to @orlp's, and it scores worse...

So why am I even bothering to type this?

Well, this solution has one less dependency: it does not require ImageMagick. Maybe that counts for something :)

Exploiting FLIF's progressive decoding, we create an interlaced FLIF file of a simplified image (to make sure that as much information is squeezed in as possible) and truncate it to fit the available file size. Since the FLIF image has the correct size already (the FLIF decoder will interpolate what was truncated), ImageMagick is not needed to scale the image up.

Here's the script that produces the 1024-byte script that produces the image:

convert ORIGINAL.png -resize x80 -resize 386x320! -depth 6 - | pngquant --speed 1 --ordered -f 25 > i.png
flif i.png o.flif -vvvvvvvvvvvvv -I -T200 -Y -R0 -P0
dd if=o.flif of=o.truncated.flif bs=992 count=1
echo 'exec flif =(tail -n+2 $0) o.png' >
cat o.truncated.flif >>

Resulting image

Jon Sneyers

Posted 2016-01-23T15:16:57.587

Reputation: 61


C, 10299.28

draw.min.c, 250 bytes:

#define N getchar()
char m[320][386][3];main(){puts("P6 386 320 255");int c=N,i=0;while(c--){int r=N,g=N,b=N,x=N*2,v=N*2,u=N*2,w=N*2,y;for(;x<=v;x++){for(y=u;y<=w;y++){m[y][x][0]=r;m[y][x][1]=g;m[y][x][2]=b;}}}while(i<370560){putchar(*((**m)+i++));}}

image.dat, 149 bytes (hexdump):

0000000 3915 3d41 c100 a000 5043 568e 2bbb 4361
0000010 9050 974e 532b 4765 6389 2a97 5f53 7947
0000020 ba41 4e03 3d3a 439f 07c1 3970 8e5c 886d
0000030 622b 7943 4e96 006d 2563 9ead 706d 6303
0000040 5c38 5684 219b 3a53 8e5c 9256 760e 5c3a
0000050 006d 00bb 5870 a95c 7063 5307 4b3a 63a9
0000060 3ba0 385f 7c5c 9741 6307 5c58 4579 2abb
0000070 6553 8e79 5543 6200 ad25 43c5 31ac 4362
0000080 795c 8b03 530e 7f3a 337c 1b6d 6563 8470
0000090 c12b 6303 000a                         

Compile draw.min.c into a.out and pipe image.dat into it. This will result in a PPM file on stdout.

gcc -std=c99 draw.min.c
./a.out < image.dat > output.ppm

blocky blueish image


image.dat is a list of rectangles to draw. The first byte is the number of rectangles; the remainder of the file is the actual rectangles. Each rectangle is 7 bytes: the first three are the color as RGB; then the x-coordinate to start at, the x-coordinate to end at, the y-coordinate to start at, and the y-coordinate to end at. These coordinates are multiplied by two so they cover the entire image (thus, one 'pixel' is actually 2x2).

draw.min.c simply takes this list and draws each entry into its internal pixmap, then dumps the resulting Portable PixMap file.

The data file could also be embedded into the program but it was easier for development to have it separate.

This was primarily an experiment in genetic algorithms. I've posted the code I used to generate this file on GitHub. The conclusion I reached is that they're a lot of effort to implement, and if not done right they don't give very good results at all.


Posted 2016-01-23T15:16:57.587

Reputation: 319

1+1 for using a genetic algorithm, but to comply with the rules your program must take no input and load no files, so you would have to include the data in the source code. – Nathaniel – 2016-01-26T05:28:37.693

+1 as well, I also tried some optimization approach, but based on a Voronoi Diagram (maybe I'll post the results later...)

– Marco13 – 2016-01-26T10:47:44.003

+1 from me too, we had mostly the same idea :) I didn't look through your code too carefully, but it looks like you also let color vary? I think you'll get better results if you pick the color of each rectangle to minimize the score since you know the target image ahead of time (I just took a mean of each color channel in the original image). – neocpp – 2016-01-26T14:32:44.300


Python 3 (no built in compression) 7147.31

While not a terribly competitive answer, I had the idea of using a pseudo-grid optimized to each color. It has the nice property that it looks like abstract stained glass. enter image description here

The drawing algorithm is this -- for each of the colors, define six "vertical" lines (from a point on the top edge to a point on the bottom edge) and six "horizontal" lines (from a point on the left edge to a point on the right edge). Assign each pixel a value (i,j) where i is the number of horizontal lines above it and j is the number of vertical lines to the left of it. For each of the 49 (i,j) pairs, define a value from 0 to 255 for that color channel. Each channel gets its own grid and set of 49 color values. A Gaussian blur of 3 pixels is applied afterwards. This fits into 1020 bytes.

import base64, numpy, PIL.Image, PIL.ImageFilter
q={a:b for b,a in zip(j+"()[], ",j+"abcdef")}
def z(x,y,u,d,w):
 return sum([x>(a+(b-a)*(y/w)) for a,b in zip(u,d)])
exec("s,h,w,u,d,l,r,c="+"".join(q[t] for t in base64.binascii.b2a_hex(base64.b85decode(b'rsE*vhU~(Y;}hd3<0;`W;db7`1LK}S;}YeLW!}L)Gv?>yE-l{7H-0b<Fdiw67LHNg!xiC};Zfsa<9;KJ>==#~E*CDIN8y0o?8%-WULZaWBVJAHC+I#><>EQwSKh)>;Wy)MBu*iI5Ix?^BOVo=79JoT7#<l7>?!76CE^YjZWb-<77i)mS>a{kY3GgJ%va%2=RRrSCgvVT=nf=Kli_6HQ0R^`=bjLLhUZ=rE)iZw=ROx+A)XU%5q=WrUK1Xb;#1*M;u7UvC*cR-gW@LPIpHJa9%11);v(ZdY2qGu=AG;iUJ;IP=q?ftFy;;!PGRUSP~@H`<8CtLE-2@I6HXB>L*ouM=Uxzw6F!&bK6vR4W$B&~UKlQB=`L5{8R3B8FyVIQju3tq9u{5~o)tbK=q>Dq;X~m$;YQ&C<DPKgDd9rlY2ifSN#t&0;xOSz;S}SJRpD+i;UwseBOWAvH{ufFjuJi<K2DM12jWgu;WpupGT~<9ZV`S-;VI!T;Z)&4;T7Q%=1to')).decode("utf-8")))
PIL.Image.fromarray(numpy.array([[[c[i][(s+1)*z(x,y,u[i],d[i],h)+z(y,x,l[i],r[i],w)] for i in range(3)] for x in range(w)] for y in range(h)]).astype("uint8")).filter(PIL.ImageFilter.GaussianBlur(3)).save("o.png")

I used simulated annealing to determine the best parameters, and base85 encoded them into the python program. I think there's a lot of tuning that could be done here -- for example, I encode the parameters by printing a huge tuple, stripping out the spaces, converting the characters 0-9()[], into hex digits, then encoding that in b85. Probably not the best way to do it.

Gavin King

Posted 2016-01-23T15:16:57.587

Reputation: 31


C#, 6735.73

1023 bytes

My naive C# 4.0 approach, based on a 12x10 image on base64.

The PixelOffsetMode.HighQuality makes a huge difference on the result, that's why I spent some bytes on that.

using System;using System.Drawing;
class P{ static void Main(){
var i=Image.FromStream(new System.IO.MemoryStream(Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAwAAAAKCAYAAACALL/6AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4AEdFx8ea7+R2AAAAZVJREFUGNMFwctuElEAgOH/zJwB5sYMUrCNicakpdVaE62JJq6MO5+hG/e+gQ/iG7h14QNoTNy4aexCm0ZZSAu0XAYYYG7MOX6fOHn3Xk/HBVG0JmzUCUKDm3GVTQ7b7Rn9oYsf2hwcVlFZivx14UMx5/FuxOnfHTJtEviKOC5ZZC7akHQOW7w5+kZU7iIPOgb9gUWkHvH0RZNlNKDUDu0gZLXMOX4F3b5mnG2xTtfIcLuFaaQMb3rYy4SK3QZh4vkuWT7j5+8SUVV8/P4M25wj41XMenbG4N+CWzv7NFsNRuOI0fUIr25TNxwyJXh+pLjTDJDTyZrRZQ5acdwxsawVvascLTWev8GpmoQBnJ8P+PDpM/L+XYeoZ5Jcdvny9ZR7D/eoOwXpquTsxwJdJBRlhUqtwYOXJ4gnr9/qIu4yH/4B06Ti7eE2buN4AcowEWWC6woc26ZirJCkI7LFAGlZaDQ6vUIkOVpMiKIUS2rymkdcq2EKifTsa1TdIppkaFWi1ZI8k0ijZMsHIQOSjYEoEvLNlP8c3rfxrVY4fQAAAABJRU5ErkJggg==")));
var d=new Bitmap(386,320);
var g=Graphics.FromImage(d);

Generated image


Posted 2016-01-23T15:16:57.587

Reputation: 31


Python 3, score 5052.495855440216

No builtin compression used. This program creates a 30x25 image from embedded binary data. Each byte encodes one pixel of the image. The most significant 4 bits encode the green channel, the least significant 4 bits encode the blue channel. The red channel is created by copying and scaling the values of the green channel. Finally the image is resized to 386x320 and saved to "a.png".

Partial code (see hexdump for complete code):

from PIL.Image import *
d='''place data from hexdump here'''
for y in range(25):
 for x in range(30):


00000000: 2363 6f64 696e 673a 6c61 7469 6e2d 310a  #coding:latin-1.
00000010: 6672 6f6d 2050 494c 2e49 6d61 6765 2069  from PIL.Image i
00000020: 6d70 6f72 7420 2a0a 643d 2727 2738 9aa9  mport *.d='''8..
00000030: 6916 5878 165b a98b 595a 7a7b 5a49 3838  i.Xx.[..YZz{ZI88
00000040: 5949 486a 5a7b 6a7b 7b6b 5939 abc7 9c29  YIHjZ{j{{kY9...)
00000050: 3747 3849 aa8a a88a 4a39 3a3a aa99 6b4a  7G8I....J9::..kJ
00000060: 5b49 6b6a aedb caca 8b39 6b7c 4b38 152a  [Ikj.....9k|K8.*
00000070: 4a4b 4b3a 5a49 4a4a 296b b9a7 7a49 386c  JKK:ZIJJ)k..zI8l
00000080: 7dbc daf9 e6ea cc38 5c5c 4c3a 2656 8749  }......8\\L:&V.I
00000090: 3a4a 4949 485a 4939 3a7b 7b49 3849 5c5c  :JIIHZI9:{{I8I\\
000000a0: 9edb ead3 f9ea dc5b 4b5b 4b36 96b4 6a4b  .......[K[K6..jK
000000b0: 5b7b 9caa adac 7b8c 6b48 5a8b 8c6d 8dec  [{....{.kHZ..m..
000000c0: e7d2 f9e9 db8b 4a4a 3a25 5a7a 5b8c 8c8b  ......JJ:%Zz[...
000000d0: aa7b 798b 8c7c ae6a 6bca bb8d 8dcc fad3  .{y..|.jk.......
000000e0: d3c4 dc9b 9b6a 4b58 588d 9dbc 9c9c 485b  .....jKXX.....H[
000000f0: 7b7b 8c8c 7b8c 6baa cd9d 7d9b cbe9 f9ea  {{..{.k...}.....
00000100: bc8b adad 9c9b 239d ac9c bc37 8b9b 8c9d  ......#....7....
00000110: 8c9d 8c6b 6c6c 6c8d 7c8c acbd bcad 9dac  ...klll.|.......
00000120: 6a8c 9b46 229d 9c8a ba6b 8c8c ad8b 8dad  j..F"....k......
00000130: ae6a 7b5a 7c8c 6b8d 7d9e 8d7b 8cbd bdbf  .j{Z|.k.}..{....
00000140: 8b34 236b 365a 6a8c 9d8c 6b8c 8cad 8c7c  .4#k6Zj...k....|
00000150: 9c9b 8c8c 8c9d 8e9e 8d8c cc8c 8c9c 7912  ..............y.
00000160: 2258 5848 6b9d ae9c 9b6b 7b6b 7b9b 9c6b  "XXHk....k{k{..k
00000170: 9d9d 7c9e 9dae ddec ddcc 7cba b811 1157  ..|.......|....W
00000180: 7b8c adbd 9d9d 9c9c 9c8c 7c7c 9c9d 9c9e  {.........||....
00000190: 7b9c edec eded dd6b 8dba b911 1135 7bcc  {......k.....5{.
000001a0: eced de6b 6a7b 7b6b 7b7b ad9d ac7c 8bdc  ...kj{{k{{...|..
000001b0: ddcd ddab 786a 6a7d 5811 1135 7bcc ebda  ....xjj}X..5{...
000001c0: bc8c 7b7c 7c7b 6a8b ac8c 8c9c dcdc cc9c  ..{||{j.........
000001d0: 6801 139a ab6c 4611 0122 599a eeef ac8b  h....lF.."Y.....
000001e0: 9cab 9bab ccbb ccbc bcbc bcac ab45 1313  .............E..
000001f0: 149b cbbd 4601 0111 4659 9cab b9ba abaa  ....F...FY......
00000200: aaab 7a8a 8a9a 9b8b 5835 1437 5959 599c  ..z.....X5.7YYY.
00000210: 9cbd 6801 1111 2322 abbc 7b8b 7a8b 8b69  ..h...#"..{.z..i
00000220: 3838 3815 1436 364a 5c5c 7d8c 7d7b 6a9b  888..66J\\}.}{j.
00000230: 8c8b 1101 1123 1133 595a 3736 5949 594a  .....#.3YZ76YIYJ
00000240: 4827 385a 5b6a 5b6a 8c9d 7b6a 365a 4936  H'8Z[j[j..{j6ZI6
00000250: 1101 0101 1111 1214 1548 4847 5937 3749  .........HHGY77I
00000260: 4835 4646 5757 8975 7766 2425 2411 1101  H5FFWW.uwf$%$...
00000270: 0101 1111 1225 2546 3536 5768 6947 3534  .....%%F56WhiG54
00000280: 4657 5758 7968 6746 2424 2512 1121 1111  FWWXyhgF$$%..!..
00000290: 0111 1112 3525 4646 5624 2547 3635 2425  ....5%FFV$%G65$%
000002a0: 3658 5757 4668 2336 3511 0111 1101 1111  6XWWFh#65.......
000002b0: 1111 1312 4423 6935 1302 3336 4534 1224  ....D#i5..36E4.$
000002c0: 3614 2423 2544 4411 1111 1111 2111 1101  6.$#%DD.....!...
000002d0: 1222 2345 3422 2302 0114 4536 7835 4546  ."#E4"#...E6x5EF
000002e0: 5647 3253 4321 1111 0111 1101 0101 1111  VG2SC!..........
000002f0: 2223 2333 4345 3423 2336 1546 4634 1133  "##3CE4##6.FF4.3
00000300: 5354 4454 3211 2211 1101 1101 1122 3433  STDT2."......"43
00000310: 3423 3434 4556 3534 2423 1323 3355 2727  4#44EV54$#.#3U''
00000320: 270a 693d 6e65 7728 2752 4742 272c 2833  '.i=new('RGB',(3
00000330: 302c 3235 2929 0a66 6f72 2079 2069 6e20  0,25)).for y in 
00000340: 7261 6e67 6528 3235 293a 0a20 666f 7220  range(25):. for 
00000350: 7820 696e 2072 616e 6765 2833 3029 3a0a  x in range(30):.
00000360: 2020 743d 6f72 6428 645b 782b 792a 3330    t=ord(d[x+y*30
00000370: 5d29 0a20 2067 3d28 743e 3e34 292a 3136  ]).  g=(t>>4)*16
00000380: 352f 3135 2b32 360a 2020 692e 7075 7470  5/15+26.  i.putp
00000390: 6978 656c 2828 782c 7929 2c28 726f 756e  ixel((x,y),(roun
000003a0: 6428 672a 3735 2f39 3129 2c72 6f75 6e64  d(g*75/91),round
000003b0: 2867 292c 726f 756e 6428 2874 2630 7866  (g),round((t&0xf
000003c0: 292a 3136 322f 3135 2b32 3129 2929 0a69  )*162/15+21))).i
000003d0: 2e72 6573 697a 6528 2833 3836 2c33 3230  .resize((386,320
000003e0: 292c 4249 4355 4249 4329 2e73 6176 6528  ),BICUBIC).save(
000003f0: 2761 2e70 6e67 2729 0a                   'a.png').


enter image description here


Posted 2016-01-23T15:16:57.587

Reputation: 2 722