# Animate crashes Mathematica

I'm editing my post by giving all the code I have until now. I'd like to write a program which "unravels" randomly and piece by piece a given image. At the moment, I'm using an image randomly created with Image[] (endImage), and I'm starting with a grey background (whitePart and beginImage). I divide the image in partNumber x partNumber parts (here 10):

partNumber = 10;
allParts = Flatten[Outer[List, Range[1, partNumber], Range[1, partNumber]], 1];
endImage = ImagePartition[
Image@RandomReal[1, {400, 400}]
,
400/partNumber];
whitePart = Image@Table[0.5, {400/partNumber}, {400/partNumber}];
beginImage = Table[whitePart, {partNumber}, {partNumber}];


(* After that, I create a random ordering of the unraveling of the parts with *)

replaceOrdering = RandomSample[allParts];


(* Then, a short function which replaces beginImage part number indexNumber with endImage part indexNumber: *)

replacePartWithNumber[tableOfParts_, indexNumber_] :=
ReplacePart[tableOfParts,
replaceOrdering[[indexNumber]] ->
endImage[[Apply[Sequence, replaceOrdering[[indexNumber]]]]]]


(* Creation of the list with recursion: *)

stage[1] = replacePartWithNumber[ beginImage, 1];
stage[n_] := replacePartWithNumber[stage[n - 1], n]


(* which finally gives the Animate command: *)

Animate[
ImageAssemble@stage[n]
,
{n, 1, Length@replaceOrdering, 1}
, DefaultDuration -> 10
, AnimationRepetitions -> 1]


This works for values of partNumber till ca. 25. But with partNumber = 40 or =50, Mathematica crashes immediately...

Any idea why this is? Any help greatly appreciated!

-
There is no way anyone can help you without knowing hoq sequence is defined. – m_goldberg Nov 19 '13 at 8:56
How can we judge if you provide no information on your sequence? Your error message does not seem to be memory-related, are the frames generated by a function or is sequence an array of images? – Yves Klett Nov 19 '13 at 8:56
My bad: I'm editing my post right now! – Gabriel Nov 19 '13 at 9:32
You start your animation with n = 0. Perhaps that's what's wrong... – cormullion Nov 19 '13 at 9:58
@cormullion: even when changing the start of the animation to n=1, Mathematica still crashes... – Gabriel Nov 19 '13 at 10:01

With large values for partNumber, I get the message

$RecursionLimit::reclim: Recursion depth of 1024 exceeded. >> The fix would be to increase $RecursionLimit.

Animate[Block[{\$RecursionLimit = 10000}, ImageAssemble@stage[n]],
{n, 1, Length@replaceOrdering, 1}, DefaultDuration -> 10,
AnimationRepetitions -> 1]


There would be no need for this fix, if the recursion were eliminated, like this:

stage[n_] :=
ReplacePart[beginImage,
Thread[Take[replaceOrdering, n] -> Extract[endImage, Take[replaceOrdering, n]]]]

-