Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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

Sometimes when I'm bored, I take some text and fill the "holes" in the letters. But isn't filling the holes the most boring thing you can do? I think we should automate it, so we could use our time better.

Standard rules apply.


A string containing sequence of alphanumeric characters (a-z, A-Z, 0-9) and spaces.


Image containing string rendered and holes on it filled. You can use any human readable font as long as it still requires filling the holes. You can either save image to file i.png (in png format) or just display the image.

Image properties:

  • Black text
  • White or transparent background
  • Padding:
    • The dimensions of the image can be maximum two times the dimensions of the text
    • Padding should be same color as background, either white or transparent


Input: Example text

Output: Example output

share|improve this question
Related. (same challenge, different scoring.) – Martin Ender Feb 13 at 15:43
Is there a minimum font size for the letters (in pixels)? – Martin Ender Feb 13 at 15:44
Yes, lets say it is 12px – Hannes Karppila Feb 13 at 18:57

Bash, 135 bytes

convert +antialias -pointsize 99 label:$1 -fill red -draw 'color 0,0 floodfill' -fill black -opaque white -fill white -opaque red i.png

Uses ImageMagick (convert).

Sample output:


  +antialias                              # disable antialiasing
  -pointsize 99                           # annoyingly necessary (see below)
  label:$1                                # draw input text
  -fill red -draw 'color 0,0 floodfill'   # flood fill from (0,0) with red
  -fill black -opaque white               # replace all white with black
  -fill white -opaque red                 # replace all red with white

Disabling antialiasing is required because otherwise antialiased interior parts of the letters wouldn't flood fill. Setting the font to a large size is also required because some fonts have "gaps" in letters that should have "holes" in them at small font sizes (in my tests, the a wasn't filled at the default small font size).

share|improve this answer

Mathematica, 83 bytes


enter image description here

An unnamed function that takes a string as input and returns an image object. The idea is to use DeleteBorderComponents to keep only the holes and then subtract those from the original image.

share|improve this answer
The funny thing is that Mathematica wins often even with long builtin names like ImageSubtract and DeleteBorderComponents. – J Atkin Feb 13 at 17:21
That's because Mathematica has builtin names for many things - you don't need to create your own functions often. – avocad juic Feb 14 at 5:35

Eats away at the text a bit, but there's also a canvas-based solution:

JS, 610 bytes

function o(a,b){return a[b]+a[b+1]+a[b+2]}x=prompt();l=x.length;c=document.createElement("canvas");document.body.appendChild(c);h=33;w=18*l;c.height=h;c.width=w;n=255;m=764;t=c.getContext("2d");t.fillStyle="white";t.fillRect(0,0,w,h);t.fillStyle="red";t.fillRect(0,2,w,h);t.font="900 30px Courier";t.fillStyle="black";t.fillText(x,0,25);d=t.getImageData(0,0,w,h);f=0;;for(i=0;i<20;i++){for(j=0;j<q;j+=4){;if(o(f,j)>0&&(o(f,j-w*4)>m||o(f,j+w*4)>m||o(f,j-4)>m||o(f,j+4)>m)){f[j]=n;f[j+1]=n;f[j+2]=n}}}for(k=0;k<q;k+=4){;if(f[k+1]<1){f[k]=0;f[k+1]=0;f[k+2]=0}}t.putImageData(d,0,0)

enter image description here

share|improve this answer

Your Answer


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

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