Make a PNG image with "Hello World!" with programming APIs, in the shortest code possible

50

16

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.

figgycity50

Posted 2014-02-01T19:52:22.007

Reputation: 727

Question was closed 2016-04-26T22:57:55.160

Does it have to output Hello World or Hello World!? – The Guy with The Hat – 2014-02-02T12:55:44.503

3@ryan Hello World! is the right one. – figgycity50 – 2014-02-02T14:52:28.823

3A 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!'. – timxyz – 2014-02-03T11:27:47.367

@timxyz Clearly not if you want the result to appear correctly on my browser! – bye – 2014-02-03T21:56:29.957

9@poldie Your browser changes PNG based on how the pixels got in there? – Christopher Creutzig – 2014-02-04T02:22:04.853

@timxyz: Arguably the extra stroke in violates the "nothing else" requirement. – Mechanical snail – 2014-02-04T03:55:43.823

2@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. – Victor Stafusa – 2014-02-04T05:24:05.537

@timxyz No, it changes the characters you want changed into a PNG. – bye – 2014-02-04T08:43:36.557

Answers

8

Sage notebook, 22

text("Hello world",0j)

boothby

Posted 2014-02-01T19:52:22.007

Reputation: 9 038

Does this produce a PNG image? – Timtech – 2014-02-04T01:02:04.480

Yup. That's why I specified 'notebook', the CLI pops a window open, but the notebook saves a .png for the browser to display. – boothby – 2014-02-04T06:31:18.400

58

Bash + ImageMagick: 35 33

Default font, default text size, default colours:

convert label:Hello\ world! a.png

and here's the result:

Hello, World!

Thanks to DigitalTrauma and sch for the help :-D

r3mainer

Posted 2014-02-01T19:52:22.007

Reputation: 19 135

14

Great!! :-) I wanted to write something like that. Unbeatable! All the esoteric languages will finally get their ass kicked today! :-)

– Tomas – 2014-02-01T20:57:14.807

1You can shave off one char by using single-quotes: convert 'label:Hello world!' a.png – Digital Trauma – 2014-02-01T22:05:18.860

7convert label:Hello\ world! a.png is even shorter. – sch – 2014-02-01T22:10:28.393

@sch - True - I was thinking the ! needed escaping when unquoted, but it only needs escaping when in double quotes. – Digital Trauma – 2014-02-01T22:12:08.583

1@DigitalTrauma, it doesn't necessarily means escaping when in double quotes. It's just that here it was !" (which is looking for an event starting with " in the history). – sch – 2014-02-01T22:14:28.770

5Also, if you make it sh, you don't have to worry about ! and you make it even less esoteric. – sch – 2014-02-01T22:17:29.470

Would it be valid to save the image with the filename 'a'? – Vortico – 2014-02-02T08:23:13.760

@Vortico: I think convert looks at the extension to figure out what format to save in. Otherwise you would have to specify PNG explicitly, which is longer than the extension. – Mechanical snail – 2014-02-02T09:44:09.397

Save one character if you change ".png" to ".ps" to save to postscript – alexyorke – 2014-02-03T13:07:34.543

1@alexy13 The task is to create a png. – Christopher Creutzig – 2014-02-04T02:23:27.937

3Frankly I don't think bash qualifies as a programming language here or that convert qualifies as an API. But in this case I'd throw GNU hello in and bring this contraption down to 29 with: convert label:"\hello`" h.png` – Arnaud Meuret – 2014-02-04T09:43:43.217

3Why do you even care for the extension of the file? Just call it a and the mime type will do the rest. – Pierre Arlaud – 2014-02-04T12:52:34.407

@ArnaudMeuret GNU hello is not very standard, e.g. it's not present on our debian server. – Tomas – 2014-02-05T09:49:27.110

@ArlaudPierre 1) convert relies on the extension to choose the output format 2) Mime Type resolvers rely mostly on file extensions; in rare cases magic cookies – Arnaud Meuret – 2014-02-06T22:04:18.360

45

Mathematica 28 27

This creates and exports the sentence, "Hello world!", as a PNG image. 1 char saved by Mechanical snail.

".png"~Export~"Hello world!"

Testing

This imports the PNG image. The image was enlarged by dragging the image box handles.

png

DavidC

Posted 2014-02-01T19:52:22.007

Reputation: 24 524

427 bytes: ".png"~Export~"Hello world!" – Mechanical snail – 2014-02-02T07:57:53.950

8Wow, two snails in a race! :-) – Tomas – 2014-02-05T09:50:33.333

31

HTML, 1494

I know this won't win, but I didn't see this here before.

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEIAAAANEAYAAABNymQSAAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAACXZwQWcAAABCAAAADQDi7GgjAAADvElEQVRYw+1YPa9pTRReRIJKEPHdiEJBoVVIRCenpCJBfCQEDeI3qAh/QCIRUchp0KCUaISgROO7QMRHSGS9xX73u+85592Xe47uepqJZ9bMrFnzrJm1MfBfwAsvAADzM8HhcDgczv2Bj9r9KRgMBoPBeJz/2/DdODwaV+bjU77wN+AliBc+4OmCmE6n0+kUwGKxWCwWAKFQKBQKAfR6vV6vBygWi8Vi8efr7Ha73W4H4HK5XC4XgFQqlUqlADKZTCaTAbjdbrfbTdndg8FgMBgMANVqtVqtUnwikUgkEgBisVgsFgOQFRfZkny/3+/3+z/3i7zC8/l8Pp8HkEgkEomE3u/tdrvdbgEcDofD4aDirVar1Wo1QCaTyWQyj8f1iyAul8vlcqEco2tJu88IBoPBYBDA7/f7/X6A5XK5XC4ByuVyuVwGSCaTyWQSYDAYDAaD7wsiHo/H43EAFovFYrEAxuPxeDwGGI1Go9GI4skDvYe3t7e3tzeAer1er9cpvtlsNptNAC6Xy+VyAYbD4XA4pPwnea1Wq9Vqn+dXu91ut9sAjUaj0WjQ28VisVgsRgl0MplMJhOAXq/X6/UAOp1Op9OhH8/j8Xg83i8EfgKbzWaz2XgXdHYCgUAgEJBfLfQtkQFfx/+/V195uVwul8sRV6vVarX6ak8IEVGpVCqVyvv7IQKISBwsIpHBiBqNRqPRIBIHiJhOp9PpNGIqlUqlUoihUCgUCj3PL3Kf6/V6vV7fjwNxAyEuFovFYvHVnuTp4moymUwmE/Wb9f0c/b3iut1ut9sFIDZO9c9ms9lsBsDn8/l8/s/Xu1c932632+12fx6dTqfT6QD2+/1+vwcolUqlUgnAaDQajUYAs9lsNpsBstlsNpulMjISiUQikef7JRKJRCLRfb+ZTCaT+ZuH/95XCflU/odHM/9Ru3A4HA6HEaPRaDQaRbxer9frFbHVarVaLURCMFSm3MsAOt7j8Xg8HkSfz+fz+RDP5/P5fEY8nU6n0wnR6/V6vV6qfRSBQCAQCFAZXCgUCoUC4vF4PB6PiMRBISoUCoVCgUg8nc/zi27/dP3kPHa73W63IxKCRjwcDofDAZGoWejnrdVqtVrtl/mfLQiiyEG02Ww2m416QogiB/H9/f39/f3xDdPx5DpOp9PpdCISxRfVkjxp9ygqlUqlUqHWm8/n8/mc6ievWKvVarVa6ff/Xb/+VBCbzWaz2VCCIOOtUqlUKhViLpfL5XKPx5XxseOFvx2v/yFe+ICXIF74gH8A5ia8gOmPT18AAAASdEVYdGxhYmVsAEhlbGxvIHdvcmxkIbl+3UEAAAAASUVORK5CYII=

Well apparently StackExchange will not allow data links so you must copy & paste it into your browser's address bar.


@squeamish ossifrage got it down to 176:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAC0AAAAHAQAAAAC0VvlnAAAAOklEQVR4nGLIv/57/+9rDGDqAkPOterN0YIM3KG712pdZcgI3bW26gZD9lUwlRNWW1wtAAAAAP//AwCcyhjs3+7tWQAAAABJRU5ErkJggg

114

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACUAAAAEAQAAAAAhFcs9AAAAIElEQVQIHWOJit1f7szy58v9DAWWPaEMP7hZHHgi1aMB

@primo got it down to 112:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACYAAAAEAQAAAADKInA+AAAAH0lEQVR4nGOIj///7wVDZOBdoQ4G4dCrYWuA7JNiHQ

Chloe

Posted 2014-02-01T19:52:22.007

Reputation: 411

This isn't HTML. <html><img src="data:..."> is HTML. This is just a URI. – hobbs – 2017-07-17T04:42:04.537

http://codegolf.stackexchange.com/a/19929/15891 – OrangeDog – 2014-02-03T13:39:00.620

5

You can easily compress this to 258 characters: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEIAAAANCAAAAAA4MeeNAAAAdklEQVR4Ac2QUQqFQAwDc9I5Ze6XB8HXXRFE8Mdg+zGEwa7yOurXdUb30ap+UIHkUSDRGg5K5D+SdRQ8iqbdTjmk043ihcQU9r8Y1aEY2A2Y1cte4KRIc1UYg58oIFYuhwQ5UgYp+yF+8Jz1Qgapsz/n23xE8QNxUbxuz+WqjAAAAABJRU5ErkJggg== http://pastebin.com/5TQzNv3D

– MMM – 2014-02-03T16:03:50.040

2@squeamish ossifrage got it down to 176: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAC0AAAAHAQAAAAC0VvlnAAAAOklEQVR4nGLIv/57/+9rDGDqAkPOterN0YIM3KG712pdZcgI3bW26gZD9lUwlRNWW1wtAAAAAP//AwCcyhjs3+7tWQAAAABJRU5ErkJggg. Must strip the hidden spaces. – Chloe – 2014-02-04T03:48:51.667

166 chars: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAD8AAAAFAQAAAADaxQm5AAAAM0lEQVQIW2Mo0a+w3iKQz8NQ+r56r+71/BIGlnwg40J+GUhkz9lr78sYShQ57O6IKfIAAJypEkNzAX3zAAAAAElFTkSuQmCC http://codegolf.stackexchange.com/a/19941/12800

– MMM – 2014-02-04T09:50:20.083

1143 chars: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACUAAAAEAQAAAAAhFcs9AAAAIElEQVQIHWOJit1f7szy58v9DAWWPaEMP7hZHHgi1aMBegAI03/J+AQAAAAASUVORK5CYII= – luxcem – 2014-02-04T10:54:15.943

149 characters (retaining mixed case "Hello world!"): data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACYAAAAFAQAAAAABfqObAAAAJklEQVR4AWOIjv//7wWzBiNfnzzzc3klmY9MbgwMHCxMLGy6B3QAmcwIOOO9CusAAAAASUVORK5CYII – Spire – 2014-02-06T02:13:00.460

1I got it down to 135 characters! data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACUAAAAEAQAAAAAhFcs9AAAAIElEQVQIHWOJit1f7szy58v9DAWWPaEMP7hZHHgi1aMBegAI03/J+AQAAAAASUV= (btw, that's 83bytes for a png image that says "Hello World", thought I might throw that out there!) – Tim Groeneveld – 2014-02-06T05:15:42.723

@timgws You can chop the last = off. It's only padding. In fact, you can chop off egAI03/J+AQAAAAASUV= to yield 114: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACUAAAAEAQAAAAAhFcs9AAAAIElEQVQIHWOJit1f7szy58v9DAWWPaEMP7hZHHgi1aMB – Chloe – 2014-02-06T06:59:22.603

1

@Chloe the IEND is required by the PNG Specification: "A valid PNG image must contain an IHDR chunk, one or more IDAT chunks, and an IEND chunk." It further states that any compliant decoder must treat any missing critical chunks as an error.

– primo – 2014-02-06T09:07:50.497

2

Valid png format - 140 bytes http://codepad.org/O0TwZbKF. No IEND - 124 bytes: http://codepad.org/QdutLeK1. No IEND, no IDAT crc32, no zlib adler32 - 112 bytes: http://codepad.org/R4srHxhh.

– primo – 2014-02-06T10:29:05.093

28

C# - 168 chars

C# is better! ;)

using System.Drawing;class a{static void Main(){var s=new Bitmap(99,9);Graphics.FromImage(s).DrawString("Hello world!",new Font("",5),Brushes.Red,0,0);s.Save(".png");}}

Saves as .png in the current directory.

Rule abuse:

  • Minimum font/image size has not been specified, so I settled for the minimum readable ;)
  • Filename is empty (only extension!), but it works flawlessly.

To mirror the Java answer, here is the indented code:

using System.Drawing;
class a
{
    static void Main()
    {
        var s = new Bitmap(99, 9);
        Graphics.FromImage(s).DrawString("Hello world!", new Font("", 5), Brushes.Red, 0, 0);
        s.Save(".png");
    }
}

.net's API is a lot cleaner.

NothingsImpossible

Posted 2014-02-01T19:52:22.007

Reputation: 381

Thanks for the suggestion of making the file call just .png. :) – Victor Stafusa – 2014-02-01T22:08:46.770

I don't see how this is much cleaner than the Java one. – user253751 – 2014-02-03T01:48:23.140

It is much cleaner than the Java one. – uSeRnAmEhAhAhAhAhA – 2014-02-03T08:10:26.183

6@immibis I can point the following: Character count is 28% lower, because of: no checked exceptions(throws Exception); the var keyword; Save is a method of the image itself, in contrast to javax.imageio.ImageIO.write(b...; file type is automagically inferred from the extension, no need to specify a separate "png"; No need to instantiate a File object as in new java.io.File("p"); Main method doesn't need to receive a parameter. The general idea is the same: create an image, use Graphics to write to it, and save it - but here it is done with more clear and succint method calls. – NothingsImpossible – 2014-02-03T09:33:41.830

But C# doesn't have wildcards in generics. And anonymous classes. And return type covariance. And some other things I'm too lazy to recall. – Display Name – 2014-02-04T23:47:37.320

@Sarge Yes we do, reflection for when we need them. Yes we do. What do you mean? Covariance works in C#. – It'sNotALie. – 2014-02-05T13:55:48.273

@It'sNotALie. http://en.wikipedia.org/wiki/Covariant_return_type

– Display Name – 2014-02-05T14:03:21.253

@Sarge I could see how that could sometimes be useful but we'd use generics in C# for this instead. Sometimes we really do want it to just return that type. – It'sNotALie. – 2014-02-05T16:40:03.773

can the use of f# reduce char count? – Rozuur – 2014-02-07T21:22:27.480

@Rozuur I guess it should, but I'm no expert in F# so I didn't use it. – NothingsImpossible – 2014-02-08T23:54:27.957

25

Linux shell + various utilities: 48 bytes

My first thought was to print Hello World! in the console, and then take a screenshot (after a small delay to avoid the race condition) using scrot:

echo Hello World\!;scrot -d1

(28 bytes). Unfortunately, this fails the "and nothing else" requirement: it will generally show other things like window decorations.

So instead, do it inside a full-screen xterm. This covers up any other windows and hides window decorations. It also satisfies the background-color requirement, since xterm defaults to a white background.

Because xterm displays a black cursor, we also need to tell it to hide the cursor. That can be accomplished using a terminal escape sequence: ESC [ ? 2 5 l.

The option to make it full-screen is -fullscreen. However, it seems to work if you abbreviate the option to the shortest unambiguous possibility, -fu, saving 8 bytes.

The final code (48 bytes) is:

xterm -fu -e 'echo \x1b[?25lHello World!;scrot -d1'

(where \x1b denotes a literal ESC character, which takes 1 byte). By default, scrot writes the screenshot to a timestamped PNG file in the current directory.

It works on my system:

output

Mechanical snail

Posted 2014-02-01T19:52:22.007

Reputation: 2 213

You missed some scrot options - I often use -s (interactively select window), but in this case you can use -u to grab the current (focused) window only. – bryn – 2014-02-04T19:36:47.673

19

Processing, 38 37

This might be considered cheating, but:

text("HeΠo World!",9,8);save(".png");

enter image description here


38 char solution:

text("Hello World!",9,9);save(".png");

Saves this image as .png:

enter image description here

The Guy with The Hat

Posted 2014-02-01T19:52:22.007

Reputation: 778

14

Java - 340 339 292 261 239 236 233 chars

This outputs a file just called p with a transparent background and white text:

import java.awt.image.*;class R{public static void main(String[]y)throws Exception{BufferedImage b=new BufferedImage(80,9,2);b.getGraphics().drawString("Hello World!",5,9);javax.imageio.ImageIO.write(b,"png",new java.io.File("p"));}}

Here a properly-indented version. Should be pretty clear what is going on:

import java.awt.image.*;

class R {
    public static void main(String[] y) throws Exception {
        // 2 = BufferedImage.TYPE_INT_ARGB
        BufferedImage b = new BufferedImage(80, 9, 2);
        b.getGraphics().drawString("Hello world!", 5, 9);
        javax.imageio.ImageIO.write(b, "png", new java.io.File("p"));
    }
}

You might argue that white text in a transparent background is awful for reading and the file being called just p without the .png extension is awful too. So this longer variant version with 290 chars use red text and outputs a file called p.png:

import java.awt.image.*;import java.awt.*;class R{public static void main(String[]y)throws Exception{BufferedImage b=new BufferedImage(80,9,2);Graphics g=b.getGraphics();g.setColor(Color.RED);g.drawString("Hello world!",5,9);javax.imageio.ImageIO.write(b,"png",new java.io.File("p.png"));}}

That properly-indented:

import java.awt.image.*;
import java.awt.*;

class R {
    public static void main(String[] y) throws Exception {
        // 2 = BufferedImage.TYPE_INT_ARGB
        BufferedImage b = new BufferedImage(80, 9, 2);
        Graphics g = b.getGraphics();
        g.setColor(Color.RED);
        g.drawString("Hello world!", 5, 9);
        javax.imageio.ImageIO.write(b, "png", new java.io.File("p.png"));
    }
}

Victor Stafusa

Posted 2014-02-01T19:52:22.007

Reputation: 8 612

Do you need to use BufferedImage, or does Image work? – Justin – 2014-02-01T21:43:52.627

@Quincunx I need BufferedImage. The write method parameter is of type RenderedImage, so it won't compile with just Image without some cast. – Victor Stafusa – 2014-02-01T21:46:02.953

Ah, that makes sense. – Justin – 2014-02-01T21:48:39.653

technically you can call the file "p" and still match the requirements, since the saved file will be a png even if the filename doesn't reflect that. – Tim B – 2014-02-02T00:07:53.477

@TimB Ok, did that. – Victor Stafusa – 2014-02-02T05:20:12.383

@Victor, am I correct in thinking that throws Exception throws an exception if something goes wrong in public static void main()? Can you add throws Exception to other methods/extension methods in Java? I haven't seen this in C# before. – uSeRnAmEhAhAhAhAhA – 2014-02-03T08:12:27.647

2@user-12506 Yes, you can add throws Exception to other methods, but the compiler enforce this rule: if you call a method that might throw a Throwable that is not a RuntimeException or an Error, then the compiler will require that you either catch or rethrow it, and this includes Exception. Further, although it is allowed, declaring throws Exception is a bad programming practice, because Exception is way too generic to be effectively handled in practice and rethrowing it just spread the bad practice to elsewhere. – Victor Stafusa – 2014-02-03T08:22:39.350

@Victor thanks for explaining. I thought it looked like a cool way of handling exceptions. Good to know :) – uSeRnAmEhAhAhAhAhA – 2014-02-03T08:24:21.533

1@user-12506 It's called checked exceptions. Look it up. :) – ntoskrnl – 2014-02-03T14:05:21.467

@ntoskrnl it's unlikely I'll look into it too much. I've never been a big fan of Java's syntax. IAmMoreOfACSharpKindaGuy - notAnEverythingSeemsToBeginWithALowerCaseCharacterKindaGuy(noOffenseIntended) - :) – uSeRnAmEhAhAhAhAhA – 2014-02-03T20:44:16.323

I've skinned over a few pages about checked exceptions and just from reading the first paragraph or two, everybody seems to talk about it in a negative undertone. A little disappointing. I will look into this more when I wake up. – uSeRnAmEhAhAhAhAhA – 2014-02-03T20:52:54.930

1@user-12506 Not everything is lower case in Java – classes are CamelCase just like in C#. Methods and fields are lowerCamelCase, and packages are alllowercase. Makes it somewhat easier to determine what a name refers to, in my opinion. – ntoskrnl – 2014-02-03T20:55:50.687

1@ntoskrnl Yeah, things can and do get a bit confusing when classes, methods and other things are camel case. But in the case of methods, it either begins with an Identifier that allows you to immediately recognize it as such, and if you're calling a method, you'll know right away that it's a method because it ends in (); or (somethinginHere);. In C#, most things are CamelCase (when I'm typing anyway), except for variables. and (most) types. Either way, I'm going to have to get used to Java eventually. I've just started my first serious Android project. – uSeRnAmEhAhAhAhAhA – 2014-02-03T21:02:50.527

Save 1 char - Heỻo world! – Timtech – 2014-02-04T00:50:46.887

2@Timtech, at least for me ỻ was rendered as a square in the resulting image. Here in the browser (using firefox), it renders as a box containing 1EFB. I strongly suspect that this is not portable. – Victor Stafusa – 2014-02-04T05:19:56.440

14

Fortran 90, 104 94 bytes:

Aight, game on. Fortran 90, using the g2 graphics library and implict typing, so "d" is a real:

d=g2_open_gd('h',80.,12.,g2_gd_png)
call g2_string(d,1,1,'Hello world!')
call g2_close(d)
end

Needs to have the g2 library installed, then just compile with

gfortran -lg2 -fsecond-underscore p.f90

Thanks Kyle Kanos for suggesting to drop "program p"!

I'm pretty satisfied that I beat C#, C + Cairo, Java, Javascript, Python and Ruby! And now also Perl!

Example output: example PNG produced by Fortran

semi-extrinsic

Posted 2014-02-01T19:52:22.007

Reputation: 641

1This can be shortened to 93 characters by eliminating program p as it is not necessary (the only necessary part of a F90 code is end). – Kyle Kanos – 2014-02-04T16:39:23.960

11

R, 52 39 chars

png()
frame()
text(.5,1,"Hello World!")

Saves as Rplot001.png in current directory. To be run as a script in non-interactive (batch) mode.

Thanks to Sven Hohenstein and Michael Hoffman for updates!

Tomas

Posted 2014-02-01T19:52:22.007

Reputation: 2 333

I count 52 characters... – Sven Hohenstein – 2014-02-02T02:47:50.463

@SvenHohenstein sorry you are correct. I made a mistake while attempting shorter versions (they didn't work). – Tomas – 2014-02-02T03:08:54.910

1dev.off() is unnecessary when you run this as a script instead of interactively. – Michael Hoffman – 2014-02-03T18:12:19.127

10

PHP + gd2 - 86 bytes

<?imagestring($i=imagecreatetruecolor(97,16),4,2,0,'Hello world!',65535);imagepng($i);

imagecreatetruecolor is used instead of the shorter imagecreate, because colors can used directly without having to allocate them with imagecolorallocate. 65535 corresponds to hex color #00FFFF, a.k.a. cyan. I could have used 255 for blue, but it's fairly hard to see on a black canvas.


If the requirement that the background must be white or transparent is to be strictly enforced, I think the best that can be done is 98 bytes:

<?imagestring($i=imagecreatetruecolor(97,16),4,2,0,'Hello world!',imagefilter($i,0));imagepng($i);

The 0 sent to imagefilter is the value of the constant IMG_FILTER_NEGATE, which of course negates the image. The result, 1, is then used as the paint color (#000001):


Another option at 108 bytes:

<?imagestring($i=imagecreatetruecolor(97,16),4,2,imagecolortransparent($i,0),'Hello world!',1);imagepng($i);

Setting black to be transparent, and drawing with #000001 instead.


PHP + No Library - 790+ bytes

<?
echo pack('CA3N2',137,'PNG',218765834,13);
echo $ihdr = pack('A4N2C5','IHDR',45,7,1,0,0,0,0);
echo hash('crc32b',$ihdr,true);
$data =
  '--------0  0      0 0                        0    0 0---'.
  '--------0  0      0 0                        0    0 0---'.
  '--------0  0  00  0 0  00    0   0  00  0 0  0  000 0---'.
  '--------0000 0  0 0 0 0  0   0   0 0  0 00 0 0 0  0 0---'.
  '--------0  0 0000 0 0 0  0   0 0 0 0  0 0    0 0  0 0---'.
  '--------0  0 0    0 0 0  0   0 0 0 0  0 0    0 0  0  ---'.
  '--------0  0  000 0 0  00     0 0   00  0    0  000 0---';
$bytes = join(array_map(chr,array_map(bindec,str_split(strtr($data,' -',10),8))));
$cmp = gzcompress($bytes);
echo pack('N',strlen($cmp));
echo $idat = 'IDAT'.$cmp;
echo hash('crc32b',$idat,true);
echo pack('NA4N',0,'IEND',2923585666);

Ahh, that's better. No bloat; exactly as much as required, and not a chunk more.
The result is this 109 byte png:

Or, URI encoded (which seems to be trending...) at 168 bytes:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAC0AAAAHAQAAAAC0VvlnAAAANElEQVR4nGPIv/7//+8LDFAq51r15mgBBu7Q3Wu1LjBkhO5aW3WBIfsqkLrBkBNWW1wtAACw2RlgLInRogAAAABJRU5ErkJggg

Supposing we wanted to cut that down a bit more, let's say we replace the data string with this:

$data =
  '--------0  0      0 0                    0   0 0'.
  '--------0  0  00  0 0 000   0   0 000 00 0 000 0'.
  '--------0000 0 00 0 0 0 0   0 0 0 0 0 0  0 0 0 0'.
  '--------0  0 00   0 0 0 0   0 0 0 0 0 0  0 0 0  '.
  '--------0  0  000 0 0 000   00 00 000 0  0 000 0';

(and update the header to the new dimensions, 40x5), the output would be this 96 byte png:

Which URI encodes to 150 bytes:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAFAQAAAAAft5MoAAAAJ0lEQVR4nGPIv/7//y6GnCvlLosYuEJLQ1cxZF4tDV3NkBNS5LoIANqBDTt5Av0NAAAAAElFTkSuQmCC

I think that's about as small as you're going to be able to get, and still be considered "human readable".


Further Analysis

You may have noticed that we've been toting along an extra byte at the beginning of each scanline (denoted by --------). This isn't solely for decoration. Each byte specifies the filtering used by each scanline. According to the PNG specification, "Filtering transforms the PNG image with the goal of improving compression." So let's try that.

The are five different filtering operations which can be applied independently to each scanline. The PHP implementation that I used for each can be seen here: http://codepad.org/xCQpBPC3 where $bytes represents the raw bytes for the current scanline, and $prior represents the raw, unfiltered bytes for the scanline above the current.

Let's start with the first 45x7 image. Seven scanlines each with 5 different filterings makes 78125 different possibilities to grind through. The initial encoding of the data block was 52 bytes in length, and after a bit of grinding zlib found a one byte improvement using filtering pattern [1, 1, 1, 1, 0, 0, 0] (that is, the first four scanlines with Sub filtering, and the last three unfiltered). The result is this 108 byte png:

Which of course looks identical to the last. But I'm not convinced that zlib is producing the best possible encoding, and I think i have good reason to be skeptical. I decided to try AdvanceComp (which uses the same DEFLATE engine used for 7-zip), and Zopfli, an implementation which claims to "find a low bit cost path through the graph of all possible deflate representations." Sure enough, Zopfli mananged to compress the same data data pattern [1, 1, 1, 1, 0, 0, 0] down to 50 bytes, producing this 107 byte png:

Once again, visually identical. (As a point of interest, it should probably be mentioned at this point that AdvanceComp with the setting -z3 (compress-extra (7z)) didn't manage to find anything shorter than 60 bytes - the data was left uncompressed. It seems it refuses to compress anything this short). The above URI encodes to 165 bytes:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAC0AAAAHAQAAAAC0VvlnAAAAMklEQVR4AWPMz9Bg+HMVRuVkLbVYsZWR2yvtU+0yhozQXWurLjBkXwVSNxhywmqLqwUA+IMVKa7QjrYAAAAASUVORK5CYII

Fully 11 bytes shorter than squeamish ossifrage's attempt at more or less an identical image.

Onwards to the 40x5 image. Five lines with 5 filterings each means we only have 3125 possibilities this time. The original encoding was 39 bytes in length, and with a bit of grinding, zlib found quite a few 38s. The one I've chosen is [1, 0, 0, 2, 0], which contains the largest number of unfiltered lines, and Sub and Up filters on lines 0 and 4, which are the simplest. Zopfli wasn't able to improve this result any further. The result is this 95 byte png:

Which URI encodes to 149 bytes:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAFAQAAAAAft5MoAAAAJklEQVR4nGPMz9Bg2M2Qc6XcZREDV2hp6Cqm+AYGBkaGnJAi10UAju4JJ/1zkEIAAAAASUVORK5CYII

You might be tempted to think that the last 18 or so bytes of this aren't necessary. After all, this 121 byte URI will still display correctly, at least in Chromium:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAFAQAAAAAft5MoAAAAIUlEQVR4nGPMz9Bg2M2Qc6XcZREDV2hp6Cqm+AYGBkaGnJAi10U

But if you save it to a file, it will break in very many image viewers. In fact, any compliant decoder is required to report an error. So what have we chopped off?

From end towards beginning:

  • 4 bytes - CRC32 for IEND chunk (always 0xAE426082)
  • 4 bytes - IEND chunk marker (always IEND)
  • 4 bytes - IEND chunk length (always 0x00000000)
  • 4 bytes - CRC32 for IDAT chunk
  • 4 bytes - Adler32 for zlib data
  • 1 byte - Stop marker for zlib data

Additionally adjusting the IDAT length marker down by 5 (to compensate for the bytes we deleted) seems to "fix" the image in Windows Previewer.

primo

Posted 2014-02-01T19:52:22.007

Reputation: 30 891

The question state that it should have a white or transparent background. I myself think that the rules should be relaxed to allow black too, but for now, it fails the spec at this point. – Victor Stafusa – 2014-02-02T05:25:43.397

@Victor Unfortunately, the gd2 library - as exposed by PHP, at least - doesn't have an option to specify the default color. Setting black to be transparent does work, though. Or simply to negate the entire image before adding the text. – primo – 2014-02-02T06:08:34.217

9

Octave, 47

axis('off')
title("Hello World!")    
print -dpng x.png

marczellm

Posted 2014-02-01T19:52:22.007

Reputation: 191

-1 until you remove the axes (I think it would be axis('off'), not sure, see here) It would also be nice if you attached the generated output as well.

– Nick T – 2014-02-06T20:58:06.700

+1 now :D why bother keeping the bigger/broken ones though... – Nick T – 2014-02-11T03:28:37.927

9

C + Cairo: 238 221 202 bytes

#include <cairo/cairo.h>
main(){cairo_surface_t*s=cairo_image_surface_create(0,99,50);cairo_t*c=cairo_create(s);cairo_move_to(c,0,9);cairo_show_text(c,"Hello world!");cairo_surface_write_to_png(s,"o");}

$ cc `pkg-config cairo --libs --cflags` mini.c && ./a.out && display o

Here is the un-minified version:

#include <cairo/cairo.h>
void main (int argc, char* argv[])
{
    cairo_t *cr;
    cairo_surface_t *surf;
    surf = cairo_image_surface_create (0, 99, 50);
    cr = cairo_create (surf);
    cairo_move_to (cr, 0, 9);
    cairo_show_text (cr, "Hello world!");
    cairo_surface_write_to_png (surf, "out.png");
}

Best enjoyed while listening to this song :)

Torkel Bjørnson-Langen

Posted 2014-02-01T19:52:22.007

Reputation: 421

You can save some chars by removing the type declaraction, i.e. the void for main and the int for x, and possibly the char for **a (I'm not sure about the last one though). In C the type is default to be int if no type is specified, and you don't necessarily need to return a value for int main. This may produce compilation warning messages, but not errors and the program will still work. – user12205 – 2014-02-02T10:54:57.420

1Shaved 17 bytes by removing the pointless #define C(n,...) …. Also, main returns int, not void (and as of C99 it's not required to explicitly "return 0" at the end of main; the implementation implicitly adds it for you). – Quuxplusone – 2014-02-03T02:09:13.277

@ace Thank you! I had forgotten that types defaults to int i C. But it looks like it's all-or-nothing; you can't have some variables in the call signature use default types while others don't. So I just changed it to main() and hardcode the filename to o instead of passing it to main. Now it's smaller than the Java version :) – Torkel Bjørnson-Langen – 2014-02-03T02:35:23.140

@Quuxplusone Thanks! Did not know that C99 add an implicit return 0 to main if not already there. That was why I choose to (incorrectly) return void instead of int. Since you got rid of the vararg macro, C99 is not required to compile the code anymore. <rant>Which makes it more portable to compilers who's vendors has chosen to not implement C99 [read: Microsoft] :)</rant> – Torkel Bjørnson-Langen – 2014-02-03T02:45:52.210

9

Javascript! 105 104 101

c=document.createElement('canvas');open(c.toDataURL(c.getContext('2d').fillText('Hello world!',0,9)))

Outputs this size-optimized and pretty image:

Hello world!

jas

Posted 2014-02-01T19:52:22.007

Reputation: 91

9

make a PNG image that says "Hello world!" …

You'll hopefully excuse my very loose interpretation of the above requirement. :)

OSX bash:

printf '"Hello world!" and nothing else on a distinguishable background using only an image making API (eg ImageMagick) and return the image.' > hello.png
say -f hello.png

Denis de Bernardy

Posted 2014-02-01T19:52:22.007

Reputation: 270

But it's not a PNG image, is it? – Ruslan – 2014-02-04T10:58:17.137

2Well, it's technically a .png file, and it does say what is needed. :-) – Denis de Bernardy – 2014-02-04T11:26:36.363

1PNG image is not "ASCII text, with no line terminators", as said by file. It's a .png file, but not PNG image. – Ruslan – 2014-02-04T11:28:24.217

2Fail point. I didn't manage to find a way to fool the file utility by pre-pending the whole thing with the relevant code (i.e. decimals 137 80 78 71 13 10 26 10). Perhaps you might know of a way? ;-) – Denis de Bernardy – 2014-02-04T12:27:59.920

seems you'd need to make full header with all information like dimensions, palette etc., because file prints this information. With your relevant code it only says "data". – Ruslan – 2014-02-04T14:47:45.333

8

TI-BASIC, 22

Text(0,0,"HELLO WORLD!

White background. Use TI-Connect if you wish to retrieve it from the calculator. Resulting PNG:

enter image description here

Timtech

Posted 2014-02-01T19:52:22.007

Reputation: 12 038

2Why the downvote? Produces a .PNG image with the text HELLO WORLD! – Timtech – 2014-02-01T20:15:11.797

6While I don't know for definite, I'm going to assume that Ti-Connect generates and outputs the PNG here. – skeevey – 2014-02-01T20:22:57.503

1@slackwear That's for transfer, it's already on the calculator. – Timtech – 2014-02-01T20:24:51.527

8There's a PNG file on the calculator? – skeevey – 2014-02-01T20:29:17.947

1@slackwear Yes, used by the graph. System variables are Pic1 through Pic9. PNG file is 94 by 62. – Timtech – 2014-02-01T21:48:19.157

22The calculator may have a 94 by 62 bitmap, stored in its own uncompressed format. I'll put my money on Ti-Connect converting it into a PNG when it's retrieved. – Tobia – 2014-02-01T23:00:02.547

18Can you post the resulting PNG? – Victor Stafusa – 2014-02-02T05:44:40.897

9This does not produce a .png image in the calculators memory. Retrieving it from the calculator converts it to a .png image. – Rolf ツ – 2014-02-04T13:13:51.713

11Personally, I think this is like displaying Hello world! on my computer screen and then saying "Oh, but I can easily convert it to a .png by taking a screenshot of it!" – The Guy with The Hat – 2014-02-07T01:12:10.920

8

Ruby, 138

I'm golfing this hole with just my putter. (I chose a PNG library without fonts or a string draw method.)

require'chunky_png';i=ChunkyPNG::Image.new 34,4
136.times{|v|i[*(v.divmod(4))]=9*(0xb0fae0f02e0eae0ece00eae0f0f0bf0f2f&1<<v)>>v};i.save ?h

Actual output is 34x4 pixels. (Enlarged below.) This plots very, very small and nearly transparent hand-drawn chars onto a very small transparent background. Image is saved to a PNG file named h.

output

Darren Stone

Posted 2014-02-01T19:52:22.007

Reputation: 5 072

Would it be shorter in Ruby to store 0xb0... in Base64 and call a decode function? – Mechanical snail – 2014-02-02T10:10:28.763

1@Mechanicalsnail, I think that would require another require and then some, so the savings would be blown. I think. I did look at Base 36 (since that can be done with to_i alone) but that ends up a bit longer. – Darren Stone – 2014-02-02T10:14:18.130

8

BASH + RST + ImageMagick = 43 chars

Just for fun, here's another (quite ugly) one:

echo 'Hello World!'|rst2pdf|convert - a.png

Output:

Hello World!

Bach

Posted 2014-02-01T19:52:22.007

Reputation: 219

6

Gnuplot

Not really a true competitor, just for fun. (homepage)

#!/usr/bin/gnuplot
set terminal pngcairo
set output "hw.png"
set label "Hello\nWorld!"
unset xtics
unset ytics
set yrange [-1:+1]
plot -1 notitle

or, as a oneliner (thanks to Phil H): 74 characters

se te pngc;se ou "q.png";se la "Hello world!";se yr[-1:1];uns ti;pl -1 not

kebs

Posted 2014-02-01T19:52:22.007

Reputation: 756

1You can shorten each of those commands to the minimum unique stub - so set te works, and set out etc. And you only need the top line to make it executable in the terminal, which is not a usual code golf requirement. So: se te pngc;se ou "q.png";se la "Hello world!";se yr[-1:1];uns ti;pl -1 not - 74 chars – Phil H – 2014-02-04T16:31:44.293

@Phil H: yeah, sure, thanks ;-) Actually I knew commands could be shortened, but as this contrib wasn't really designed to compete with more efficient solutions, I thought I might as well show full length commands for educational purposes. – kebs – 2014-02-05T18:59:45.003

5

Perl, 95:

The whole command incantation:

perl -MGD::Simple -e'$i=new GD::Simple;moveTo$i 9,50;string$i "Hello world!";print$i->png'>.png

Because of (reasonable) module defaults, it's 95 characters (or 92 if single letter file name allowed).

On Windows we need to binmode STDOUT, the shortest way I think can be -M-encoding+ get rid of double colons:

perl -MGD'Simple -M-encoding -e"$i=new GD'Simple;moveTo$i 9,50;string$i 'Hello world!';print$i->png">.png

i.e. 105

enter image description here

user2846289

Posted 2014-02-01T19:52:22.007

Reputation: 1 541

5

Data URI: 446

Following Chloe's idea, applied some basic optimisation to the image.

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEEAAAALCAAAAAAFX7+TAAABBUlEQVQoz2P4Tylg+M8IohiRRUCIkC6E0sFlwhFBJo5kmPAdWSYmuTsgGa6y/3pMf/8yLYEJMcQwARXckmZi8YCawAAG//8L537ZxbEMaoKCwuvXCkogE2y0/nMyL1vC/BcmxKC6AqhAXurBc1kGkNUINzCBDIqFmsB84f//c8wgmcVs91j13d3U4EIMl0AKmM78/38GqJQXyQSWQ///H3sJMwGo7ALYhL/MQUq1wkK1cCEGsE+ZgSacAzKNkUxQ1/kygfEczBeKb18rKICDS4U58SUT8ye4EMQEBelHz+WAzEokE+6IM7HkI4ekLDgk/5cxnPrPK/4fLgQxARSSUdCQpBQAAFZKE8rQG60FAAAAAElFTkSuQmCC

Might be able to hack it further, but then would not be a perfectly conformant PNG and some viewers may not display it correctly.

OrangeDog

Posted 2014-02-01T19:52:22.007

Reputation: 157

Could you explain what kind of optimizations did you perform? – svick – 2014-02-03T15:48:52.790

PNG2, stripped optional chunks, optimal (not streamed) compression – OrangeDog – 2014-02-03T16:22:13.100

1If you reduce the font size slightly, remove the spacing, the actual binary size of the image would be smaller than some of the posted solutions. – OrangeDog – 2014-02-03T16:24:15.653

1@OrangeDog I got it down to 179: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAC0AAAAHAQAAAAC0VvlnAAAAOklEQVR4nGLIv/57/+9rDGDqAkPOterN 0YIM3KG712pdZcgI3bW26gZD9lUwlRNWW1wtAAAAAP//AwCcyhjs3+7tWQAAAABJRU5ErkJggg== – r3mainer – 2014-02-03T16:37:48.997

1@squeamishossifrage That doesn't load in Firefox for me. – OrangeDog – 2014-02-03T17:05:52.903

@OrangeDog Oops, should be 178. Try without the line break. – r3mainer – 2014-02-03T19:40:08.307

Still not working – OrangeDog – 2014-02-03T19:44:26.753

@OrangeDog: Stack Exchange comments seem to insert U+200B and U+200C into the comment. It works in Firefox once I strip them out. – Mechanical snail – 2014-02-04T03:45:08.823

Actually, the pad = characters are optional in Base64, giving 177 chars. – Mechanical snail – 2014-02-04T03:50:18.760

Can you add a working one to the answer? – OrangeDog – 2014-02-04T10:14:22.920

5

Python and Matplotlib - 66 65 chars

from pylab import*;title('Hello world!');axis('off');savefig('X')

Bit of whitespace, but it has the text and nothing else. File is saved as X.png:

enter image description here

Nick T

Posted 2014-02-01T19:52:22.007

Reputation: 3 197

from pylab import* – Timtech – 2014-02-03T23:04:14.777

@Timtech thanks, I'm a golfing noob clearly ;) – Nick T – 2014-02-04T00:36:10.087

I think you did a great job! – Timtech – 2014-02-04T00:47:33.077

4

Python with PIL 122 Chars

from PIL import Image,ImageDraw;d=Image.new("RGB",(70,9));i=ImageDraw.Draw(d);i.text((0,0),"Hello World!");d.save("a.png")

It could probably be much smaller but I haven't worked with PIL extensively and made this in 5 minutes.

Output: enter image description here

globby

Posted 2014-02-01T19:52:22.007

Reputation: 1 132

2You beat me to it by 10 minutes! With about 10 less chars! +1 :D Lose another char by going from 10 to 9 in your new, but you left out the !. – Aaron Hall – 2014-02-02T01:29:19.560

The question state that it should have a white or transparent background. I myself think that the rules should be relaxed to allow black too, but for now, it fails the spec at this point. – Victor Stafusa – 2014-02-02T05:26:01.913

2Couldn't you save lots of characters with from PIL import*? – Timtech – 2014-02-04T00:54:44.293

You would expect so, but you have to import them individually or else you get a not defined error from PIL import Image,ImageDraw;d=Image.new("RGB",(66,10));i=ImageDraw.Draw(d);i.text((0,0),"Hello World");d.save("a.png") NameError: name 'Image' is not defined – globby – 2014-02-07T21:14:44.270

4

Ghostscript command line incantation, 84 (i.e. Postscript) :

 gs -sDEVICE=png16 -oa.png -c '/ 72 selectfont 9 9 moveto(Hello world!)show showpage'

Missing font message is intentional ;-). And proper (i.e. not hidden) name for our PNG file, too :-)

user2846289

Posted 2014-02-01T19:52:22.007

Reputation: 1 541

3

Python, with 118 115 117 116 114 chars

Here's a full criteria passing with white text on alpha background at 116 114 chars!:

from PIL import Image,ImageDraw as D
m=Image.new("LA",(99,9))
D.Draw(m).text((9,0),'Hello World!')
m.save('r.png')

(You can replace newlines with semicolons - count is the same regardless.)

Aaron Hall

Posted 2014-02-01T19:52:22.007

Reputation: 141

ImageDraw as D, and then D.Draw saves 3 chars. – primo – 2014-02-02T03:02:06.007

3

Bash + DOT, 46 41

dot -Tpng<<<'graph{label="Hello World!"}'

This outputs a png into the standard output. It is saving a file, but I don't think that was a requirement.

Outout: Hello World!

Bach

Posted 2014-02-01T19:52:22.007

Reputation: 219

I we allow them be in ellipses, it can be 8 chars shorter... – Bach – 2014-02-02T10:57:17.633

1I am not sure whether you should add the bash chars as well for compiling this dot script. This adds dot -Tpng -op d, which is 15 chars. – Bach – 2014-02-02T11:01:48.670

You should count the command line options/argument if they are needed to make your program solve the task. Normally the dashes are not counted, so I think you only should add Tnpg, op and d to the character count (+7), and specify how to run it in the answer itself. – daniero – 2014-02-02T18:40:50.360

It can be better if you don't echo but instead write it in a file and run dot -Tpng on the file; it makes it 38, but I don't know if it counts... – Bach – 2014-02-02T19:28:02.120

Why would it not count? – daniero – 2014-02-02T20:13:55.950

Hans, I decided for fair play :) Golf it down to 41 with the improvement I suggested in my answer: dot -Tpng<<<'graph{label="Hello World!"}', I will delete my answer then. These derivations shouldn't be done in fair play and should stay as a comments to original answers. – Tomas – 2014-02-04T11:27:35.457

Updated according to Tomas' suggestion. – Bach – 2014-02-04T12:07:53.510

2

PureBasic - 128 chars

UsePNGImageEncoder()
CreateImage(0,99,24)
StartDrawing(ImageOutput(0))
DrawText(0,0,"Hello World!")
SaveImage(0,"a.png",4673104)

enter image description here

Not the shortest here, but I have to support my favorite Basic language :)

edit: just in case there is a complain about the black background, at 133 chars:

UsePNGImageEncoder()
CreateImage(0,80,16)
StartDrawing(ImageOutput(0))
DrawText(0,0,"Hello World!",0,-1)
SaveImage(0,"a.png",4673104)

enter image description here

Fozzedout

Posted 2014-02-01T19:52:22.007

Reputation: 121

2

Rebol/View 45

save/png %i to-image layout[h1"Hello World!"]  

writes a png image file named i into current directory

sqlab

Posted 2014-02-01T19:52:22.007

Reputation: 181

2

bash + netpbm: 31 chars

pbmtext "Hello world!"|pnmtopng

Will make:

Hello world by netPBM

F. Hauri

Posted 2014-02-01T19:52:22.007

Reputation: 2 654

2

HL-SDK

Posted 2014-02-01T19:52:22.007

Reputation: 137

2

Clever! Google charts is more readable ;) http://chart.googleapis.com/chart?cht=tx&chl=Hello%5C+world%21

– primo – 2014-02-05T01:15:12.047

1

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

gerrit

Posted 2014-02-01T19:52:22.007

Reputation: 296

1

EPL, 28

0,0,0,1,1,1,N,"Hello World!"

koko

Posted 2014-02-01T19:52:22.007

Reputation: 201

1Can you show the output? – The Guy with The Hat – 2014-02-04T13:22:42.117

1

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

MMM

Posted 2014-02-01T19:52:22.007

Reputation: 181

157, see: http://codegolf.stackexchange.com/a/19828

– primo – 2014-02-04T12:17:46.040

1

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

Adam Davis

Posted 2014-02-01T19:52:22.007

Reputation: 245

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. – Adam Davis – 2014-02-03T18:07:56.443

2Yeah, I wouldn't think using a URL shortener is very kosher for this. :-) – Chris Jester-Young – 2014-02-03T18:21:20.570

1I have a shorter solution: cat image.png > h.png where image.png is the result of curl http://goo.gl/a8ntA4 > image.png!!! – Farid Nouri Neshat – 2014-02-04T12:32:48.810

1

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

unclekyky

Posted 2014-02-01T19:52:22.007

Reputation: 11

1

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

krs

Posted 2014-02-01T19:52:22.007

Reputation: 160

1

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.

Ismael Miguel

Posted 2014-02-01T19:52:22.007

Reputation: 6 797

similar to http://codegolf.stackexchange.com/a/20019

– gnibbler – 2014-02-05T13:23:13.037

Similar, but WAAAAAAAAAAAAY shorter and a totally different website. dummyimage.com is bigger than placehold.it – Ismael Miguel – 2014-02-05T17:29:15.640

http://cambelt.co/300/Hello+world%21 and http://fakeimg.pl/99/?text=Hello+world%21 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. – manatwork – 2014-02-06T08:17:05.787

If you didn't downvoted, perhaps I could get enough reputation to make comments on other posts! – Ismael Miguel – 2014-02-06T09:34:23.427

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.) – manatwork – 2014-02-06T10:23:15.240

0

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

Guillaume

Posted 2014-02-01T19:52:22.007

Reputation: 101

Could you please post the resulted image? By the way, are all those spaces necessary in the code? – manatwork – 2014-02-04T10:44:50.777

image posted and useless spaces removed – Guillaume – 2014-02-04T12:53:23.283

0

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

user13426

Posted 2014-02-01T19:52:22.007

Reputation:

0

Shell + Graphviz (55)

echo 'graph{a[label="Hello World!"]}'|dot -Tpng>h.png

grim

Posted 2014-02-01T19:52:22.007

Reputation: 397

You can get away with echo 'graph{"Hello World!"}'|dot -Tpng>a.png, which is 44 characters... – Martin Broadhurst – 2015-07-11T15:23:30.813

...but you should really get rid of the ellipse, which you can do with echo 'graph{"Hello World!"[shape=plaintext]}'|dot -Tpng>a.png, which is 61 characters. – Martin Broadhurst – 2015-07-11T15:25:15.737

@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. – grim – 2014-02-07T22:21:18.193

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 – Timtech – 2014-02-08T00:19:34.427

alright, well I'm going to remove it then :) Thanks for getting the code formatting, not sure how I missed it.. – grim – 2014-02-09T06:42:43.370

It's fine - I usually do that kind of stuff whenever I see it :) – Timtech – 2014-02-09T16:37:43.973

-1

Mouse: 0 chars

"right click" > "save image as"

Result:

enter image description here

Mark Dee

Posted 2014-02-01T19:52:22.007

Reputation: 119

3Where is the “with programming APIs” part here? – manatwork – 2014-02-04T10:41:25.023