15
\$\begingroup\$

Interestingly, although there are challenges that work with images, there is no challenge to simply output an image. This is that challenge

Goal: output an image in the shortest possible code.

Output: Any image output is accepted. Files would be preferable, but most any non-character on the screen will function as an image. An example of this is matplotlib output.

For languages that do not support file saving or direct image output, it is acceptable to output what would be the image file's content to any other kind of output. If a language supports saving to a file, this is not an option.

This is ; the shortest code that can accomplish the task will win.

\$\endgroup\$
6
  • \$\begingroup\$ Does a single pixel qualify? \$\endgroup\$
    – Someone
    Commented Dec 10, 2023 at 22:09
  • 2
    \$\begingroup\$ What is an image? Anything non-text on the screen? An image file in any format? The contents of an image file outputted or returned? \$\endgroup\$
    – Adám
    Commented Dec 10, 2023 at 22:09
  • 1
    \$\begingroup\$ @Somebody Yes, that is intended. \$\endgroup\$
    – Jakav
    Commented Dec 10, 2023 at 22:09
  • \$\begingroup\$ @Adám An image file in any format, unless a language has image output in another way. \$\endgroup\$
    – Jakav
    Commented Dec 10, 2023 at 22:12
  • \$\begingroup\$ What do you think about @Stef's comment on my answer? codegolf.stackexchange.com/questions/267423/output-an-image/… Does printing the contents of an image file to stdout qualify? \$\endgroup\$
    – Someone
    Commented Dec 12, 2023 at 18:25

19 Answers 19

10
\$\begingroup\$

MATLAB, 3 bytes

spy

Displays the following image in a figure window.

Tested on Matlab R2017b and R2023b. Very old Matlab versions produced a different image.

enter image description here

Explanation

From the documentation:

spy(S) plots the sparsity pattern of matrix S. Nonzero values are colored while zero values are white. The plot displays the number of nonzeros in the matrix, nz = nnz(S).

When called without input arguments, spy uses a default image. This is not documented, but it is well known.

\$\endgroup\$
9
\$\begingroup\$

Pyxplot, 1 byte

p

p is an abbreviation for the plot command. When p is called as a bare keyword in the default environment, the file pyxplot.eps is created in the working directory, showing the default plot axes.

enter image description here

\$\endgroup\$
5
\$\begingroup\$

PICO-8, 5 bytes

spr()

Displays sprite 0 at (0,0). Sprite 0 is, conveniently enough, the only sprite that's not empty by default.

A PICO-8 terminal with a graphic displayed.

\$\endgroup\$
5
\$\begingroup\$

Google Sheets, 17 bytes

=sparkline({1;0})

Inserts a diagonal line in the formula cell. See sparkline().

Multi-cell image with Unicode: (13 bytes)

=cursorpark()

cursorpark

The function is undocumented.

\$\endgroup\$
2
5
\$\begingroup\$

Desmos (or any graphing calculator really), 0 bytes

Ok this might be bending the rules quite a bit, but the output is the graph on the right (albeit empty, but still with axis lines and such) upon loading the Desmos Graphing Calculator website. Please say so if this isn't allowed; I will promptly delete this answer if that is the case.

\$\endgroup\$
2
  • \$\begingroup\$ This definitely bends the rules. I'm going to say it doesn't count because the program isn't actually drawing anything. The axes and grid are really just background. One character would work, however: x. P.S. please don't delete this answer \$\endgroup\$
    – Jakav
    Commented Dec 12, 2023 at 22:37
  • \$\begingroup\$ It's self-contradictory to say "it doesn't count" and "please don't delete this answer" - the community consensus has long been that answers that don't count have to be deleted. \$\endgroup\$ Commented Feb 26 at 22:51
4
\$\begingroup\$

Mathematica (Notebook interface), 3 bytes

Red

In recent versions of Mathematica (I don't remember since when), color objects are displayed as colored boxes in the notebook interface.

enter image description here

\$\endgroup\$
4
\$\begingroup\$

HTML (Stack Snippet), 9 bytes

<img src=

Displays the "broken image" icon. Abuses the fact that the stack snippet wraps the HTML input inside a template HTML document, so what ends being parsed is <img src=<script type="text/javascript"> (thanks @Kaiido).

Google Chrome and Microsoft Edge: image of the chrome and edge "broken image" icon

Firefox: image of the firefox "broken image" icon

HTML, 11 bytes

<img src=/>

Same thing but works outside of stack snippets.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ This doesn't really work. The StackSnippet will wrap your HTML input inside a template html, so what ends being parsed is <img src="script" type="text/javascript">. An HTML parser won't recognize just <img src= as an <img> tag, you need the closing >. Also, to show the "broken image" icon, it needs to have a non-empty src attribute. So the actual minimal is <img src=.>. super-attractive-libra.glitch.me But there is still a catch, it must be served from a non-opaque origin, otherwise the browser won't do the relative request and still no broken-icon. \$\endgroup\$
    – Kaiido
    Commented Dec 12, 2023 at 4:43
4
\$\begingroup\$

Uiua, 9 6 bytes (SBCS)

Outputs an actual image.

↯.⊂.30

30  put the number 30 on the stack (numeric arrays of size 30×30+ output as images)

. duplicate (now we have two 30s on the stack)

 join (into single array, leaving [30,30] on the stack)

. duplicate (now we have two [30,30]s on the stack)

 reshape (now we have a 30×30 array of 30s on the stack)

Try it on uiua.org

\$\endgroup\$
5
  • \$\begingroup\$ You can save 1 byte (but +1 character) by using 30_30 instead of ⊂.30. Unfortunately, Uiua's takes up 3 bytes. Probably worth keeping both solutions should someone create a better byte encoding for Uiua. \$\endgroup\$
    – Tbw
    Commented Dec 12, 2023 at 5:05
  • \$\begingroup\$ [.30] also works. \$\endgroup\$
    – Tbw
    Commented Dec 12, 2023 at 22:17
  • \$\begingroup\$ @Tbw Thanks, added. \$\endgroup\$
    – Adám
    Commented Dec 13, 2023 at 7:01
  • \$\begingroup\$ I made a Uiua SBCS, so you can stick with the shorter solution at only 6 bytes. \$\endgroup\$
    – Tbw
    Commented Dec 28, 2023 at 5:12
  • \$\begingroup\$ @Tbw Thanks, updated. \$\endgroup\$
    – Adám
    Commented Dec 28, 2023 at 9:52
3
\$\begingroup\$

QBasic, 15 bytes

SCREEN 9
DRAW"D

Starting in the middle of the screen, draw a short vertical line in the default white foreground color. Output when run on Archive.org:

A black screen with a tiny white line in the middle and the text "Press any key to continue" at the bottom

\$\endgroup\$
1
  • \$\begingroup\$ Upvoted for QBasic, woop woop! \$\endgroup\$ Commented Dec 14, 2023 at 16:46
3
\$\begingroup\$

HTML, 8 bytes

Outputs a transparent-black canvas image. On most browsers you can right-click "Save image as..." on it.

canvas { border: 1px solid } /* not required */
<canvas>

\$\endgroup\$
3
\$\begingroup\$

Python 3, 18 bytes

import antigravity
\$\endgroup\$
6
  • 1
    \$\begingroup\$ I think you should at the very least do with open('f.pbm', 'w') as f: print("P1 1 1 1", file=f) in order for the pbm solution to qualify. The OP says either output a file or display an image on-screen; print("P1 1 1 1") does neither, it just outputs 9 characters. The antigravity option is great though. \$\endgroup\$
    – Stef
    Commented Dec 12, 2023 at 18:23
  • \$\begingroup\$ @Stef good point; I asked the OP. If they say printing doesn't count, then I'll just remove the first part and leave the antigravity one, because open('f.pbm','w').write("P1 1 1 1") is longer. \$\endgroup\$
    – Someone
    Commented Dec 12, 2023 at 18:26
  • 1
    \$\begingroup\$ Well, if it doesn't count then many current solutions are invalidated. It functions as a valid option. \$\endgroup\$
    – Jakav
    Commented Dec 12, 2023 at 22:32
  • \$\begingroup\$ @Jakav Which "Many current solutions" would be invalidated? I can see only this python answer and the brainfuck answer. Other solutions either really display an image on screen, or save an image file. And brainfuck cannot save image files nor display images, so it's fair to tolerate the brainfuck answer. But python can both save images and display images, so printing 9 characters to screen really is not a correct solution! \$\endgroup\$
    – Stef
    Commented Dec 13, 2023 at 13:19
  • \$\begingroup\$ I see 4 answers which use the PBM format and simply output it, which is "many current solutions". However, only two of those would be affected by this change. I'll revise the challenge. \$\endgroup\$
    – Jakav
    Commented Dec 13, 2023 at 14:05
2
\$\begingroup\$

PBM, 8 bytes

P1 1 1 1

Or, if you don't consider an image file as a valid submission, any program that outputs the bytes above.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ I would prefer not to consider an image file itself a valid submission, as although it could technically be "run" by an image viewer, it doesn't really output an image; it is the image. \$\endgroup\$
    – Jakav
    Commented Dec 10, 2023 at 22:26
  • 3
    \$\begingroup\$ @Jakav Then it can simply claim the language as Text, and nothing would be changed. \$\endgroup\$
    – tsh
    Commented Dec 11, 2023 at 2:55
  • \$\begingroup\$ I just want to say I approve, this is the answer I was going to write when I saw the question. \$\endgroup\$
    – porglezomp
    Commented Dec 28, 2023 at 21:57
2
\$\begingroup\$

(ba)sh: 20 bytes

Black pixel:

echo P1 1 1 1 >i.pbm

White pixel:

echo P1 1 1 0 >i.pbm

Utilizes the rather rarely-used PBM format. PPM is it's bigger brother, supporting RGB. Red pixel (more bytes):

echo P3 1 1 1 1 0 0 >i.pbm

Very useful protocol when piping images from stdout of one program to stdin of another, not so much for anything else. But it's still an image...

Phylosophical answer: Browser: 1 byte

$

It's rendered from a font file onto your screen. It's a shape consisting of multiple bytes. Therefore, it's an image?

\$\endgroup\$
3
  • \$\begingroup\$ Love the philosophical answer. \$\endgroup\$
    – lyxal
    Commented Dec 12, 2023 at 10:27
  • \$\begingroup\$ If you're running bash on Mac OS X>=10.2 you can do screencapture a.png which is only 19 bytes \$\endgroup\$
    – Stef
    Commented Dec 12, 2023 at 21:16
  • 2
    \$\begingroup\$ You can save a byte on each Bash answer by doing echo>i.pbm P1 1 1 1 etc. \$\endgroup\$
    – pxeger
    Commented Dec 12, 2023 at 21:27
1
\$\begingroup\$

Brainfuck, 60 bytes

-[+>++[++<]>]>.>>-[-[-<]>>+<]>->>-[<+>-----]<--.<.>.<.>.<.>.

Attempt This Online!

Outputs the same PBM image of these answers.

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

Windows, 1 keystroke

PrintScreen

Output to clipboard

\$\endgroup\$
2
  • \$\begingroup\$ That relies on the Use the PrtScn button to open screen snipping setting in Settings>Ease of Access>Interaction>Keyboard being set to Off. \$\endgroup\$
    – Tbw
    Commented Dec 12, 2023 at 5:08
  • \$\begingroup\$ In Windows 10 at least. I'm not sure whether that setting is on or off by default. \$\endgroup\$
    – Tbw
    Commented Dec 12, 2023 at 5:09
1
\$\begingroup\$

JavaScript (browser), 58 47 bytes

f=

_=>document.createElement("canvas").toDataURL()

;document.write(`<a href="${f()}">Right-click to download</a>`)

Outputs a transparent 300×150 rectangle. Edit: Saved 11 bytes thanks to @Kaiido.

\$\endgroup\$
8
  • \$\begingroup\$ It's actually transparent, not white. And no need for the mime type, png is the default. Also toBlob() is shorter. \$\endgroup\$
    – Kaiido
    Commented Dec 12, 2023 at 4:52
  • \$\begingroup\$ And if going the toBlob() road, then new OffscreenCanvas(1,1).convertToBlob() is one byte shorter. \$\endgroup\$
    – Kaiido
    Commented Dec 12, 2023 at 4:59
  • \$\begingroup\$ @Kaiido Although those functions don't actually return the image as such, which currently doesn't appear to be an allowed output method. \$\endgroup\$
    – Neil
    Commented Dec 12, 2023 at 7:13
  • \$\begingroup\$ dataURL() either. It outputs a string, which is one of the representations of a binary file. A Blob object is another. If a data: URL is a valid return value, I don't see why a Blob wouldn't be. \$\endgroup\$
    – Kaiido
    Commented Dec 12, 2023 at 7:53
  • \$\begingroup\$ @Kaiido toBlob doesn't actually return the blob though... \$\endgroup\$
    – Neil
    Commented Dec 12, 2023 at 8:17
1
\$\begingroup\$

TI-BASIC, 1 byte

DispGraph

Displays the graph screen.

\$\endgroup\$
2
  • \$\begingroup\$ Is this really 1 byte? It's 9 in the format it is shown here. \$\endgroup\$
    – Jakav
    Commented Dec 12, 2023 at 22:40
  • 1
    \$\begingroup\$ @Jakav TI-BASIC is tokenized. e is 2 bytes while LinReg(ax+b) is 1 byte. \$\endgroup\$
    – Yousername
    Commented Dec 13, 2023 at 0:40
1
\$\begingroup\$

C# (.NET Core), 41 bytes

new System.Drawing.Bitmap(9,9).Save("p");

Creates a transparent 9x9 pixel PNG image.

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

MATL, 3 bytes

1XG

Try it at MATL Online

Draws a plot of the point (1,1):

The MATL online output window, showing axes and a point at coordinates (1,1)

XG is the plot builtin. I'm not 100% sure why passing it a real number \$N\$ plots the point \$(1,N)\$, but it works.

\$\endgroup\$

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.