Approximate a given image using only colors from a given list
Mathematica 109 100 97 111
Update: now outputs the image as required.
The function, f, replaces a single pixel with the closest pixel found in the input file (in this case, &quot;colors.txt&quot;).
t is the output file.
The function, g, applies f to each pixel in the input image.
g[i_,h_,t_]:=Export[t,(c=Import[h,&quot;Data&quot;]/255;
f@p_:=Sort[{p~EuclideanDistance~#,#}&amp;/@c][[1,2]];f~ImageApply~i)]

Test Cases
Example 1: Seascape
(*creating &quot;colors.txt&quot;*)
    c = &quot;255 0 0
      250 150 0
      255 50 0
      0 0 200
      0 0 80
      0 0 0&quot;;
    Export[&quot;colors.txt&quot;, c]



Example 2: Earth from moon
I'll skip the part about storing the color info into &quot;colors.txt&quot;.
It closely follows example 1.


Example 3: Jupiter


Example 4: American Gothic