นี่เป็นความท้าทายที่เรียบง่าย
ภารกิจคือการเขียนโค้ดที่ให้ภาพสี่เหลี่ยมจัตุรัสขนาด 448 * 448 ที่โปร่งใส 100% เอาท์พุทควรปฏิบัติตามกฎระเบียบของภาพมาตรฐาน
นี่เป็นความท้าทายที่เรียบง่าย
ภารกิจคือการเขียนโค้ดที่ให้ภาพสี่เหลี่ยมจัตุรัสขนาด 448 * 448 ที่โปร่งใส 100% เอาท์พุทควรปฏิบัติตามกฎระเบียบของภาพมาตรฐาน
คำตอบ:
convert -size 448x448 xc:none a.png
ได้รับอนุญาตหรือไม่
\windows\system32
)
convert
เครื่องมือเพราะชื่อขัดแย้งกับยูทิลิตี้ระบบ คุณวิ่งmagick convert ...
แทน
convert
เป็นส่วนหนึ่งของ ImageMagick ดังนั้นการเรียกมันว่าภาษาของมันเองจะไม่ทำงาน
ImageMagick Convert
คุณก็สามารถพูดภาษาที่คุณใช้อยู่ อาจจะคุ้มค่าที่จะเปิดเมตาเกี่ยวกับเรื่องนี้
โปรแกรมเต็มรูปแบบ พิมพ์ 448-by-448-by-4 array ที่แสดงภาพ 448-by-448 rgba
448 448 4⍴0
⍴
คือr eshape
448 448
?
802832p0
คำตอบ?
import i"image";func a()i.Image{return i.NewRGBA(i.Rect(0,0,448,448))}
ไม่เคยเห็นกอล์ฟมาก่อนเลย กำหนดฟังก์ชั่นที่เรียกว่า a เอาท์พุทภาพ
f=
(_=document.createElement`canvas`)=>_.toDataURL(_.height=_.width=448)
;document.write(f());
ผลตอบแทนภาพ PNG เข้ารหัสเป็นข้อมูล: URL เช่นที่เหมาะสมสำหรับการตั้งค่าเป็นของsrc
HTMLImageElement
แก้ไข: บันทึกแล้ว 3 ไบต์ขอบคุณ @Shaggy และอีก 2 ไบต์ขอบคุณ @Arnauld
document.createElement`canvas`
ควรทำงานได้ประหยัด 2 ไบต์ขึ้นไป
d=document
ที่ด้านบนและแทนที่document
ด้วยd
? ควรบันทึก ~ 5 ตัวอักษร
document.write
เป็นเพียงส่วนหนึ่งของกองข้อมูลย่อยไม่ได้เป็นส่วนหนึ่งของฟังก์ชันจึงไม่นับรวม
v->new int[448][448][4]
ส่งคืนอาร์เรย์ 3D ที่มีขนาด 448x448x4 0
คำอธิบาย:
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]
ส่งคืนเมทริกซ์ 448x448 เป็น 0
ใน Java ค่า RGBA สามารถแสดงเป็นจำนวนเต็มได้ เลขฐานสิบหก0x00000000
จะแทน00
ด้วยสีแดงสีเขียวสีน้ำเงินและอัลฟาตามลำดับ และจะมีค่าเท่ากับจำนวนเต็ม0x00000000
0
v->new int[448][448]
ไม่ได้ใช่มั้ย เนื่องจากสีสามารถแสดงด้วยจำนวนเต็ม (RGBA) ใน Java
0-255
ค่า RBGA ที่ถูกต้องหรือไม่ ฉันรู้ว่ามันเป็นค่า RGB ที่ถูกต้อง แต่ยัง RGBA? ฉันแทบไม่เคยใช้ภาพใน Java ดังนั้นจึงไม่คุ้นเคยกับมันมากนัก ฉันเพียงแค่ใช้ 0 0 ภายในของฉันกับคำตอบอื่น ๆ
0x00000000
ที่พอดีกับ int เลขฐานสิบหก 2 หลักแรกคืออัลฟา 2 ถัดไปเป็นสีแดง 2 ถัดไปคือสีเขียว 2 สุดท้ายคือสีฟ้าดังนั้นคุณจะต้องเป็นจำนวนเต็ม ดูBufferedImage.TYPE_INT_ARGB คุณมี 4 ค่าสรุปเป็นหนึ่งจำนวนเต็ม และจะมีค่าเท่ากับ0x00000000
0
บันทึกภาพไปยังไฟล์ที่มีเส้นทางs
101 ไบต์
s->javax.imageio.ImageIO.write(new java.awt.image.BufferedImage(448,448,2),"png",new java.io.File(s))
ลองออนไลน์สิ ...
ส่งคืน BufferedImage ขนาด 46 ไบต์
v->new java.awt.image.BufferedImage(448,448,2)
บันทึกภาพไปยังไฟล์f
83 ไบต์
f->javax.imageio.ImageIO.write(new java.awt.image.BufferedImage(448,448,2),"png",f)
ทิ้ง PNG ไปยัง STDOUT, 92 ไบต์ (ขอบคุณ ASCII-only!)
v->javax.imageio.ImageIO.write(new java.awt.image.BufferedImage(448,448,2),"png",System.out)
ลองออนไลน์!
ขอบคุณเควินที่บันทึกไบต์สำหรับโซลูชันที่สองและสี่!
()->
ในสองคำตอบของคุณv->
เนื่องจากได้รับอนุญาตให้มีการป้อนข้อมูลที่ไม่ได้ใช้ที่ว่างเปล่า (ซึ่งฉันใช้เองVoid
) TIO สุดท้ายของคุณแล้วจะเป็นนี้แทน
448LDδ4Å0
หรืออีกทางหนึ่ง:
¾4Ž1ÂDиии
-2 ไบต์ขอบคุณที่@Emigna
-1 ขอบคุณไบต์@Adnan
เอาท์พุตรายการ 3D 448x448x4 เป็น 0
คำอธิบาย:
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)
เห็นนี้ 05AB1E คำตอบของฉัน (ส่วนวิธีการบีบอัดจำนวนเต็มขนาดใหญ่? )จะเข้าใจว่าทำไมเป็นŽ1Â
448
PGraphics g=createGraphics(448,448);g.beginDraw();g.save("a.png");
[[[0]*4]*488]*488
ด้วยการพิมพ์:
print[[[0]*4]*488]*488
เป็นตัวแปร:
x=[[[0]*4]*488]*488
ในฐานะที่เป็นอาร์เรย์ของ RGBA ที่ได้รับอนุญาตนั่นคือสิ่งที่ฉันได้สร้างไว้ข้างต้นโดยเริ่มต้นกับ 0 ทั้งหมด - หมายถึงสีดำ แต่โปร่งใสโดยสิ้นเชิง
[0]*802816
ก็จะได้ผล
448ṁ4¬¥þ
ลิงก์ niladic ซึ่งให้ผลอาร์เรย์ RGBA ขนาด 448 x 448 พิกเซลสีดำโปร่งใส
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]
ถูกต้องหรือไม่
<svg height=448 width=448
ทดสอบ ( background
ใช้กับ CSS เพื่อให้คุณสามารถ "เห็น")
<svg height=448 width=448
ลงในไฟล์ HTML <body>
และการเปิดมันผลิตที่ว่างเปล่า อย่างไรก็ตามด้วยความผิดทางอาญา 1 ไบต์ถ้าคุณเขียน<svg height=448 width=448>
มันจะสร้าง<body>
ด้วยภาพ SVG ที่ว่างเปล่า
>
หมายความว่านี่เป็นสิ่งที่ถูกต้องตามความเป็นจริงตราบใดที่มาร์กอัปเกิดขึ้น
<!DOCTYPE html>
, <html>
, <head>
, <style>
, และทุกอย่างไป<script>
<body>
ต้องใช้รหัสเพิ่มเติมบางอย่างเพื่อสร้างผลลัพธ์ที่ต้องการ คุณจะเห็นว่าโค้ดผลลัพธ์ถูกวิเคราะห์คำเป็น<svg height="448" width="448" < body></svg>
เพราะมันเขียน<svg height=448 width=448 </body>
ใน iframe เช่นนี้ฉันยังเชื่อว่ามันไม่ถูกต้อง
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);}
สิ่งนี้เขียนไฟล์ o.tga ที่แท้จริงและอ่านได้โดยไม่ต้องมีไลบรารีหรือฟังก์ชัน builtin โดยใช้รูปแบบไบนารี TGA ต่อhttp://paulbourke.net/dataformats/tga/โดยการเข้ารหัสความกว้างและความสูงลงในส่วนหัวของไฟล์ไบนารี่อย่างหนัก
-5 ไบต์สั้นลงชื่อไฟล์แก้ไขขนาด img, @ ASCII เท่านั้น
a
จะทำงานเป็นชื่อไฟล์มันจะไม่ btw มันคือ 448 * 448 ไม่ใช่ 444 * 444 ดังนั้นนี่จึงไม่ถูกต้อง
[255,0,0,0,0]
6272 เท่า ( [0,0,0,0]
128 ครั้งซ้ำแล้วซ้ำอีก 6272 ครั้ง) จากนั้นประเภทภาพจะเป็น10
ไม่2
ดียิ่งขึ้น colormap สำหรับการเข้ารหัสความยาวที่สั้นลง ( 0,0,0,0
-> 0
)
_=>(Enumerable.Repeat((0,0,0,0),200704),448,448)
เห็นได้ชัดว่าการส่งออก [1D array of pixels, width, height]
ก็โอเคดังนั้นสิ่งนี้จะส่งออก tuple ของ `(IE จำนวนพิกเซลความกว้างความสูง)
_=>Enumerable.Repeat(Enumerable.Repeat((0,0,0,0),448),448)
เมทริกซ์เดิมตอบกลับ
เนื่องจากกฎของอิมเมจ IO อนุญาตให้เอาท์พุทเป็นเมทริกซ์ของค่า RGB การส่งนี้จึงส่งออกเมทริกซ์ของค่า RGBA แทนด้วย tuples ที่มีค่าสี่ค่าทั้งหมดเป็น 0
น่าเสียดายที่ PHP ค่อนข้างเข้าใจในแง่นั้นเพราะมันต้องใช้รหัสจำนวนมาก แต่แล้วอีกครั้งที่ PHP ไม่ดูด
$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
เห็นได้ชัดว่าถ้าคุณเพียงแค่ต้องการสร้างมันโดยไม่ต้องเอาท์พุทคุณสามารถละเว้นheader()
และreadfile()
คำสั่ง ถึงกระนั้นมันก็ยังงี่เง่า
'<?php return '.var_export(array_fill(0,952576,0),1).';';
เพื่อส่งออกอาร์เรย์ในวิธีปฏิบัติ คุณสามารถทำphp -f image.php > array.php
และที่อื่นคุณสามารถทำ$array = include('array.php');
อาร์เรย์ที่ใช้งานได้อีกครั้ง แต่ฉันไม่รู้ว่ามันถูกต้องหรือไม่ดังนั้นฉันให้คุณเพิ่มเป็นทางเลือก <?=json_encode(array_fill(0,952576,0));
หรือแม้กระทั่ง
true
สามารถถูกแทนที่ด้วย1
เพื่อบันทึก 3 ไบต์ อ่านเอกสารของคุณก็สามารถทำได้imagepng()
header('Content-type: image/png');imagepng($i);
ฉันเชื่อว่าคุณสามารถแทนที่$b=imagecolorallocatealpha($i,0,0,0,127);imagefill($i,0,0,$b);
ด้วยimagefill($i,0,0,imagecolorallocatealpha($i,0,0,0,127));
แต่อย่าอ้างฉันในสิ่งนั้น
<? imagecolorallocatealpha($i=imagecreate(448,448),0,0,0,127);header('Content-type: image/png');imagepng($i);
สร้างภาพที่โปร่งใส (109 ไบต์) ส่งภาพไปยังเบราว์เซอร์ / เอาต์พุตมาตรฐานและทุกอย่างโดยอัตโนมัติ
Python 3.7 - PIL นำเข้า 30 ไบต์
Image.new("LA",[448]*2).show()
สิ่งนี้ต้องการการนำเข้า แต่มีประโยชน์ในการสร้างและแสดงไฟล์รูปภาพจริงแทนที่จะเป็นอาร์เรย์ว่างเปล่าแบบนามธรรม
คำอธิบาย:
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 () จะเปิดภาพในโปรแกรมรูปภาพเริ่มต้นของคุณ ในกรณีของฉันนี้จะเปิดไฟล์บิตแมปชั่วคราวใน Windows Photo Viewer แต่ผลลัพธ์อาจแตกต่างกันไป เนื้อหานี้เป็นการโกงเนื่องจากการแสดงบิตแมปไม่มีความโปร่งใส
รูปแบบ ...
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
นอกจากนี้ในการอธิบายคุณประโยชน์
new("LA",[448]*2).show()
คือ 26 ไบต์
imwrite(nan(448),'.png','tr',1)
สร้างเมทริกซ์ของNaN
ค่า448 x 448 จากนั้นใช้imwrite
เพื่อบันทึกลงในไฟล์ PNG ที่ตั้งชื่อ'.png'
และตั้งค่าความโปร่งใสเป็น 1 ทำให้โปร่งใส 'Transparency'
ชื่อพารามิเตอร์สามารถที่จะยากที่จะ'tr'
เป็น MATLAB ช่วยให้การจับคู่สตริงบางส่วนของชื่อพารามิเตอร์ตราบเท่าที่คร่าว ๆ ที่ไม่ซ้ำกันในหมู่พารามิเตอร์ใช้ได้
{P5.img⍴∘0,⍨448}
ฟังก์ชั่นที่ส่งออกวัตถุภาพที่สามารถวาดลงบนหน้าจอ (โดยไม่มีผลกระทบ) หรือแปลงกลับเป็นค่าพิกเซล
-2 ขอบคุณngn !
{P5.img⍴∘0,⍨448}
2¡
สามารถ⁺
a: make image![448x448 0.0.0.255]
ความทึบเริ่มต้นที่ทึบแสงเต็มที่ (0)
นี่คือวิธีใช้ / โปรแกรมเต็มรูปแบบ /:
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
วิธีนี้ช่วยประหยัด 1 ไบต์เมื่อเปรียบเทียบกับ "มาตรฐาน" ♦7*_4º*a*a*
_=>[w=448,w,Array(w*w).fill([0,0,0,0])]
เห็นได้ชัดว่าการส่งออก [height, width, 1d array of RGBA values]
ก็โอเค
-3 ไบต์ขอบคุณ @Arnauld
[1D array of pixels, width, height]
DIM A[448,448]SAVE"DAT:I",A
บันทึกอาร์เรย์ 448x448 สองมิติที่เต็มไปด้วย 0s ไปยังไฟล์ที่ชื่อDAT:I
(ซึ่งสั้นกว่าการกำหนดฟังก์ชันที่ส่งกลับอาร์เรย์อย่างใด)
รูปแบบมาตรฐาน (ใช้โดยฟังก์ชั่นกราฟิกทั้งหมด) สำหรับสีใน SmileBASIC คือ 32 บิต ARGB และ 16 บิต 5551 RGBA และ0
โปร่งใสทั้งสองแบบ
ARGB
รูปแบบ (ใช้กันทั่วไปใน Smilebasic) เพื่อ0x00000000
เป็นอัลฟา = 0, สีแดง = 0, สีเขียว = 0, สีฟ้า = 0 0xFF000000
จะเป็นสีดำ
GRP
แต่ฉันจะปล่อยไปเนื่องจากข้อมูลจริงที่จัดเก็บนั้นเหมือนกัน
Text"R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
Base64Encode Decode
ResizeImage 448
ใช้ gif โปร่งใสขนาด 1x1 ที่เข้ารหัสเป็น base64 และปรับขนาด
♪☺ü@/øP♦£Q)%)£Q◄úæD)%)£Q
คำอธิบาย
♪☺ü@ # 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");}
การคลิก "เรียกใช้ข้อมูลโค้ด" จะสร้าง PNG แบบโปร่งใส 448x448 ใน IFRAME จากนั้นคุณสามารถคลิกขวาที่ "บันทึกภาพเป็น ... " เพื่อดาวน์โหลดลงในคอมพิวเตอร์ของคุณ