50
\$\begingroup\$

So, I want you to make a PNG image that says "Hello world!" and nothing else on a distinguishable background using only an image making API (eg ImageMagick) and return the image. Fewest characters win, as usual. Oh, and you can use any color text.

\$\endgroup\$

closed as too broad by Blue, DJMcMayhem, mbomb007, Rɪᴋᴇʀ, Alex A. Apr 26 '16 at 22:57

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Does it have to output Hello World or Hello World!? \$\endgroup\$ – The Guy with The Hat Feb 2 '14 at 12:55
  • 3
    \$\begingroup\$ @ryan Hello World! is the right one. \$\endgroup\$ – figgycity50 Feb 2 '14 at 14:52
  • 3
    \$\begingroup\$ A lot of answers could save one character by replacing the double 'l' in 'hello' with the medieval Welsh ligatures 'Ỻ' or 'ỻ': once the chars have been transformed to pixels it shouldn't matter what their origin was as long as people still perceive 'heỻo world!' as 'hello world!'. \$\endgroup\$ – timxyz Feb 3 '14 at 11:27
  • 9
    \$\begingroup\$ @poldie Your browser changes PNG based on how the pixels got in there? \$\endgroup\$ – Christopher Creutzig Feb 4 '14 at 2:22
  • 2
    \$\begingroup\$ @timxyz For me, ỻ is rendered as a box containing 1EFB. I tried to use it in my program anyway, and the result was a square. So, this is probably not very portable. \$\endgroup\$ – Victor Stafusa Feb 4 '14 at 5:24

41 Answers 41

1
\$\begingroup\$

Matlab: 47 characters

I'm not sure what exactly an Image making API is. This uses a figure/graph making API, which is arguably an image.

text(1,1,'Hello World!');axis off;print -dpng f

Matlab example

\$\endgroup\$
1
\$\begingroup\$

Chloe used a Base64 encoded approach with 1494 characters, however you can easily reduce the size:

Anchor link - 166 chars

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAD8AAAAFAQAAAADaxQm5AAAAM0lEQVQIW2Mo0a+w3iKQz8NQ+r56r+71/BIGlnwg40J+GUhkz9lr78sYShQ57O6IKfIAAJypEkNzAX3zAAAAAElFTkSuQmCC

Paste this into your browser

\$\endgroup\$
1
\$\begingroup\$

Bash, using Google's chart API: 92 characters

curl http://chart.googleapis.com/chart?chst=d_text_outline&chld=|9|h|||Hello+world! > h.png

enter image description here

Further optimization: 33 characters

curl http://goo.gl/a8ntA4 > h.png

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Don't know if the "further optimization" counts, as it caches data in the cloud in the form of a shortened URL service, which allows one to make a script of arbitrary size and complexity to solve this problem that never requires more than 33 characters to represent. \$\endgroup\$ – Adam Davis Feb 3 '14 at 18:07
  • 2
    \$\begingroup\$ Yeah, I wouldn't think using a URL shortener is very kosher for this. :-) \$\endgroup\$ – Chris Jester-Young Feb 3 '14 at 18:21
  • 1
    \$\begingroup\$ I have a shorter solution: cat image.png > h.png where image.png is the result of curl http://goo.gl/a8ntA4 > image.png!!! \$\endgroup\$ – Farid Nouri Neshat Feb 4 '14 at 12:32
1
\$\begingroup\$

EPL, 28

0,0,0,1,1,1,N,"Hello World!"
\$\endgroup\$
  • 1
    \$\begingroup\$ Can you show the output? \$\endgroup\$ – The Guy with The Hat Feb 4 '14 at 13:22
1
\$\begingroup\$

MATLAB, 41 chars

@gerrit posted a MATLAB solution as well, but since I don't have enough rep to comment, I'll post mine too. (The output is the same as his, so I don't want to take up more space with the resulting PNG).

axis off,title 'Hello world!',print -dpng
\$\endgroup\$
1
\$\begingroup\$

Groovy, 133

Not going to win any points for size here as it's just an abbreviated Java.

No color setting:

i=new java.awt.image.BufferedImage(75,30,2);i.graphics.drawString('Hello World',5,20);javax.imageio.ImageIO.write(i,'PNG',System.out)

With setting color (164):

i=new java.awt.image.BufferedImage(75,30,2);g=i.graphics;g.color=java.awt.Color.RED;g.drawString('Hello World',5,20);javax.imageio.ImageIO.write(i,'PNG',System.out)

Formatted:

i = new java.awt.image.BufferedImage(75,30,2)
g = i.graphics
g.color = java.awt.Color.RED
g.drawString('Hello World',5,20)
javax.imageio.ImageIO.write(i,'PNG',System.out)

hw.png

\$\endgroup\$
1
\$\begingroup\$

Here is a cool trick, only 37 chars: placehold.it/png/99&text=Hello+world! (+7 chars for the protocol).

This generates a 239 bytes long png image with 99px of width and height.

I know it's not programming, but this is a website that generates placeholder images.

You should visit http://placehold.it.

\$\endgroup\$
  • \$\begingroup\$ similar to codegolf.stackexchange.com/a/20019 \$\endgroup\$ – gnibbler Feb 5 '14 at 13:23
  • \$\begingroup\$ Similar, but WAAAAAAAAAAAAY shorter and a totally different website. dummyimage.com is bigger than placehold.it \$\endgroup\$ – Ismael Miguel Feb 5 '14 at 17:29
  • \$\begingroup\$ cambelt.co/300/Hello+world! and fakeimg.pl/99/?text=Hello+world! are even shorter. Should I post two more answers with them? No. Multiple answers using the same language and principles are boring. If you have improvement for existing answer, please post it as comment. \$\endgroup\$ – manatwork Feb 6 '14 at 8:17
  • \$\begingroup\$ If you didn't downvoted, perhaps I could get enough reputation to make comments on other posts! \$\endgroup\$ – Ismael Miguel Feb 6 '14 at 9:34
  • \$\begingroup\$ Ah, that is indeed a good reason for not posting comment. Revoked the vote. (I had to make and edit to your post to be able to change my vote.) \$\endgroup\$ – manatwork Feb 6 '14 at 10:23
0
\$\begingroup\$

Groovy, 151 (or 202 with a black text)

a rewrite of the proposed Java version. Resulting image is with white text on a transparent background. Add 50 chars to have black text !

import java.awt.image.BufferedImage as B
B b=new B(80,9,2)
b.graphics.drawString("Hello world!",5,9)
javax.imageio.ImageIO.write(b,"png",new File("p"))

The version with black text:

import java.awt.*
import java.awt.image.BufferedImage as B
B b=new B(80,9,2)
b.graphics.with {setColor(new Color(0)); drawString("Hello world!",5,9); }
javax.imageio.ImageIO.write(b,"png",new File("p"))

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Could you please post the resulted image? By the way, are all those spaces necessary in the code? \$\endgroup\$ – manatwork Feb 4 '14 at 10:44
  • \$\begingroup\$ image posted and useless spaces removed \$\endgroup\$ – Guillaume Feb 4 '14 at 12:53
0
\$\begingroup\$

Ruby 192 chars:

I use RMagick RVG (Ruby Vector Graphics).

require 'rvg/rvg';include Magick;RVG::dpi = 72;RVG.new(1.5.in, 1.5.in){|canvas|canvas.background_fill = '#C0C0C0';canvas.text(10, 100){|title|;title.tspan("Hello World!")}}.draw.write('hw.png')

output:
enter image description here

\$\endgroup\$
0
\$\begingroup\$

Shell + Graphviz (55)

echo 'graph{a[label="Hello World!"]}'|dot -Tpng>h.png
\$\endgroup\$
  • \$\begingroup\$ @Timtech I don't know what shell you're using, but the ! works fine for me in bash and doesn't need to be escaped. \$\endgroup\$ – grim Feb 7 '14 at 22:21
  • \$\begingroup\$ That's really weird - all I did was add four spaces in front of your answer to format is correctly. I see what you mean, though O_o I don't remember doing that at all :S \$\endgroup\$ – Timtech Feb 8 '14 at 0:19
  • \$\begingroup\$ alright, well I'm going to remove it then :) Thanks for getting the code formatting, not sure how I missed it.. \$\endgroup\$ – grim Feb 9 '14 at 6:42
  • \$\begingroup\$ It's fine - I usually do that kind of stuff whenever I see it :) \$\endgroup\$ – Timtech Feb 9 '14 at 16:37
  • \$\begingroup\$ You can get away with echo 'graph{"Hello World!"}'|dot -Tpng>a.png, which is 44 characters... \$\endgroup\$ – Martin Broadhurst Jul 11 '15 at 15:23
-1
\$\begingroup\$

Mouse: 0 chars

"right click" > "save image as"

Result:

enter image description here

\$\endgroup\$
  • 3
    \$\begingroup\$ Where is the “with programming APIs” part here? \$\endgroup\$ – manatwork Feb 4 '14 at 10:41

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