Generating random image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
LubiePlacki

Generating random image

Post by LubiePlacki »

Hello,
I wanna make some experiment. I want generate all possible variations of black/white (2 "colors) 100x100 image and compare generated images to some pattern (or OCR). I know this a lot of combinations but i'm just curious ;)

Is it possible to pass to some of IM programs "map" (i don't know) of pixels arrangement? Or is there some other scripts (not exactly from IM dist.) that can do this stuff?

I know very well perl but i don't know how to handle this, maybe some clues?

Thanks for help and sorry for my English.

P.s. i made some math, and for 1600x1600 24bit images (one pixel=3 colors - RGB, each pixel can take 0-255) there will be...
2 ^ 46 080 000 combinations. But on such a images will be everything, you making sex with yourself with three heads, you killing Hitler using a rubber chicken in pink pants or pictures of distant planets, you in every single moment of your life from any angle or with every ISO...

Greets.
xc3ll

Re: Generating random image

Post by xc3ll »

if you want to make every possible variation of a black & white 100x100 image, you're going to need a lot of computing power. Thats 2^200 or 1.60693804 × 10^60 possible combinations.

If you want to make a random image, you could use a 2d array to store the pixel arrangement. For example (this is C++, but the perl implementation should be very similar):

bool picture[100][100];

to populate it, just use two for loops:

for ( int x = 0; x < 100; x++ ) {
for ( int y = 0; y < 100; y++ ) {
if ( rand() > 0.5 ) {
picture[x][y] = true;
}
else {
picture[x][y] = false;
}
}
}

if you want to write the image to a file, you can do the following:

for ( int x = 0; x < 100; x++ ) {
for ( int y = 0; y < 100; y++ ) {
if ( rand() > 0.5 ) {
Image.pixelColor( x, y, Color( "black" ));
}
else {
Image.pixelColor( x, y, Color("white"));
}
}
}
Image.write( "finalImage.gif" );

of course, that only makes one image. If you want multiple, add another for loop around that whole statement. To make a different file every time, you can do the following:

Image.write( "finalImage" . i . "gif" ); <-- that should work in Perl.

Hope this helps.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Generating random image

Post by fmw42 »

ImageMagick can generate random data images using +Noise Random, but note if you are using Q16 IM, then the data will be 16-bits in depth not just 8, so each pixel can have 65535 combination of graylevels. Thus you need to add -depth 8 to keep the bit depth at 8 (255 graylevels). In any case, I doubt you have enough storage to save every possible combination.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Generating random image

Post by anthony »

xc3ll wrote:if you want to make every possible variation of a black & white 100x100 image, you're going to need a lot of computing power. Thats 2^200 or 1.60693804 × 10^60 possible combinations.
Actually it is worse that than. a black and white image is basicakkly just one big binary number where each pixel is a binary digit (0=black 1=white). As there is 100x100 pixels that is a number that in 10,000 binary digits long. or 2^10,000 different images...

So unless you can store about 1.99*10^3010 images you are in trouble.
To give you an idea of the scope their are about 1x10^57 hydrogen atoms in the sun and though to be about 5x10^68 atoms in our galaxy, and 4x10^79 atoms in the universe!

So just to store your image collection you will need more storage space than GOD needed to create the universe, and then some!!!!!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Generating random image

Post by anthony »

Getting back to your problem however.

I suggest you take a look at morphology, part of which is about taking a pattern of pixels, and searching for similar patterns.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply