259
\$\begingroup\$

Note: a 1000 point bounty is still available for this challenge, which I will create and award if anyone 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.

Scoring

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 validate.py myImage.png.

from PIL import Image
import sys

orig = Image.open("ORIGINAL.png")
img  = Image.open(sys.argv[1])

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

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

print(score/255.**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.

Leaderboard

var QUESTION_ID=69930,OVERRIDE_USER=21034;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+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}}",e.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,link:e.link}});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}}",o.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="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <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>

\$\endgroup\$
37
  • 5
    \$\begingroup\$ On 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. \$\endgroup\$
    – user42643
    Commented Jan 23, 2016 at 16:17
  • 2
    \$\begingroup\$ @tepples other than going in the opposite direction and not being logarithmic, yes :) \$\endgroup\$
    – hobbs
    Commented Jan 24, 2016 at 16:11
  • 6
    \$\begingroup\$ I'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. \$\endgroup\$ Commented Jan 28, 2016 at 10:16
  • 3
    \$\begingroup\$ @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. \$\endgroup\$
    – N. Virgo
    Commented Jan 28, 2016 at 10:29
  • 3
    \$\begingroup\$ @Nathaniel hasn't been said enough, but this question is so awesome! ;-) \$\endgroup\$ Commented Jan 28, 2016 at 10:33

38 Answers 38

144
+250
\$\begingroup\$

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

output

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  [email protected]...?...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  Q........ca...].
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  &.\.'.qN.td.....
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

KjC"…"93
  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

Training

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

WEIGHTS = idx(NEURONS)
SCALE = SCALE_DIV / np.log(SCALE_BASE)
W_MIN, W_MAX = -MID, BASE - 1 - MID

sess = tf.Session()

with open('ORIGINAL.png', 'rb') as f:
    img = sess.run(tf.image.decode_image(f.read(), 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])

sess.run(tf.global_variables_initializer())

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

while True:
    num_unquantized_val, err_val, w_val, _ = sess.run(
        [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)
        sys.stderr.flush()
        print(
            'weights', list(w_val.astype(np.int64)),
            'count', count, 'err', err_val)
        best_err = err_val
    count += 1
    print(
        '\r\x1b[Kcount', count, 'err', err_val,
        'unquantized', num_unquantized_val, end='', file=sys.stderr)
    sys.stderr.flush()
    quantization_val = (1 - 1e-4) * quantization_val + 1e-4

Visualization

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

neuron activations

\$\endgroup\$
9
  • \$\begingroup\$ 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. \$\endgroup\$
    – Steven H.
    Commented Dec 4, 2017 at 5:51
  • 5
    \$\begingroup\$ @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. \$\endgroup\$ Commented Dec 4, 2017 at 7:15
  • 14
    \$\begingroup\$ Oh 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. \$\endgroup\$
    – N. Virgo
    Commented Dec 4, 2017 at 10:02
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ Commented Dec 13, 2017 at 1:19
  • 3
    \$\begingroup\$ How long did the training take for this (disregarding quantization)? \$\endgroup\$
    – orlp
    Commented Jun 25, 2018 at 14:29
124
\$\begingroup\$

Mathematica, score 14125.71333

"a.png"~Export~ConstantImage[{28,34,41}/95,{386,320}]

Saves this image:

to a.png.

\$\endgroup\$
12
  • 31
    \$\begingroup\$ @ThomasWeller I just took the mean of each channel. \$\endgroup\$ Commented Jan 24, 2016 at 22:54
  • 39
    \$\begingroup\$ @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... \$\endgroup\$ Commented Jan 27, 2016 at 1:06
  • 67
    \$\begingroup\$ I hope people know that a lower score is better in this challenge. \$\endgroup\$ Commented Jan 27, 2016 at 5:24
  • 52
    \$\begingroup\$ @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. \$\endgroup\$ Commented Jan 27, 2016 at 5:30
  • 42
    \$\begingroup\$ Personally I think this is a fine answer, well within the spirit of the question. \$\endgroup\$
    – N. Virgo
    Commented Jan 28, 2016 at 10:13
112
\$\begingroup\$

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};
double[][]p={{-1},{88},{85,0.284,-0.0064,2.028e-5},{128,0.18},{180,0.674,-0.00473,6.65e-6},{240,-0.181},{272,-0.1167},{273,0.075},{3270,-95.57,0.7},{854,-9.83,0.0381}};
int[]c={-12561790,-11439717,-10981487,-11836288,-9600372,-13088667,-13287091,-13354436,-14275540,-14736605};
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]);}
}
javax.imageio.ImageIO.write(b,"png",new java.io.File("o.png"));
}
}

9807.7168935 points:
submission image
7399.80678201 points:
New submission image

\$\endgroup\$
5
  • 5
    \$\begingroup\$ This is neat! Welcome to PPCG! \$\endgroup\$
    – user45941
    Commented Jan 26, 2016 at 5:25
  • 4
    \$\begingroup\$ If you converted this into Groovy, you could strip some of the boilerplate and get a few more operations in. \$\endgroup\$ Commented Jan 27, 2016 at 2:06
  • 3
    \$\begingroup\$ That looks a lot like it's from the intro of good old Day of the Tentacle. \$\endgroup\$
    – moooeeeep
    Commented Jan 27, 2016 at 18:23
  • 14
    \$\begingroup\$ +1 for actually drawing objects, thus filling 'objectively' term in challenge \$\endgroup\$
    – prototype
    Commented Jan 28, 2016 at 4:08
  • 3
    \$\begingroup\$ From the drawing objects solutions this is actually the nicest one. \$\endgroup\$ Commented Jan 28, 2016 at 12:01
95
\$\begingroup\$

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
PIL.Image.open(io.BytesIO(base64.b85decode('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

\$\endgroup\$
10
  • 3
    \$\begingroup\$ Surely it would be better to store the data using all 256 bytes instead of base 64, 85 or whatever. \$\endgroup\$
    – feersum
    Commented Jan 23, 2016 at 23:08
  • \$\begingroup\$ @feersum I don't know how to store binary data inside Python files in a better way than base encoding. \$\endgroup\$
    – orlp
    Commented Jan 23, 2016 at 23:09
  • \$\begingroup\$ Score confirmed and validated - this is the winner for now. \$\endgroup\$
    – N. Virgo
    Commented Jan 24, 2016 at 2:06
  • 15
    \$\begingroup\$ @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. \$\endgroup\$ Commented Jan 24, 2016 at 7:01
  • 1
    \$\begingroup\$ Yes, 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). \$\endgroup\$
    – nneonneo
    Commented Jan 28, 2016 at 8:18
87
\$\begingroup\$

Python 3, score 5701.31

import base64,io,PIL.Image
PIL.Image.open(io.BytesIO(base64.b85decode('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

\$\endgroup\$
74
\$\begingroup\$

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:

StarryNightAnimation

The final image is this one:

StarryNight

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.io.File;
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(
    275,159,247,237,46,115,108,313,244,267,59,116,94,111,219,166
),Arrays.asList(
    -10127225,-13022618,-11310968,-14341589,-13551293,-14209747,-11311484,-10915442
)); 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, 
c.get(r));}}}}

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

EDIT:

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:

StarryNightMosaic

\$\endgroup\$
16
  • 5
    \$\begingroup\$ +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. \$\endgroup\$
    – neocpp
    Commented Jan 26, 2016 at 20:28
  • \$\begingroup\$ @neocpp I added a short paragraph and an example image with a larger number of points. \$\endgroup\$
    – Marco13
    Commented Jan 28, 2016 at 18:09
  • \$\begingroup\$ You can squeeze a few more bytes using import java.util.*; In fact change all import classes to asterisks. \$\endgroup\$
    – Chloe
    Commented Jan 29, 2016 at 0:53
  • \$\begingroup\$ @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. \$\endgroup\$
    – Marco13
    Commented Jan 29, 2016 at 10:18
  • \$\begingroup\$ 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. \$\endgroup\$
    – neocpp
    Commented Jan 29, 2016 at 13:10
64
\$\begingroup\$

AutoIt, 9183.25 7882.53

UPDATE

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
$c=$3[$6(0,4,1)]
pen($2,$c,$c)
$4($2,$6(0,386),$6(233,320),4,3)
Next

Blobs on the floor

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

pen($2,0x1E221F,0x1E221F)
$4($2,44,44,37,290)
$4($2,40,200,99,130)
$4($2,245,270,30,20)
$4($2,355,165,30,20)

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:

$4($2,333,193,50,31)
pensize($2,10)
pen($2,0x24292E,0x24292E)
move($2,0,250)
linestep($2,386,175)
pen($2,0x9FAC9C,0x9FAC9C)
$4($2,333,120,66,33)
move($2,215,190)
linestep($2,340,140)

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

#include<ScreenCapture.au3>
#include<GDIPlus.au3>
#include<processing.au3>
SRandom(0)
$4=ellipse
$6=Random
$1=GUICreate(0,386,320)
color(0x587092)
$2=size(0,0)
Dim $3=[0x202526,0x48555A,0x394143,0x364458,0x272E3A]
For $n=1 To 1e4*2.9
$c=$3[$6(0,4,1)]
pen($2,$c,$c)
$4($2,$6(0,386),$6(233,320),4,3)
Next
pen($2,0x1E221F,0x1E221F)
$4($2,44,44,37,290)
$4($2,40,200,99,130)
$4($2,245,270,30,20)
$4($2,355,165,30,20)
pen($2,0x506E9B,0x506E9B)
$4($2,333,193,50,31)
pensize($2,10)
pen($2,0x24292E,0x24292E)
move($2,0,250)
linestep($2,386,175)
pen($2,0x9FAC9C,0x9FAC9C)
$4($2,333,120,66,33)
move($2,215,190)
linestep($2,340,140)
$4($2,105,145,40,40)
$4($2,315,15,70,70)
$4($2,215,15,30,30)
pen($2,-1,0xB6A73F)
$4($2,330,30,40,40)
$4($2,20,5,20,20)
$4($2,115,155,30,30)
GUISetState()
Sleep(99)
_GDIPlus_Startup()
$5=_GDIPlus_BitmapCreateFromHBITMAP(_ScreenCapture_CaptureWnd("",$1,1,26,386,345,0))
_gdiplus_ImageSaveToFile($5,_GDIPlus_BitmapApplyEffect($5,_GDIPlus_EffectCreateBlur(20))&".png")

OLD ANSWER

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:

picture

The program is 998 bytes long:

#include<ScreenCapture.au3>
#include<GDIPlus.au3>
$1=GUICreate(0,386,320)
$4=0
For $3=0 To 7
For $2=0 To 9
GUICtrlSetBkColor(GUICtrlCreateLabel("",$2*40,$3*40,40,40),Dec(StringSplit("1B2A56,3D5183,39487B,3E4D7E,313C72,2B375B,333C6F,6E7A8F,878D8A,868985,4E6590,344992,344590,3E5793,485C9C,6A7BA0,525C65,637B8F,96A48D,92A0A0,758087,465D84,3D5C94,4A6387,496498,6A83A4,778F97,607A8F,6A8498,727E6E,72878D,65747E,586D83,71889D,476792,57708B,68899E,7A959A,708892,808789,728282,4C5747,415458,5C778B,5A6E80,4C6C94,63848E,656D6F,6A7687,75858F,434E63,29343D,263036,4C574D,6B747A,506895,2D4871,1D2F46,3D4C46,434E7A,2B2D2A,151B1D,282723,121727,23312F,26343C,213537,1E282E,414861,444F45,887E6B,434133,262216,352D26,3E3522,34322E,444040,352F36,444551,3F4047",",",3)[$4]))
$4+=1
Next
Next
GUISetState()
Sleep(2e3)
_GDIPlus_Startup()
$5=_GDIPlus_BitmapCreateFromHBITMAP(_ScreenCapture_CaptureWnd("",$1,1,26,386,345,0))
_gdiplus_ImageSaveToFile($5,_GDIPlus_BitmapApplyEffect($5,_GDIPlus_EffectCreateBlur(40))&".png")
\$\endgroup\$
6
  • 10
    \$\begingroup\$ Hope you haven't tested the drunk toddler part of the answer. \$\endgroup\$ Commented Jan 26, 2016 at 23:30
  • 5
    \$\begingroup\$ +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! \$\endgroup\$ Commented Jan 27, 2016 at 13:47
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – user42643
    Commented Jan 28, 2016 at 18:26
  • 1
    \$\begingroup\$ It's only a three-byte savings, but isn't 1e4*2.9 equal to 29e3? \$\endgroup\$ Commented Feb 1, 2016 at 5:21
  • 1
    \$\begingroup\$ Well, every byte you save gets you closer to adding another primitive. Using it for optimization is a good point though. \$\endgroup\$ Commented Feb 1, 2016 at 5:32
49
+200
\$\begingroup\$

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
z=n.ones((320,386,3),'u1')
z[:]=145,106,81
z[226:]=80,67,56
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'
m,h=512,62
for a,b,c,d,r in[struct.unpack('HHBBB',s[7*i:7*i+7])for i in range(92)]:
 x,y=a/m-h,b/m-h
 for k in range(h):v.circle(z,(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)
v.imwrite('a.png',v.blur(z,(9,9)))

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  [email protected]...
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):v.circle
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: https://github.com/str4w/starrynight/tree/StackExchange

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

\$\endgroup\$
4
  • 4
    \$\begingroup\$ Excellent 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. \$\endgroup\$
    – N. Virgo
    Commented Mar 20, 2016 at 13:43
  • 4
    \$\begingroup\$ Welcome to CG.SE! Nice first post! \$\endgroup\$
    – Arcturus
    Commented Mar 20, 2016 at 14:24
  • \$\begingroup\$ 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. \$\endgroup\$
    – neocpp
    Commented Mar 27, 2016 at 7:43
  • 1
    \$\begingroup\$ The 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. \$\endgroup\$
    – Strawdog
    Commented Apr 3, 2016 at 12:32
46
\$\begingroup\$

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.

Compression:

  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

\$\endgroup\$
5
  • 2
    \$\begingroup\$ Looks like we have a new winner! (This one indisputably does meet the spec.) \$\endgroup\$
    – N. Virgo
    Commented Jan 27, 2016 at 13:31
  • 9
    \$\begingroup\$ At 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. \$\endgroup\$ Commented Jan 27, 2016 at 13:32
  • 5
    \$\begingroup\$ @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. \$\endgroup\$
    – N. Virgo
    Commented Jan 27, 2016 at 13:37
  • 3
    \$\begingroup\$ @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. \$\endgroup\$ Commented Jan 27, 2016 at 13:44
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – N. Virgo
    Commented Jan 28, 2016 at 9:48
42
\$\begingroup\$

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;}

Hexdump:

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  igned.int 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  ...bt.c.M.&1d[!=
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  P8..by..5.0....@
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:
{a
,{0,174,385,145,{48,56,65}}
,{0,33,322,201,{97,123,144}}
,{289,26,96,136,{152,167,153}}
,{114,62,225,128,{128,150,151}}
,{46,74,116,245,{33,38,36}}
,{150,17,224,63,{170,172,109}}
,{85,41,125,158,{70,94,122}}
,{125,197,260,37,{59,77,118}}
,{109,78,105,138,{111,132,145}}
,{76,94,309,33,{88,115,148}}
,{176,17,139,160,{86,111,148}}
,{213,228,172,35,{62,79,97}}
,{0,11,270,89,{75,94,130}}
}
){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:
{
a
,{48,31,334,288,C{46,55,66}}
,{1,237,169,81,C{35,40,40}}
,{348,48,37,115,C{126,147,155}}
,{165,20,217,68,C{169,173,113}}
,{106,2,209,217,C{98,120,143}}
,{206,199,178,62,C{61,79,108}}
,{11,31,113,48,C{65,83,129}}
,{239,84,109,106,C{108,132,152}}
,{0,78,326,42,C{86,110,142}}
,{47,0,248,55,C{64,79,121}}
}
) { 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

Explanation

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.

\$\endgroup\$
6
  • \$\begingroup\$ 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. \$\endgroup\$
    – user45941
    Commented Jan 26, 2016 at 5:23
  • 4
    \$\begingroup\$ +1 for using a genetic algorithm. I'm hoping that a GA will eventually win this challenge. \$\endgroup\$
    – N. Virgo
    Commented Jan 26, 2016 at 5:34
  • \$\begingroup\$ You can get a couple more bytes by losing the "#include <fstream>" and using "std::cerr << p" at the end. \$\endgroup\$
    – Eponymous
    Commented Jan 26, 2016 at 17:17
  • \$\begingroup\$ @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. \$\endgroup\$
    – neocpp
    Commented Jan 26, 2016 at 17:20
  • \$\begingroup\$ Why a space in #include <fstream>? Also, drop the newlines and put it all on one line since C++ needs semicolons anyways \$\endgroup\$
    – cat
    Commented Mar 27, 2016 at 0:42
41
\$\begingroup\$

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` |
000003c8;

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.

\$\endgroup\$
2
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$
    – Doorknob
    Commented Jan 27, 2016 at 0:35
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – nneonneo
    Commented Jan 28, 2016 at 8:06
37
\$\begingroup\$

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.

imwrite(imresize(reshape('@<8BJJME<NI=388?WI9:IMKNDFRA48;?::65BG<E<478441;>4IC>;5011012313:6IFLF:=>8532313@<4AOI6M\M>22322M><JCECVhZM72312C@AL>HMJNQH44504B8FGBIDF=LE6:738>4@IDFAEAMH9:<69<B>HHNEB>OA;<<99AV?@DFFCCN98<58<?@;G@?IFMQ67;44>=8;XJ?IIKQ89875@>?ABC@ECNM9>:88;AL[TBBCN^F5><7=6F`hgXAI_T==C@;:9Iehh\?RdB5FFD9;7DX]^OD]]66CCA:9:EB?HQSUNFURE7<:A[O@AOUUWNP[J7;=A>?;<JL?G>6;<542=C7JHC?9122113435?<KMTOAFG>642325FB;JWP?U`SB33333SCCRKMN\h_U;4422HDHRFPTSUWQ:7633G>KNKQMOGTM:?858C8EPLOINKUP@@>:<AGDPOVMKHVGAB?;9E[FGKMOMMV?>C89<DFBMIGPPUX:<B84>B<A]SHPQSY<@>:6ADFIKMJOLUV>E?;>=HTb[LMOVdM:EB:@8Odde_NTc\ACIF@=;QfebaLYhG7OLI<>:I^aaXN`a78JGF>;<PRUVX\\YUVYQ?@<:OX[TY``VZZ]X?A:<LQOOUQA?>:=?5219LG@KK?:01///2323JSLV`ZNPQC741223PTVZ\RO]\YE12211SUUXRU]^Z_XA3210QOUUS[^^]VUI<930RSTVY[WZVXSFF;57SQSZZ]WXZVVMF>;<SUT\[`YWWWQPHC<:NOTWZZYZVWNLK>9:SSQSXUX]]XFJK<6>ROTX\YZ][[INB>:CSY^^^Y\[YXMNBACAUZ[ZZ]^\^PKPH?D?YSAFXaa^^HVSKEAAWMK<V^\bJ@`SJC@9TYWQ\Y[^:@[KFE@;'-40,R,C,3)/86,[320,386],'lanczos3'),'t.png')

enter image description here

\$\endgroup\$
32
\$\begingroup\$

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  [email protected]
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  Aw16.sb.v...]...
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  .x...tdZc.g...ni
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  [email protected]`>..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<RH.B.ca
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.

\$\endgroup\$
9
  • \$\begingroup\$ That's neat. Nice one! \$\endgroup\$
    – bjornl
    Commented Jan 28, 2016 at 5:52
  • \$\begingroup\$ 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 =/ \$\endgroup\$
    – orlp
    Commented Jan 28, 2016 at 9:35
  • \$\begingroup\$ Wow! I had no idea BPG was so awesome. \$\endgroup\$
    – user9206
    Commented Jan 28, 2016 at 10:21
  • \$\begingroup\$ @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. \$\endgroup\$
    – nneonneo
    Commented Jan 28, 2016 at 14:45
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – Vi.
    Commented Feb 1, 2016 at 0:31
31
\$\begingroup\$

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

Explanation

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.

\$\endgroup\$
1
  • 35
    \$\begingroup\$ Congratulations, 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). \$\endgroup\$ Commented Oct 17, 2016 at 22:41
29
\$\begingroup\$

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

Code,

#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;}
}r-=1;
}}
int v[] = {
91,116,143,183,150,356,
52,70,125,48,36,51,
60,77,124,165,-236,303,
159,168,159,129,171,24,
115,132,131,29,14,19,
129,133,90,80,56,14,
157,171,136,352,54,41,
184,161,46,353,57,22,
183,184,119,360,52,15,
90,113,146,183,108,59,
141,158,154,373,224,96,
41,46,62,379,219,56,
51,62,77,352,1400,1204,
62,76,96,354,269,73,
51,62,79,236,271,70,
33,37,35,95,274,65,
36,43,44,68,191,29,
42,50,51,66,142,23,
40,46,44,66,110,11,
40,46,44,68,91,5,
111,128,123,231,30,18,
};
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);
return(0);}
\$\endgroup\$
5
  • 8
    \$\begingroup\$ Kudos for doing actual programming! \$\endgroup\$
    – oligofren
    Commented Feb 1, 2016 at 17:54
  • \$\begingroup\$ 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. \$\endgroup\$
    – neocpp
    Commented Feb 14, 2016 at 6:26
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ Commented Feb 15, 2016 at 7:50
  • 1
    \$\begingroup\$ That'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. \$\endgroup\$
    – neocpp
    Commented Feb 15, 2016 at 7:59
  • 2
    \$\begingroup\$ 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. github.com/jamieguinan/starrynight \$\endgroup\$ Commented Feb 16, 2016 at 20:36
27
\$\begingroup\$

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
m=Image.new("RGB",(386,320),"#4b5b6e")
d=D.Draw(m,"RGBA")
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]]
        x,y=3*x,3*y
        d.ellipse([x-w,y-h,x+w,y+h],fill=(2*r,2*g,2*b,2*a))
m.save("a.png")

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  age.new("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)).m.save("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 = Image.open("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
    m=Image.new("RGB",(386,320),"#4b5b6e")
    d=D.Draw(m,"RGBA")
    for i in range(n_genes):
        x,y,w,h,r,g,b,a=[ord(c)-9for c in s[i::n_genes]]
        x,y=3*x,3*y
        d.ellipse([x-w,y-h,x+w,y+h],fill=(2*r,2*g,2*b,a))
    # 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"
        exit()
    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:
        img.save(save_filename)
    return score

# hex escape function from http://stackoverflow.com/a/13935582/1119340
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
m=Image.new("RGB",(386,320),"#4b5b6e")
d=D.Draw(m,"RGBA")
s="'''
    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]]
    x,y=3*x,3*y
    d.ellipse([x-w,y-h,x+w,y+h],fill=(2*r,2*g,2*b,2*a))
m.save("a.png")'''
    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],:]
                else:
                    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
        else:
            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("best_so_far.py",'w') as file:
                file.write(make_full_program(genomes[winner]))

        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.

\$\endgroup\$
2
  • 3
    \$\begingroup\$ Very interesting. I wonder if some of the other box based answers would also be improved by painting transparently. \$\endgroup\$ Commented Feb 3, 2016 at 13:31
  • \$\begingroup\$ 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. \$\endgroup\$
    – N. Virgo
    Commented Feb 7, 2016 at 1:46
26
\$\begingroup\$

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
i=I.new('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:]
e=D.Draw(i)
for X in range(0,len(K),21):
    y=K[X:X+21];x=[]
    for z in range(0,21,3):x+=[int(y[z:z+3])]
    e.rectangle((x[0],x[1],x[2],x[3]),(x[4],x[5],x[6]))
i=i.filter(F.GaussianBlur(radius=5))
i.save('2.png')

enter image description here

\$\endgroup\$
4
  • 7
    \$\begingroup\$ +1 for drawing objects vs just a byte array from compressing the image \$\endgroup\$
    – prototype
    Commented Jan 28, 2016 at 4:09
  • \$\begingroup\$ 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! \$\endgroup\$ Commented Jan 28, 2016 at 6:32
  • \$\begingroup\$ @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. \$\endgroup\$
    – Magenta
    Commented Jan 28, 2016 at 8:03
  • \$\begingroup\$ This is a brilliantly bad answer :) \$\endgroup\$
    – user9206
    Commented Jan 28, 2016 at 10:19
20
+100
\$\begingroup\$

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  [email protected]....
0000360: e355 ccee 3211 6766 11c4 44e0 fce0 0440  .U..2.gf..D....@
0000370: 0e0e 40ce 3888 ece4 3321 6676 11e5 2250  [email protected]!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.

\$\endgroup\$
8
  • 2
    \$\begingroup\$ +1 for nice abuse of the python encoding to store binary data. \$\endgroup\$ Commented Jan 28, 2016 at 15:57
  • 2
    \$\begingroup\$ @2012rcampion: The ! is actually flanked on both sides by non-printable characters. The full color is #1d211e, which is a slightly bluish dark grey. \$\endgroup\$
    – nneonneo
    Commented Feb 1, 2016 at 6:15
  • 2
    \$\begingroup\$ Congrats, this wins the bounty! \$\endgroup\$
    – N. Virgo
    Commented Feb 2, 2016 at 12:35
  • 3
    \$\begingroup\$ If it helps any, the #coding:latin line can be replaced by a UTF-8 byte order mark:  (0xEF,0xBB,0xBF). \$\endgroup\$
    – primo
    Commented Feb 3, 2016 at 4:39
  • 2
    \$\begingroup\$ @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. \$\endgroup\$
    – nneonneo
    Commented Feb 3, 2016 at 6:18
18
\$\begingroup\$

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  [t.gq.G..."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' > genstarry.sh
echo 'exec convert o.png -resize 386x320! o.png' >> genstarry.sh
cat o.flif >> genstarry.sh
zsh genstarry.sh
\$\endgroup\$
1
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 29, 2016 at 23:59
18
\$\begingroup\$

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
PIL.Image.open(io.BytesIO(base64.b64decode('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 = PIL.Image.open('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;
        small.save(tempFileName, quality=q)
        file = open(tempFileName, 'rb')
        data = file.read()
        data64 = base64.b64encode(data)
        bytes = len(data64) + 109   # Decoding code is 109 bytes
        if (bytes <= 1024):  # Size limit
            decoded = PIL.Image.open(io.BytesIO(data))
            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;
best_image.save(finalFileName, quality=best_q)

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

script = open('generated.py', 'wb')
script.write('import base64,io,PIL.Image\n')
script.write('PIL.Image.open(io.BytesIO(base64.b64decode(\'' + data64 + '\'))).resize((386,320),1).save(\'a.png\')')
\$\endgroup\$
0
16
\$\begingroup\$

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)

\$\endgroup\$
5
  • \$\begingroup\$ 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. \$\endgroup\$
    – N. Virgo
    Commented Feb 2, 2016 at 12:47
  • \$\begingroup\$ @Nathaniel I may have just missed the bounty by hours; my optimizer is still running, and I just reached 5000. \$\endgroup\$ Commented Feb 2, 2016 at 13:02
  • \$\begingroup\$ 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.) \$\endgroup\$
    – N. Virgo
    Commented Feb 2, 2016 at 13:24
  • \$\begingroup\$ 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. \$\endgroup\$
    – nneonneo
    Commented Feb 2, 2016 at 16:16
  • \$\begingroup\$ +1 for Voronoi and for using a subplane of the RGB cube. Did you try using any of the answers from codegolf.stackexchange.com/q/50299/8478 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? \$\endgroup\$ Commented Feb 3, 2016 at 14:07
16
\$\begingroup\$

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  .....fk.......fk
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....|~.fm...
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.

\$\endgroup\$
16
\$\begingroup\$

HTML/JavaScript


Score:   10855.83   8000.55   5999.90


Notes:

Due to rendering differences, the score may vary slightly depending on the browser or GPU.

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.


Attempt #1 (Jan 2016): 10855.83

For my first attempt, I had misread the prompt and thought that a higher score was better. Oops.

I used polygons for this attempt because I was focusing more on keeping the total number of shapes low rather than squeezing in as many simple shapes as possible.

All things considered, it's pretty crazy how just 4-5 shapes can look so much like a famous painting.

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

First attempt

<canvas width="386" height="320" id="c">
<script>
var canvas = document.getElementById("c");
var context = canvas.getContext("2d");
context.fillStyle="#2c3e84";
context.fillRect(0,0,386,320);

context.fillStyle="#517a9c";
context.fillRect(0,80,386,150);

context.fillStyle="#1d201d";
context.beginPath();
context.moveTo(33, 319);
context.lineTo(63, 26);
context.lineTo(97, 200);
context.lineTo(179, 319);
context.closePath();
context.fill();

context.fillStyle="#acae6e";
context.beginPath();
context.arc(355,52,35,0,6.3);
context.fill();

context.beginPath();
context.moveTo(0,0);
context.lineTo(300,150);
</script>

Attempt #2 (Sep 2018): 8000.55

For my second attempt, I switched to a rectangle-based approach and made some simple code optimizations to allow for 14+ rectangles.

This time around, I used Inkscape to arrange the shapes and experiment with the colors.

Second attempt

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

function f(x,y,w,h,c)
{
    context.fillStyle="#"+c;
    context.fillRect(x,y,w,h);
}

//Top area
f(0,0,386,250,"607391");
f(0,0,215,48,"415084");
f(298,160,41,28,"9cab9b");

//Bottom area
f(0,250,386,70,"414f5d");

//Middle
f(200,200,188,56,"313f68");
f(133,223,67,27,"2c3955");
f(223,177,74,23,"869999");
f(322,131,64,29,"a9b4a4");
f(354,164,32,32,"26293a");

//Sun outer
f(318,17,67,70,"adaf7b");

//Star
f(107,148,45,44,"a1a9a1");


//Foreground mountain
f(45,210,88,168,"222420");
f(45,130,42,104,"222421");
f(58,20,18,112,"222421");
</script>

Attempt #3 (Mar 2022): 5999.90

For my third attempt, I minified the HTML code and added some size and color compression to the rectangles.

Instead of passing values like 240 to the function that draws the rectangles, I'm now passing values like 30 and having the function multiply them by 8. That makes each function call several bytes smaller on average since the numbers generally have fewer digits.

I also restricted the colors to three-digit values like 6AF rather than 66AAFF or 65ABFE. That restriction makes the colors slightly less accurate individually but saves up to three bytes per function call.

Those changes, along with minifying the HTML code, allowed me to nearly quadruple the number of rectangles!

The exact number of them that will fit into a kilobyte depends on their parameter values. For example, a width like 12 takes up more room than a width like 8, and a mixed color value like "3c3" requires quotes unlike an all-numeric color value such as 333.

With the code below, I was able to paint 50 rectangles onto the canvas using 1021 bytes of barely-valid HTML and JavaScript.

Third attempt

<canvas width=386 height=320><script>function f(f,a,e,l,n){t.fillStyle='#'+n,t.fillRect(8*f,8*a,8*e,8*l)}v=document.querySelector`*>*>*`,t=v.getContext`2d`,f(0,22,39,13,678),f(16,27,33,10,346),f(26,10,24,14,899),f(5,10,8,17,456),f(22,33,27,7,334),f(0,0,50,14,347),f(0,7,51,8,568),f(0,11,45,11,579),f(40,17,9,4,"9A9"),f(0,29,21,8,234),f(2,0,4,4,678),f(39,2,13,10,"9a8"),f(42,22,7,4,224),f(0,35,8,5,343),f(3,1,2,2,995),f(9,0,2,2,667),f(14,0,4,3,678),f(18,2,2,2,776),f(27,2,4,3,787),f(0,14,5,2,"78A"),f(36,3,3,18,569),f(9,5,3,3,775),f(7,0,2,17,345),f(32,7,4,4,788),f(42,8,4,2,"BA3"),f(4,18,4,3,897),f(6,15,5,16,222),f(6,27,10,13,222),f(14,12,2,4,897),f(10,14,6,5,457),f(14,18,5,6,"9a9"),f(38,19,5,4,899),f(45,20,5,2,334),f(27,25,26,2,346),f(30,38,6,3,444),f(11,25,3,2,333),f(41,4,5,4,"BA6"),f(16,31,6,11,222),f(21,0,3,2,568),f(16,6,8,3,678),f(17,12,12,7,679),f(20,15,10,8,568),f(0,29,6,1,347),f(39,24,15,8,458),f(36,29,14,4,456),f(20,31,20,2,345),f(23,31,4,4,345),f(36,35,15,2,345),f(39,24,3,2,234),f(16,11,2,2,346)</script>

HTML tool (Mar 2022)

For the first two attempts, I had used simple trial and error in GIMP/Inkscape to generate some decent rectangle values, which was fine for the small number of rectangles being used. After starting on the third attempt, though, I realized that managing ~50 rectangles by hand would be a terrible idea, so I decided to write a separate HTML tool to automate the process.

The tool went through several iterations, some of which worked better than others, and what I ended up with was a tool that takes an initial array of rectangles and then repeats this loop:

  1. Randomly modify the dimensions, coordinates, and color values of several random rectangles in the initial array
  2. Paint the modified array's rectangles on a canvas and check its score
  3. If the modified array is an improvement, make it the new initial array

I generated some decent starting values by hand using Inkscape, let the tool run for a while in several browser windows, collected its best output, and then gradually updated the starting values and refreshed the page until I ended up with a score that was difficult to improve further.

Here's the final code for that tool. Due to CORS policy, the canvas becomes tainted if you try to run it locally, so you'll need to use a workaround like hosting it on a website. Also, you'll need to include the original Starry Night image in the same directory with the filename reference-image.png.

The starting values included here are already heavily optimized, so you likely won't see any improvements being made unless you run the tool for a very long time or change the starting values.

<html>
<head>
    <style>
        html, body
        {
            margin: 0;
            padding: 0;
        }
    
        .outer-container
        {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            height: 100%;
            gap: 60px;
        }
        
        .inner-container
        {
            display: flex;
            justify-content: center;
            gap: 20px;
        }
        
        .canvas-container
        {
            display: flex;
            flex-direction: column;
            gap: 15px;
        }
        
        .textarea-container
        {
            width: 100%;
            max-width: 1200px;
        }
        
        .textarea-container textarea
        {
            width: 100%;
            height: 120px;
            resize: none;
        }
        
        .canvas-text
        {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 16px;
            font-weight: bold;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="outer-container">
        <div class="inner-container">
            <div class="canvas-container">
                <canvas width="386" height="320" id="reference-image-canvas"></canvas>
                <div class="canvas-text"><span>Original image</span></div>
            </div>
            <div class="canvas-container">
                <canvas width=386 height=320 id=c></canvas>
                <div class="canvas-text"><span>Comparison score:</span> <span id="comparison-score">TBD</span></div>
                <div class="canvas-text"><span>Last 100 average:</span> <span id="average-score">TBD</span></div>
            </div>
            
            <div class="canvas-container">
                <canvas width="386" height="320" id="best-score-canvas"></canvas>
                <div class="canvas-text"><span>Best score:</span> <span id="best-score">TBD</span></div>
                <div class="canvas-text"><span>Num rects:</span> <span id="num-rects">TBD</span></div>
            </div>
        </div>
        <div class="textarea-container">
            <textarea id="html-output-textarea" readonly rows="8"></textarea>
        </div>
    </div>
</body>

<script>
    var best_score = Infinity;
    var best_rects = [];
    var last_100_scores = [];

    function is_canvas_tainted(context)
    {
        try
        {
            var pixel = context.getImageData(0, 0, 1, 1);
            return false;
        }
        
        catch (err)
        {
            return (err.code === 18);
        }
    }
    
    //Prepend the given keys/values to the given index in the given associate array and return a new associate array
    //Usage example: my_array = add_items_to_array_at_position(['one' => 'One', 'three' => 'Three'], 1, ['two' => 'Two'])
    function add_items_to_array_at_position(array, index, new_items)
    {
        return [...array.slice(0, index), ...new_items, ...array.slice(index)];
    }
    
    //Return an int between min and max, inclusive
    //Example: get_random_int_in_range(1, 5) === 5
    function get_random_int_in_range(min, max)
    {
        min = Math.ceil(min);
        max = Math.floor(max) + 1;
        return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
    }
    
    //Example: get_random_hex_code(3) === '08E'
    function get_random_hex_code(num_characters)
    {
        let hex_code = '';
        
        for (let i = 0; i < num_characters; i++)
        {
            hex_code = hex_code + '0123456789ABCDEF'.charAt(Math.floor(Math.random() * 16));
        }
        
        return hex_code;
    }
    
    //Example: paint_square_on_canvas(context, 25, 136, 180, 56, '35F')
    function paint_square_on_canvas(context, x, y, width, height, color_code)
    {
        if (color_code === 'random')
        {
            color_code = get_random_3_digit_hex();
        }
        
        context.fillStyle = '#' + color_code;
        context.fillRect(x * 8, y * 8, width * 8, height * 8);
    }
    
    //Compress a 0-to-255 red, green, and blue value down to a 3-digit hex code like '5fa'
    //TODO: Is there a way to round the values better?
    function convert_rgb_to_3_digit_hex(red, green, blue)
    {
        let red_hex = '0123456789ABCDEF'.charAt(Math.floor(red / 16));
        let green_hex = '0123456789ABCDEF'.charAt(Math.floor(green / 16));
        let blue_hex = '0123456789ABCDEF'.charAt(Math.floor(blue / 16));
        return red_hex + green_hex + blue_hex;
    }
    
    function get_random_3_digit_hex()
    {
        let red_pixel = get_random_int_in_range(0, 255);
        let green_pixel = get_random_int_in_range(0, 255);
        let blue_pixel = get_random_int_in_range(0, 255);
    
        let hex = convert_rgb_to_3_digit_hex(red_pixel, green_pixel, blue_pixel);
        
        return hex;
    }
    
    function get_random_3_digit_hex_from_canvas(pixels)
    {
        let num_pixels = pixels.length;
        
        let pixel_start_index = get_random_int_in_range(0, (pixels.length - 4) / 4) * 4;
        
        let red_pixel = pixels[pixel_start_index + 0];
        let green_pixel = pixels[pixel_start_index + 1];
        let blue_pixel = pixels[pixel_start_index + 2];
        let alpha_pixel = pixels[pixel_start_index + 3];
        
        let hex = convert_rgb_to_3_digit_hex(red_pixel, green_pixel, blue_pixel);
        
        return hex;
    }
    
    //Given a canvas context, top left coordinate, and rectangle width/height, return the average color found within the rectangle
    function get_average_rectangle_color(context, rect_x, rect_y, rect_width, rect_height)
    {
        let canvas_width = context.canvas.clientWidth;
        let canvas_height = context.canvas.clientHeight;
        
        let ref_pixels = context.getImageData(rect_x, rect_y, rect_width, rect_height).data;
        
        let reds = [];
        let greens = [];
        let blues = [];
        
        for (let i = 0, n = ref_pixels.length; i < n; i += 4)
        {
            reds.push(ref_pixels[i+0]);
            greens.push(ref_pixels[i+1]);
            blues.push(ref_pixels[i+2]);
        }
        
        let average_red = reds.reduce((a, b) => a + b) / reds.length;
        let average_green = greens.reduce((a, b) => a + b) / greens.length;
        let average_blue = blues.reduce((a, b) => a + b) / blues.length;
        
        return convert_rgb_to_3_digit_hex(average_red, average_green, average_blue);
    }
    
    function paint_random_squares_on_canvas(ref_context, test_context, num_squares, rects_used)
    {
        let canvas_width = test_context.canvas.clientWidth;
        let canvas_height = test_context.canvas.clientHeight;
        
        let ref_pixels = ref_context.getImageData(0, 0, canvas_width, canvas_height).data;
    
        for (let i = 1; i < num_squares; i++)
        {
            let square_width = Math.round(get_random_int_in_range(4, canvas_width) / 8);
            let square_height = Math.round(get_random_int_in_range(4, canvas_height) / 8); //squares
            
            let square_x = Math.round(get_random_int_in_range(0, (canvas_width / 8) - square_width));
            let square_y = Math.round(get_random_int_in_range(0, (canvas_height / 8) - square_height));
            
            let color_code = get_average_rectangle_color(ref_context, square_x * 8, square_y * 8, square_width * 8, square_height * 8);
            
            paint_square_on_canvas(test_context, square_x, square_y, square_width, square_height, color_code);
            
            rects_used.push({
                                'square_width': square_width,
                                'square_height': square_height,
                                'square_x': square_x,
                                'square_y': square_y,
                                'color_code': color_code
                            });
        }
    }

    function calculate_new_average(test_score)
    {
        last_100_scores.push(test_score);
        
        if (last_100_scores.length === 100)
        {
            let average_score = Math.round(last_100_scores.reduce((a, b) => a + b) / last_100_scores.length);
            let average_score_element = document.querySelector('#average-score');
            average_score_element.innerHTML = average_score;
            last_100_scores = [];
        }
    }
    
    function check_for_new_best(test_pixels, test_score, rects_used)
    {
        calculate_new_average(test_score);
    
        if (test_score >= best_score)
        {
            return false;
        }
    
        let best_canvas = document.querySelector('#best-score-canvas');
        let best_context = best_canvas.getContext('2d');
        let best_score_element = document.querySelector('#best-score');
        
        best_score = test_score;
        
        best_score_element.innerHTML = best_score.toFixed(2);
        
        let num_rects_element = document.querySelector('#num-rects');
        num_rects_element.innerHTML = rects_used.length;
        
        best_context.putImageData(test_pixels, 0, 0);
        
        return true;
    }
    
    function compare_images(ref_context, test_context, rects_used)
    {
        let ref_image_data = ref_context.getImageData(0, 0, ref_context.canvas.clientWidth, ref_context.canvas.clientHeight);
        let ref_pixels = ref_image_data.data;
        
        let test_image_data = test_context.getImageData(0, 0, test_context.canvas.clientWidth, test_context.canvas.clientHeight);
        let test_pixels = test_image_data.data;
    
        if (ref_pixels.length !== test_pixels.length)
        {
            alert('Error! The reference and test image must have the same number of pixels.');
            return null;
        }
        
        let test_score = 0; //lower is better
    
        for (let i = 0; i < ref_pixels.length; i += 4)
        {
            let ref_red = ref_pixels[i] / 255.0;
            let ref_green = ref_pixels[i + 1] / 255.0;
            let ref_blue = ref_pixels[i + 2] / 255.0;
            let ref_alpha = ref_pixels[i + 3] / 255.0;
            
            let test_red = test_pixels[i] / 255.0;
            let test_green = test_pixels[i + 1] / 255.0;
            let test_blue = test_pixels[i + 2] / 255.0;
            let test_alpha = test_pixels[i + 3] / 255.0;
            
            let pixel_diff = ((ref_red - test_red) ** 2) + ((ref_green - test_green) ** 2) + ((ref_blue - test_blue) ** 2);
            
            test_score += pixel_diff;
        }
    
        let test_score_element = document.querySelector('#comparison-score');
        test_score_element.innerHTML = test_score.toFixed(2);
        
        let test_image_is_new_best = check_for_new_best(test_image_data, test_score, rects_used);
        
        if (test_image_is_new_best === true)
        {
            let html_output_textarea = document.querySelector('#html-output-textarea');
            
            best_rects = rects_used;
            
            html_output_textarea.innerHTML = JSON.stringify(best_rects, null, 2);
        }
    }
    
    function find_best_color_for_rect(ref_context, test_context)
    {
    
    }
    
    function clamp_number(number, min, max)
    {
        
    }
    
    //Return a random color code that's similar to the one provided
    //max_changes controls the maximum number of characters in the string that will be changed
    //max_distance controls the maximum distance that each character can be changed
    //Example: get_adjacent_color_code('FC8', 2, 1) === 'EC9'
    function get_adjacent_color_code(color_code, max_changes, max_distance)
    {
        let hex_chars = '0123456789ABCDEF';
        
        let new_color_code = '';
        
        [...color_code].forEach(function(temp_char, old_char_index)
        {
            let decimal_index = hex_chars.indexOf(temp_char);
            let index_change = get_random_int_in_range(-max_distance, max_distance); //TODO: Exclude 0?
            let new_char = hex_chars.charAt(Math.min(Math.max(0, decimal_index + index_change), hex_chars.length - 1));
            new_color_code += new_char;
        });
        
        return new_color_code;
    }

    function try_more_test_squares(ref_context, test_context)
    {
        let rects_used = [];
    
        let ref_image_data = ref_context.getImageData(0, 0, ref_context.canvas.clientWidth, ref_context.canvas.clientHeight);
        let ref_pixels = ref_image_data.data;
    
        ref_canvas_width = ref_context.canvas.clientWidth;
        ref_canvas_height = ref_context.canvas.clientHeight;
    
        test_canvas_width = test_context.canvas.clientWidth;
        test_canvas_height = test_context.canvas.clientHeight;
    
        test_context.clearRect(0, 0, test_canvas_width, test_canvas_height);
        
        let num_squares = 45;
        
        let bg_ref_square_width = Math.round(get_random_int_in_range(4, ref_canvas_width) / 8);
        let bg_ref_square_height = Math.round(get_random_int_in_range(4, ref_canvas_height) / 8); //squares
        let bg_ref_square_x = Math.round(get_random_int_in_range(0, (ref_canvas_width / 8) - bg_ref_square_width));
        let bg_ref_square_y = Math.round(get_random_int_in_range(0, (ref_canvas_height / 8) - bg_ref_square_height));
        let color_code = get_average_rectangle_color(ref_context, bg_ref_square_x * 8, bg_ref_square_y * 8, bg_ref_square_width * 8, bg_ref_square_height * 8);
        
        let rects_to_use;
        
        if (!best_rects || best_rects == false)
        {
            //5999.90 (50 rects)
            rects_to_use = [{"square_x": 0, "square_y": 22, "square_width": 39, "square_height": 13, "color_code": "678"},{"square_x": 16, "square_y": 27, "square_width": 33, "square_height": 10, "color_code": "346"},{"square_x": 26, "square_y": 10, "square_width": 24, "square_height": 14, "color_code": "899"},{"square_x": 5, "square_y": 10, "square_width": 8, "square_height": 17, "color_code": "456"},{"square_x": 22, "square_y": 33, "square_width": 27, "square_height": 7, "color_code": "334"},{"square_x": 0, "square_y": 0, "square_width": 50, "square_height": 14, "color_code": "347"},{"square_x": -1, "square_y": 7, "square_width": 51, "square_height": 8, "color_code": "568"},{"square_x": 0, "square_y": 11, "square_width": 45, "square_height": 11, "color_code": "579"},{"square_x": 40, "square_y": 17, "square_width": 9, "square_height": 4, "color_code": "9A9"},{"square_x": 0, "square_y": 29, "square_width": 21, "square_height": 8, "color_code": "234"},{"square_x": 2, "square_y": 0, "square_width": 4, "square_height": 4, "color_code": "678"},{"square_x": 39, "square_y": 2, "square_width": 13, "square_height": 10, "color_code": "9a8"},{"square_x": 42, "square_y": 22, "square_width": 7, "square_height": 4, "color_code": "224"},{"square_x": -1, "square_y": 35, "square_width": 8, "square_height": 5, "color_code": "343"},{"square_x": 3, "square_y": 1, "square_width": 2, "square_height": 2, "color_code": "995"},{"square_x": 9, "square_y": 0, "square_width": 2, "square_height": 2, "color_code": "667"},{"square_x": 14, "square_y": 0, "square_width": 4, "square_height": 3, "color_code": "678"},{"square_x": 18, "square_y": 2, "square_width": 2, "square_height": 2, "color_code": "776"},{"square_x": 27, "square_y": 2, "square_width": 4, "square_height": 3, "color_code": "787"},{"square_x": -2, "square_y": 14, "square_width": 7, "square_height": 2, "color_code": "78A"},{"square_x": 36, "square_y": 3, "square_width": 3, "square_height": 18, "color_code": "569"},{"square_x": 9, "square_y": 5, "square_width": 3, "square_height": 3, "color_code": "775"},{"square_x": 7, "square_y": -2, "square_width": 2, "square_height": 17, "color_code": "345"},{"square_x": 32, "square_y": 7, "square_width": 4, "square_height": 4, "color_code": "788"},{"square_x": 42, "square_y": 8, "square_width": 4, "square_height": 2, "color_code": "BA3"},{"square_x": 4, "square_y": 18, "square_width": 4, "square_height": 3, "color_code": "897"},{"square_x": 6, "square_y": 15, "square_width": 5, "square_height": 16, "color_code": "222"},{"square_x": 6, "square_y": 27, "square_width": 10, "square_height": 13, "color_code": "222"},{"square_x": 14, "square_y": 12, "square_width": 2, "square_height": 4, "color_code": "897"},{"square_x": 10, "square_y": 14, "square_width": 6, "square_height": 5, "color_code": "457"},{"square_x": 14, "square_y": 18, "square_width": 5, "square_height": 6, "color_code": "9a9"},{"square_x": 38, "square_y": 19, "square_width": 5, "square_height": 4, "color_code": "899"},{"square_x": 45, "square_y": 20, "square_width": 5, "square_height": 2, "color_code": "334"},{"square_x": 27, "square_y": 25, "square_width": 26, "square_height": 2, "color_code": "346"},{"square_x": 30, "square_y": 38, "square_width": 6, "square_height": 3, "color_code": "444"},{"square_x": 11, "square_y": 25, "square_width": 3, "square_height": 2, "color_code": "333"},{"square_x": 41, "square_y": 4, "square_width": 5, "square_height": 4, "color_code": "BA6"},{"square_x": 16, "square_y": 31, "square_width": 6, "square_height": 11, "color_code": "222"},{"square_x": 24, "square_y": -1, "square_width": -3, "square_height": 3, "color_code": "568"},{"square_x": 16, "square_y": 6, "square_width": 8, "square_height": 3, "color_code": "678"},{"square_x": 17, "square_y": 12, "square_width": 12, "square_height": 7, "color_code": "679"},{"square_x": 30, "square_y": 15, "square_width": -10, "square_height": 8, "color_code": "568"},{"square_x": -2, "square_y": 29, "square_width": 8, "square_height": 1, "color_code": "347"},{"square_x": 39, "square_y": 24, "square_width": 15, "square_height": 8, "color_code": "458"},{"square_x": 36, "square_y": 29, "square_width": 14, "square_height": 4, "color_code": "456"},{"square_x": 20, "square_y": 33, "square_width": 20, "square_height": -2, "color_code": "345"},{"square_x": 23, "square_y": 31, "square_width": 4, "square_height": 4, "color_code": "345"},{"square_x": 36, "square_y": 35, "square_width": 15, "square_height": 2, "color_code": "345"},{"square_x": 39, "square_y": 24, "square_width": 3, "square_height": 2, "color_code": "234"},{"square_x": 16, "square_y": 11, "square_width": 2, "square_height": 2, "color_code": "346"}];
        }
        
        else
        {
            rects_to_use = structuredClone(best_rects);
            
            let max_rects = 50;
            
            //Chance to add a new rectangle
            
            for (let i = 1; i < (get_random_int_in_range(0, max_rects - rects_to_use.length)); i++)
            {
                if (Math.random() < (.05))
                {
                    let square_width = Math.round(get_random_int_in_range(1, test_canvas_width) / 8);
                    let square_height = Math.round(get_random_int_in_range(1, test_canvas_height) / 8); //squares
                    
                    let square_x = Math.round(get_random_int_in_range(0, (test_canvas_width / 8) - square_width));
                    let square_y = Math.round(get_random_int_in_range(0, (test_canvas_height / 8) - square_height));
                    
                    let color_code = get_random_hex_code(3);
                    
                    let new_rect = {square_x: square_x, square_y: square_y, square_width: square_width, square_height: square_height, color_code: color_code};
                    
                    rects_to_use = add_items_to_array_at_position(rects_to_use, get_random_int_in_range(0, rects_to_use.length - 1), [new_rect]);
                }
            }
            
            
            //Chance to change each rectangle's color
            for (let num_color_loops = 0; num_color_loops < 6; num_color_loops++)
            {
                if (Math.random() < .3)
                {
                    rects_to_use[get_random_int_in_range(0, rects_to_use.length - 1)].color_code = get_random_3_digit_hex();
                }
            }
            
            
            //Chance to change rectangle's position
            
            let coord_change;
            
            if (Math.random() < .2)
            {
                coord_change = ((Math.random() < .5) ? 1 : -1) * get_random_int_in_range(1, 6);
                rects_to_use[get_random_int_in_range(0, rects_to_use.length - 1)].square_x += coord_change;
            }
            
            if (Math.random() < .2)
            {
                coord_change = ((Math.random() < .5) ? 1 : -1) * get_random_int_in_range(1, 6);
                rects_to_use[get_random_int_in_range(0, rects_to_use.length - 1)].square_y += coord_change;
            }
            
            if (Math.random() < .2)
            {
                coord_change = ((Math.random() < .5) ? 1 : -1) * get_random_int_in_range(1, 6);
                rects_to_use[get_random_int_in_range(0, rects_to_use.length - 1)].square_width += coord_change;
            }
            
            if (Math.random() < .2)
            {
                coord_change = ((Math.random() < .5) ? 1 : -1) * get_random_int_in_range(1, 6);
                rects_to_use[get_random_int_in_range(0, rects_to_use.length - 1)].square_height += coord_change;
            }
            
            if (Math.random() < .2)
            {
                coord_change = ((Math.random() < .5) ? 1 : -1) * get_random_int_in_range(1, 6);
                rects_to_use[get_random_int_in_range(0, rects_to_use.length - 1)].square_height += coord_change;
            }
        }
        
        rects_to_use.forEach(function(rect)
        {
            paint_square_on_canvas(test_context, rect.square_x, rect.square_y, rect.square_width, rect.square_height, rect.color_code);
            rects_used.push(rect);
        });
        
        compare_images(ref_context, test_context, rects_used);
    }

    document.addEventListener("DOMContentLoaded", function()
    {
        let ref_canvas = document.querySelector('#reference-image-canvas');
        let ref_context = ref_canvas.getContext('2d');
        
        let test_canvas = document.querySelector('#c');
        let test_context = test_canvas.getContext('2d');
        
        let ref_image = new Image();
                
        ref_image.onload = function()
        {
            ref_context.drawImage(ref_image, 0, 0);
            
            setInterval(function(){try_more_test_squares(ref_context, test_context)}, 1);
        };
        
        ref_image.src = 'reference-image.png';
    });
</script>
</html>

Example of my workflow using the tool:

enter image description here

enter image description here


Future plans

My approach so far has been limited to rectangles. Based on how curvy the original Starry Night painting is, I could probably achieve a higher score by using circles or polygons.

I also realized too late that I overcompressed the coordinates and dimensions in attempt #3 and could have allowed the rectangles to have slightly more precision without adding any additional bytes.

After finishing attempt #3, I tested out several canvas filters and was able to immediately improve its final score by several hundred points using blur, drop-shadow, and opacity. However, I decided to exclude those improvements for now until I can properly incorporate them into the tool.

\$\endgroup\$
4
  • 22
    \$\begingroup\$ If you used a single letter instead of "context" and removed unnecessary whitespace you might have room to add additional detail to the image. \$\endgroup\$ Commented Jan 29, 2016 at 14:04
  • 3
    \$\begingroup\$ @RedwolfPrograms This isn't a code-golf challenge, answers don't have to golf their answers, so long as the code is less than 1024 bytes. \$\endgroup\$ Commented Apr 5, 2021 at 20:32
  • \$\begingroup\$ @ChartZBelatedly But that still involves golfing, a lot of improvement would be possible by adding more rectangles, which requires saving more space. \$\endgroup\$
    – rydwolf
    Commented Apr 5, 2021 at 21:34
  • 2
    \$\begingroup\$ While this does not attempt golfing and could be clearly improved if that wasted space were replaced with more rectangles, I believe it is still a serious enough contender to stand as an answer. However, I would still recommend removing the unnecessary space and variable names to instead improve the image accuracy. \$\endgroup\$
    – hyperneutrino
    Commented Apr 6, 2021 at 14:09
13
\$\begingroup\$

Scala, 6003.56

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

object T extends App {
import com.github.libxjava.io._, 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

\$\endgroup\$
6
  • \$\begingroup\$ Yep just did that sorry :) \$\endgroup\$
    – sksamuel
    Commented Jan 23, 2016 at 19:45
  • \$\begingroup\$ This look like 993 chars \$\endgroup\$
    – Downgoat
    Commented Jan 23, 2016 at 20:06
  • \$\begingroup\$ So it is. Updated. \$\endgroup\$
    – sksamuel
    Commented Jan 23, 2016 at 20:08
  • \$\begingroup\$ Just out of curiosity, it's there any significance of having the import inside of the object? \$\endgroup\$ Commented Jan 24, 2016 at 22:42
  • \$\begingroup\$ No, it was just easier to copy and paste I thought \$\endgroup\$
    – sksamuel
    Commented Jan 24, 2016 at 22:49
13
\$\begingroup\$

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);
        g=(Graphics2D)b.getGraphics();
        col(77,98,119);
        rect(0,0,386,320);
        col(82,98,128);
        rect(11,0,386,57);
        col(186,159,80);
        rect(333,43,24,24);
        col(73,90,104);
        rect(0,282,386,50);
        col(76,97,95);
        rect(0,292,60,30);
        col(55,65,72);
        rect(34,130,91,190);
        rect(32,143,97,190);
        rect(46,193,99,190);
        rect(40,32,50,110);
        rect(45,20,43,110);
        javax.imageio.ImageIO.write(b,"png",new java.io.File("out.png"));
    }
}

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

Results:

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

\$\endgroup\$
4
  • 11
    \$\begingroup\$ SE inserts the link so images can be clicked to view them individually at full size. \$\endgroup\$ Commented Jan 24, 2016 at 7:04
  • \$\begingroup\$ @Calvin'sHobbies But these images are small enough that the full size fits in the post perfectly anyway. \$\endgroup\$ Commented Jan 25, 2016 at 1:30
  • 7
    \$\begingroup\$ Not necessarily for all screen/window sizes. \$\endgroup\$ Commented Jan 25, 2016 at 1:54
  • 1
    \$\begingroup\$ So this is how this game's graphics were designed \$\endgroup\$
    – Luis Mendo
    Commented Mar 28, 2016 at 23:51
11
\$\begingroup\$

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."....bl....#_.
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:,[email protected]$..X.
0000280: 5375 f9db a740 c166 e098 c4c2 3d9b dad3  [email protected]....=...
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  "f......9..7.nf>
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  [email protected])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

\$\endgroup\$
2
  • \$\begingroup\$ I think you should have added another answer, instead of embedding your two answers into one post \$\endgroup\$
    – anatolyg
    Commented Feb 8, 2016 at 19:31
  • \$\begingroup\$ @anatolyg These are not two answers, this is one method with two versions, the latter being improved by using another codec. \$\endgroup\$
    – niutech
    Commented Feb 8, 2016 at 20:54
11
\$\begingroup\$

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)
c=k*256+255
$p.rect(w,x,y,z,c,c)
end
$p=ChunkyPNG::Image.new(386,320,255)
r(97,160,192,199,7374989)
r(0,80,48,159,7045519)
r(193,160,289,199,6716560)
r(0,220,192,239,2963277)
r(338,80,385,159,8623770)
r(0,0,48,79,4938368)
r(49,0,96,79,4212844)
r(193,240,385,259,3756895)
r(193,260,385,279,3094854)
r(290,0,313,159,5271706)
r(314,0,337,159,8555664)
r(290,160,337,199,7570058)
r(338,160,385,199,3883603)
r(193,0,289,39,4674429)
r(193,40,289,79,5398918)
r(0,240,96,319,2566701)
r(97,240,192,319,2041898)
r(193,200,289,239,3424622)
r(290,200,385,239,4348033)
r(97,80,144,159,6124943)
r(145,80,192,159,5926550)
r(97,0,192,39,4609663)
r(97,40,192,79,5334923)
r(193,280,385,299,3619650)
r(193,300,385,319,3815999)
r(49,80,96,119,5334650)
r(49,120,96,159,2767683)
r(0,200,96,219,3951445)
r(97,200,192,219,5860985)
r(0,160,48,199,5468034)
r(49,160,96,199,2503733)
r(193,80,289,119,5599124)
r(193,120,289,159,6189974)
r(338,0,385,39,7900038)
r(338,40,385,79,11317092)
$p.save('o.png')

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 https://github.com/jrotter/starry_night_contest

\$\endgroup\$
2
  • 2
    \$\begingroup\$ +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. \$\endgroup\$
    – N. Virgo
    Commented Feb 3, 2016 at 2:26
  • \$\begingroup\$ 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. \$\endgroup\$
    – Jeremy
    Commented Feb 3, 2016 at 2:33
11
\$\begingroup\$

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;
$X=GD::Image->new(386,320,1);
@r=do{$/=\8;<DATA>;};
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;
close($h);
__END__
<<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  /....va.*...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  [email protected]..>.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;
$X=GD::Image->new(386,320);
sub c{$X->colorAllocate(@_);};
sub e{$X->filledEllipse(@_);};
e(0,7,9,4,c(98,122,142));
e(352,168,130,61,c(38,41,57));
e(313,296,319,213,c(44,56,92));
e(281,71,240,257,c(99,127,149));
e(55,266,372,67,c(41,55,96));
e(39,15,281,130,c(55,73,128));
e(235,149,226,90,c(136,156,152));
e(81,55,29,29,c(129,133,89));
e(183,139,285,65,c(58,79,108));
e(368,261,177,130,c(75,97,145));
e(283,270,386,88,c(70,86,99));
e(271,11,322,58,c(86,104,129));
e(254,78,185,200,c(90,116,151));
e(29,16,34,35,c(119,135,130));
e(195,311,297,189,c(51,69,98));
e(234,19,150,89,c(58,75,126));
e(286,313,286,108,c(49,56,68));
e(232,30,40,31,c(114,130,122));
e(375,145,106,32,c(159,172,160));
e(59,16,21,141,c(43,53,90));
e(66,135,21,108,c(39,45,43));
e(353,53,84,80,c(167,172,117));
e(196,122,150,137,c(86,110,145));
e(101,320,284,185,c(48,55,59));
e(79,193,73,114,c(36,42,42));
e(106,310,126,191,c(31,35,33));
e(119,169,70,56,c(128,144,150));
open $h,">","p.png" or die "$!";
binmode $h;
print $h $X->png;
close($h);

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$
    – N. Virgo
    Commented Feb 3, 2016 at 3:54
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – neocpp
    Commented Feb 3, 2016 at 4:05
7
\$\begingroup\$

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.io.*;
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.setRenderingHint(KEY_INTERPOLATION,VALUE_INTERPOLATION_BILINEAR);
        g.drawImage(ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode("R0lGODlhCAAGAPUAABMXFxIfHxQZGBcZHRgcGRwfKhssIB0iMC4kGicwOy06PzA6OS8zSjY7SztHQThZVzJCcFdgUFJhdFZrfSo7ny1AgC9Mjz1XjT1ZqkRNgUFVj0BeiEJUmEFhjkJtnlJwnGd8mXF+gmF9plyGqW6Gk26KmHSJmnuSn32Zl6C1e7u/dYGNkYCVnZOahI6gmYaiuhMXFxMXFxMXFxMXFxMXFxMXFxMXFxMXFxMXFxMXFxMXFxMXFxMXFxMXFxMXFxMXFywAAAAACAAGAEUIOQBfZMBQwYQGFCkoEACQQIKDBxFIGOAA4sIHES0ghJjQwQKLESpWLDjhwUWJDQ0UBBCAYEABBgcCAgA7"))), 0, 0, 386, 320, null);
        g.dispose();
        ImageIO.write(r, "PNG", new File("RESULT.PNG"));
    }        
}

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

StarryNight

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Could you post the image? \$\endgroup\$
    – N. Virgo
    Commented Jan 26, 2016 at 5:23
7
\$\begingroup\$

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
K=bin(int('29zoubbejrojv5nc2c09w8008mmc93di8nsq75g5ep83xtelg14ua2jvhm6ez5gry7isq1g82qvqezkbvl0ibovc6kltldjgklqeg7g5oyfefamfrvei712jnrd8a2ften12xme2bfh654a6r8kfe5xtckpxxt60pujhs02r0zt9a733ofmyhsprmxw9max72f9az1cpsa48szbbi3cl0ah4tusuuin49vtzambzv8omzfa0lt9wkot1p17trvvvvwmrf31g14vvs59ea3uo3k2ycgibgxwnd7qbv6enrynzwhng30thklvk4mvrhf66ba0gqnyf0do6xn9xfjker8fnpr79zac6tsowm6oohszjc16k3a8iisv7yj7i67aq6r7f629zldmv9l816azzu96jikqaw02icsv9b79yy73gbvw0scid9266hph04m6nb3lae5a59d6djauw38i1wtd7qqn17uxugi4r52y0cfpjsb444uj30gih7jmek26uhdn41w2b2g0y34xl1kgxegkjtj6iq1u3k3zk34qtw76hysxj6jl7qrj908pa5vcao6m4i4m2h8sg4ir10mh1y315bakfag611ilwy7y569jh18ydabo5zgdyr7m5vcc9dqxj63nu2s67urqui8gnqu9u40hahyehqu9ugtqf8ab0m1v4fu5pr88k6ch7ep0echekocg78za1f74ladjgm',36))[3:]
a=[]
for x in range(0,len(K),18):
    Y=K[x:x+18]
    y=[Y[0:6],Y[6:12],Y[12:18]]
    for x in range(0,3):
        y[x]=int(y[x],2)*8
    a.append(tuple(y))
i=new('RGB',(16,13))
i.putdata(a)
i.resize((386,320),1).save('2.png')

enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ Toooooo blurry! \$\endgroup\$
    – user9206
    Commented Jan 28, 2016 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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