This is a simple challenge.
The task is to write code that outputs a 448*448 square image with 100% transparency. The output should follow the standard image rules.
This is a simple challenge.
The task is to write code that outputs a 448*448 square image with 100% transparency. The output should follow the standard image rules.
convert -size 448x448 xc:none a.png
Is this allowed?
\windows\system32
).
\$\endgroup\$
convert
tool, because the name clashes with system utility. You run magick convert ...
instead.
\$\endgroup\$
convert
is part of ImageMagick, so calling it its own language would not work.
\$\endgroup\$
ImageMagick Convert
. Might be worth opening a meta about it though
\$\endgroup\$
Full program. Prints 448-by-448-by-4 array representing a 448-by-448 rgba image.
448 448 4⍴0
⍴
is reshape
448 448
?
\$\endgroup\$
Commented
Mar 13, 2019 at 8:24
802832p0
as the answer?
\$\endgroup\$
Commented
Mar 13, 2019 at 11:25
import i"image";func a()i.Image{return i.NewRGBA(i.Rect(0,0,448,448))}
Never seen a golf in Go before. Defines a function called a that outputs the image
448ṁ4¬¥þ
A niladic Link which yields a 448 by 448 RGBA array of transparent black pixels.
448ṁ4¬¥þ - Link: no arguments
448 - 448
þ - outer-product with: -- i.e. [[f(x,y) for y in [1..448]] for x in [1..448]]
¥ - last two links as a dyad:
ṁ4 - mould like [1,2,3,4] -- e.g. x=7 -> [7,7,7,7]
¬ - logical NOT -> [0,0,0,0]
f=
(_=document.createElement`canvas`)=>_.toDataURL(_.height=_.width=448)
;document.write(f());
Returns a PNG image encoded as a data: URL suitable e.g. for setting as the src
of an HTMLImageElement
. Edit: Saved 3 bytes thanks to @Shaggy and a further 2 bytes thanks to @Arnauld.
document.createElement`canvas`
should work, saving 2 more bytes.
\$\endgroup\$
d=document
at the top and replace document
with d
? Should save ~5 characters.
\$\endgroup\$
document.write
is just part of the stack snippet, not part of the function, so it doesn't count anyway.
\$\endgroup\$
v->new int[448][448][4]
Returns a 3D array of 448x448x4 0s.
Explanation:
v-> // Method with empty unused parameter and 3D integer-array as return-type
new int[448][448][4]
// Create a 3D array of dimensions 448 by 448 by 4 (filled with 0s by default)
v->new int[448][448]
Returns a 448x448 matrix of 0s.
In Java, RGBA values can be represented by an integer. The hexadecimal 0x00000000
would represent 00
for red, green, blue, and alpha respectively. And 0x00000000
is equal to the integer 0
.
v->new int[448][448]
? Because a color can be represented with an integer (RGBA) in Java.
\$\endgroup\$
Commented
Mar 13, 2019 at 9:27
0-255
a valid RBGA value? I know it's a valid RGB value, but also RGBA? I almost never use images in Java, so not too familiar with them. I simply based my 4 inner 0s on other answers.
\$\endgroup\$
Commented
Mar 13, 2019 at 9:31
0x00000000
that fit on an int. The first 2 hex digits are the Alpha, the next 2 are the red, the next 2 are the green, the final 2 are the blue.So you only need an integer. See BufferedImage.TYPE_INT_ARGB. You have 4 values, summarized as one integer. And 0x00000000
is equal to 0
.
\$\endgroup\$
Commented
Mar 13, 2019 at 9:40
Saving the image to a file with path s
, 101 bytes
s->javax.imageio.ImageIO.write(new java.awt.image.BufferedImage(448,448,2),"png",new java.io.File(s))
Try it online... somehow
Returning the BufferedImage, 46 bytes
v->new java.awt.image.BufferedImage(448,448,2)
Saving the image to the file f
, 83 bytes
f->javax.imageio.ImageIO.write(new java.awt.image.BufferedImage(448,448,2),"png",f)
Dumping PNG to STDOUT, 92 bytes (thanks ASCII-only!)
v->javax.imageio.ImageIO.write(new java.awt.image.BufferedImage(448,448,2),"png",System.out)
Try it online!
Thanks to Kevin for saving a byte for the second and fourth solutions!
()->
in two of your answers to v->
, since it's allowed to have an empty unused input (for which I personally use Void
). Your last TIO would then be this instead.
\$\endgroup\$
Commented
Mar 13, 2019 at 10:58
448LDδ4Å0
or alternatively:
¾4Ž1ÂDиии
-2 bytes thanks to @Emigna.
-1 byte thanks to @Adnan.
Outputs a 448x448x4 3D list of 0s.
Explanation:
448LD # Push a list in the range [1,448], and duplicate it
δ # Outer product; apply the following double-vectorized:
4Å0 # Push a list of 4 0s: [0,0,0,0]
# (and output the result implicitly)
Ž1ÂD # Push compressed integer 448, and duplicate it
и # Create a list of 448 times 448
4 и # Transform it into a list of 448 times 448 times 4
¾ и # Transform it into a list of 448 times 448 times 4 times 0
# (and output the result implicitly)
See this 05AB1E answer of mine (section How to compress large integers?) to understand why Ž1Â
is 448
.
PGraphics g=createGraphics(448,448);g.beginDraw();g.save("a.png");
[[[0]*4]*488]*488
With Print:
print[[[0]*4]*488]*488
As variable:
x=[[[0]*4]*488]*488
As an array of RGBA is allowed, that is what I have created above, defaulting to all 0's - meaning black, but totally transparent.
[0]*802816
that should also work.
\$\endgroup\$
Commented
Mar 13, 2019 at 11:26
Is this valid?
<svg height=448 width=448
Test it (background
applied with CSS so you can "see" it)
<svg height=448 width=448
into an html file and opening it produces an empty <body>
. However, with the penality of 1 byte, if you write <svg height=448 width=448>
, it produces a <body>
with an empty SVG image.
\$\endgroup\$
Commented
Mar 15, 2019 at 10:43
>
means that this is, indeed, valid, as far as the markup goes.
\$\endgroup\$
<!DOCTYPE html>
, <html>
, <head>
, <style>
, <script>
and everything goes to the <body>
. Some of the additional code is required to produce the desired output. You can see that the resulting code is parsed as <svg height="448" width="448" < body></svg>
, because it writes <svg height=448 width=448 </body>
in the iframe. As such, I still believe it isn't valid.
\$\endgroup\$
Commented
Mar 15, 2019 at 11:10
use std::{io::Write,fs::File};fn main(){let mut v=vec![0,0,2,0,0,0,0,0,0,0,0,0,192,1,192,1,32,0];v.extend(vec![0u8;802816]);File::create("o.tga").unwrap().write(&v);}
This writes an actual, readable o.tga file, without any libraries or builtin functions, using the TGA binary format per http://paulbourke.net/dataformats/tga/ , by hard coding the width and height into the binary file header.
-5 bytes shorten filename, fix img size, @ASCII-only
a
would work as file name would it not. btw it's 448*448 not 444*444 so this is invalid
\$\endgroup\$
Commented
Mar 13, 2019 at 3:29
[255,0,0,0,0]
6272 times ([0,0,0,0]
128 times, repeated 6272 times). image type would then be 10
not 2
. even better, colormap for shorter run length encoding (0,0,0,0
-> 0
)
\$\endgroup\$
Commented
Mar 13, 2019 at 3:35
{P5.img⍴∘0,⍨448}
Function that outputs an image object that can be drawn to the screen (for no effect) or converted back to pixel values.
-2 thanks to ngn!
_=>(Enumerable.Repeat((0,0,0,0),200704),448,448)
Apparently outputting [1D array of pixels, width, height]
is ok, so this outputs a tuple of `(IEnumerable of pixels, width, height).
_=>Enumerable.Repeat(Enumerable.Repeat((0,0,0,0),448),448)
The original matrix returning answer.
Since the image IO rules allow output as a matrix of RGB values, this submission outputs a matrix of RGBA values, represented by tuples with four values, all being 0.
Sadly, PHP kinda sucks in that aspect because it requires a whole lot of code. But then again, where doesn't PHP suck.
$i=imagecreatetruecolor(448,448);imagesavealpha($i,true);$b=imagecolorallocatealpha($i,0,0,0,127);imagefill($i,0,0,$b);imagepng($i,'i.png');header('Content-type: image/png');readfile('i.png');
Ungolfed:
$i=imagecreatetruecolor(448,448); // Create a new image with a set width
imagesavealpha($i,true); // Tell PHP to save alphachannels on that image
$b=imagecolorallocatealpha($i,0,0,0,127); // set the actual transparency values
imagefill($i,0,0,$b); // Fill the image with the color saved above
imagepng($i,'i.png'); // Save the file as PNG
header('Content-type: image/png'); // Set the content type for the browser
readfile('i.png'); // Read the file and output it
Obviously, if you just want to create it without outputting it, you can omit the header()
and readfile()
commands. Still, it's idiotically long.
'<?php return '.var_export(array_fill(0,952576,0),1).';';
to export the array in an executable way. you can do php -f image.php > array.php
, and somewhere else you can do $array = include('array.php');
to have an usable array again. But I don't know if it is valid, so, I'm giving it to you to add as an alternative. Or even a <?=json_encode(array_fill(0,952576,0));
.
\$\endgroup\$
Commented
Mar 15, 2019 at 15:18
true
can be replaced with 1
, to save 3 bytes. Reading the documentation of imagepng()
, you can just do header('Content-type: image/png');imagepng($i);
. I believe you can also replace $b=imagecolorallocatealpha($i,0,0,0,127);imagefill($i,0,0,$b);
with imagefill($i,0,0,imagecolorallocatealpha($i,0,0,0,127));
but dont quote me on that.
\$\endgroup\$
Commented
Mar 15, 2019 at 16:29
<? imagecolorallocatealpha($i=imagecreate(448,448),0,0,0,127);header('Content-type: image/png');imagepng($i);
produces a transparent image (109 bytes). Automatically sends the image to the browser/standard output and everything.
\$\endgroup\$
Commented
Mar 15, 2019 at 17:44
Python 3.7 - PIL Imported, 30 bytes
Image.new("LA",[448]*2).show()
This requires an import but has the benefit of creating and displaying an actual image file rather than an abstract empty array.
Explanation:
from PIL import Image
Image.new( // create a new image
mode="LA" // select LA colour mode, this is grey-scale plus an alpha channel
size=[448]*2 // size argument needs to be a 2D tuple, [448*2] is a better golf shot than (448,448)
color=0 // populates the image with the specified colour, helpfully preset to transparent
).show() // display the image object
Image.show() will open the image in your default image program. In my case this opens a temporary bitmap file in Windows Photo Viewer but results may vary. Arguably this is cheating since the bitmap representation contains no transparency
Variations...
Image.new("LA",[448]*2) // 24 bytes but doesn't open image
Image.new("LA",[448]*2).show() // 30 bytes, shows you a bitmap
Image.new("LA",[448]*2).save("x.png") // 37 bytes, saves image to disk
Import
.
\$\endgroup\$
Commented
Mar 14, 2019 at 0:53
new("LA",[448]*2).show()
, 26 bytes.
\$\endgroup\$
Commented
Mar 14, 2019 at 2:11
imwrite(nan(448),'.png','tr',1)
Creates a 448 x 448 matrix of NaN
values, and then uses imwrite
to save them to a PNG file named '.png'
and sets the transparency to 1, making it transparent. The 'Transparency'
parameter name is able to be abbreviated to 'tr'
as MATLAB allows for partial string matching of parameter names as long as the shortened version is unique among available parameters.
a: make image![448x448 0.0.0.255]
Opacity defaults to fully opaque(0)
This is how to use it /a full program/ :
Red [ ]
a: make image! [ 448x448 0.0.0.255 ]
view [ image a ]
º4♦7*_ß{.a
º push [0]
4 push 4
♦7* push 64*7=448
_ duplicate TOS
ß wrap last three elements in array (stack is now [[0], [4, 448, 448]])
{ foreach in [4, 448, 448]
. repeat TOS x times
a wrap TOS in array
This method saves 1 byte compared to the "standard" ♦7*_4º*a*a*
_=>[w=448,w,Array(w*w).fill([0,0,0,0])]
Apparently outputting [height, width, 1d array of RGBA values]
is ok.
-3 bytes thanks to @Arnauld
[1D array of pixels, width, height]
\$\endgroup\$
Commented
Mar 13, 2019 at 2:21
DIM A[448,448]SAVE"DAT:I",A
Saves a 2-dimensional 448x448 array filled with 0s to a file named DAT:I
(Which is shorter than defining a function that returns the array, somehow)
The standard formats (used by all graphics functions) for colors in SmileBASIC are 32 bit ARGB and 16 bit 5551 RGBA, and 0
is transparent in both of them.
ARGB
format (commonly used in Smilebasic), so 0x00000000
is Alpha = 0, Red = 0, Green = 0, Blue = 0. Black would be 0xFF000000
.
\$\endgroup\$
GRP
but I'll let it go since the actual data being stored is the same.
\$\endgroup\$
Text"R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
Base64Encode Decode
ResizeImage 448
Takes a 1x1 transparent gif encoded as base64 and resizes it
♪☺ü@/øP♦£Q)%)£Q◄úæD)%)£Q
Explanation
♪☺ü@ # Push literal 448
/ # Store the first stack value in the a register.
ø # Push literal 0
P # Push four copies of the first stack value.
♦ # Push literal 4
£Q # Push stack values into a list of the size of the first stack value starting with the second stack value.
) # Push the value contained in the a register.
% # Push the second stack value the absolute value of the first stack value times.
) # Push the value contained in the a register.
£Q # Push stack values into a list of the size of the first stack value starting with the second stack value.
◄úæ # Push literal \n
D # Push the sum of the second and first stack values.
) # Push the value contained in the a register.
% # Push the second stack value the absolute value of the first stack value times.
) # Push the value contained in the a register.
£Q # Push stack values into a list of the size of the first stack value starting with the second stack value.
#include <SFML/Graphics.hpp>
void f(){sf::Image i;i.create(448,448);i.createMaskFromColor(sf::Color::Black);i.saveToFile("a.png");}
location=C.toDataURL()
<canvas id=C width=448 height=448>
Clicking "Run code snippet" will generate a 448x448 transparent PNG in an IFRAME. You may then right-click "Save image as..." to download it to your computer.