Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random BMP in CLI

I need a truly random BMP in order to test various lossy image compression algorithms. Ideally, this would not rely on any library and run in a Linux CLI.

It should generate a random BMP given a certain width and height.

like image 531
Pierre Spring Avatar asked Dec 25 '22 21:12

Pierre Spring


1 Answers

Updated Answer - April 2021

Here are some more ideas for random images:


Random coloured squares

magick -size 8x8 xc: +noise Random -scale 100x100 RandomColouredSquares.png

enter image description here


Random black and white crosswords

magick -size 8x8 xc:gray +noise Random -threshold 50% -scale 100x100 RandomCrosswords.png

enter image description here


Random grey blur

magick -size 8x8 xc:gray +noise Random -resize 100x100 RandomGreyBlur.png

enter image description here


Random coloured blur

magick -size 5x5 xc: +noise Random -auto-level -resize 100x100 RandomColouredBlur.png

enter image description here


Random salt and pepper

magick -size 100x100 xc:gray +noise Random -threshold 1% -negate RandomSaltAndPepper.png

enter image description here


Repeated coloured pattern

magick -size 50x50 xc: +noise random -virtual-pixel tile -blur 0x6 -auto-level -write MPR:tile +delete -size 250x250 tile:MPR:tile RandomRepeatedPattern.png

enter image description here

Updated Answer - March 2021

If you want random-noise type of images, see original answer below, noting that you should replace convert with magick in those examples if working with ImageMagick v7 onwards.

If you want images of a solid random colour, you could do something like this:

magick -size 400x200 xc:"rgb($((RANDOM%255)),$((RANDOM%255)),$((RANDOM%255)))" image.png

Sample Outputs

enter image description here

enter image description here


If you want images of a random size and a random solid colour, you could use this for images between 200..264 pixels wide by 100..132 pixels tall:

magick -size "$(((RANDOM%64)+200))x$(((RANDOM%32)+100))" xc:"rgb($((RANDOM%255)),$((RANDOM%255)),$((RANDOM%255)))"random.png

Sample Outputs

enter image description here

enter image description here

Original Answer

You can use ImageMagick (which is installed on most Linux distros by default) to generate an image of random noise like this:

convert -size 300x200 xc:gray +noise random out.bmp

where 300 is the width and 200 is the height (just examples).

Other types of noise are available, just run

convert -list noise

Output

Gaussian
Impulse
Laplacian
Multiplicative
Poisson
Random
Uniform

If the noise is too noisy ;-) for you, you can attenuate it with

convert -size 300x200 xc:gray -attenuate 0.5 +noise random out.bmp

for a 50% attenuation

Here are some examples of the different types:

enter image description here

Here are the corresponding distribution histograms:

enter image description here


Just for completeness, note that this answer features in Daniel Barrett's book "Efficient Linux at the Command Line".

like image 111
Mark Setchell Avatar answered Dec 28 '22 23:12

Mark Setchell