Who am I?

Here is a little snippet on how to make a fun game with the help of Mathematica: “Who am I?” An animated gif which will slowly reveal its contents… A guessing game for the whole family!

We start off by loading a random picture, and split it in parts with the help of ImagePartition

Img = Import["/home/gooby/pic.jpg"];
ImgDim = ImageDimensions[Img];
AllBlack = 
  Graphics[Rectangle[{0, 0}, ImgDim], ImageSize -> ImgDim, 
   PlotRangePadding -> 0];
Div = 8;
Parts = ImagePartition[Img, ImgDim/Div];

We then construct the individual steps by making a randomized version of the indices of Parts using a little recursion

Clear@AvailableSections
Clear@RandomIndex

AvailableSections[0] := 
 Flatten[Table[{i, j}, {i, 1, Div}, {j, 1, Div}], 1]
RandomIndex[n_] := RandomIndex[n] = 
  RandomInteger[{1, Length[AvailableSections[n - 1]]}]
AvailableSections[n_] := 
 Delete[AvailableSections[n - 1], RandomIndex[n]]

And define the individual steps (also recursively)

Clear@Reveal
Clear@ImageStep

Reveal[P_, 0] := AllBlack
Reveal[P_, n_] := 
 ImageCompose[Reveal[P, n - 1], 
  Parts[[P[[n]][[2]]]][[P[[n]][[1]]]],
  {Floor[-ImgDim[[1]]/(2 Div) + P[[n]][[1]] ImgDim[[1]]/Div], 
   Floor[ImgDim[[2]] + ImgDim[[2]]/(2 Div) - P[[n]][[2]] ImgDim[[2]]/Div]}]


ImageStep[0] := AllBlack
ImageStep[n_] := Reveal[RandomizedSeq, n]

That’s it. Watch by using

Animate[ImageStep[n], {n, 0, Div^2, 1}]

Exporting a gif works like this

ImgTable = Table[ImageStep[n], {n, 0, Div^2, 1}]
Export["/home/gooby/pic.gif", ImgTable, "DisplayDurations" -> 1]

 

ls

Advertisements

About goobypl5

pizza baker, autodidact, particle physicist
This entry was posted in Fun, Mathematica and tagged , , , . Bookmark the permalink.

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s