随机图像生成器

pxo*_*oto 15 command-line random images display

我正在生成随机数据并尝试使用以下方法将其转换为 PNG 图像:

head -c 1MB < /dev/urandom | hexdump -e '16/1 "_x%02X"' | sed 's/_/\\/g; s/\\x  //g; s/.*/    "&"/' | tr -d "\"" | display -depth 8 -size 1000x1000+0 rgb:-
Run Code Online (Sandbox Code Playgroud)

此命令始终显示带有一些 RGB 像素的灰色图像。我究竟做错了什么 ?

我的最终目标是生成至少一张带有随机数据的图像。

PM *_*ing 23

Firstly, you need to feed display RGB:- raw bytes, not an encoded hex string like you're building with that hexdump | sed | tr pipeline.

Secondly, you aren't giving it enough bytes: you need 3 bytes per pixel, one for each colour channel.

This does what you want:

mx=320;my=256;head -c "$((3*mx*my))" /dev/urandom | display -depth 8 -size "${mx}x${my}" RGB:-
Run Code Online (Sandbox Code Playgroud)

要直接保存为 PNG,您可以执行以下操作:

mx=320;my=256;head -c "$((3*mx*my))" /dev/urandom | convert -depth 8 -size "${mx}x${my}" RGB:- random.png
Run Code Online (Sandbox Code Playgroud)

这是一个典型的输出图像:

从 /dev/urandom 生成的 RGB 图像


如果您想制作动画,则无需创建和保存单个帧。您可以将原始字节流直接提供给 ffmpeg / avconv,例如

mx=320; my=256; nframes=100; dd if=/dev/urandom bs="$((mx*my*3))" count="$nframes" | avconv -r 25 -s "${mx}x${my}" -f rawvideo -pix_fmt rgb24 -i - random.mp4
Run Code Online (Sandbox Code Playgroud)