Python: rolling an array multiple times and saving each iteration as an image
<p>I have multiple images that I am taking in with a for loop. I want to obtain 5 pseudo-layers for each image by rolling the initial by one pixel in both x and y and saving the rolled image each time. </p>

<p>I'm practicing with one image making sure I can get five layers out of it but no joy.</p>

<p>My code is below but it is only saving the first image; output is pl_0.tif</p>

<pre><code>list_frames = glob.glob('*.gif')
for index, fname in enumerate(list_frames):
    im = Image.open(fname)
    shift=1
    imary0 = np.array(im) # initial image (first layer)
    imary1x = np.roll(imary0, shift, axis=0) # shift image by one pixel to create second layer
    imary1xy = np.roll(imary1x, shift, axis=1)
    imary2x = np.roll(imary1xy, shift, axis=0) # shift previous image again to create third layer
    imary2xy = np.roll(imary2x, shift, axis=1)
    imary3x = np.roll(imary2xy, shift, axis=0) # shift previous image again to create fourth layer
    imary3xy = np.roll(imary3x, shift, axis=1)
    imary4x = np.roll(imary3xy, shift, axis=0) # shift previous image again to create fifth layer
    imary4xy = np.roll(imary4x, shift, axis=1)
    im0 = Image.fromarray(imary0)
    im0.save('pl_{}.tif'.format(index))
    im1 = Image.fromarray(imary1xy)
    im1.save('pl_{}.tif'.format(index))
    im2 = Image.fromarray(imary2xy)
    im2.save('pl_{}.tif'.format(index))
    im3 = Image.fromarray(imary3xy)
    im3.save('pl_{}.tif'.format(index))
    im4 = Image.fromarray(imary4xy)
    im4.save('pl_{}.tif'.format(index))
    im.close()
</code></pre>

<p>Any suggestions? (And please feel free to ridicule me if this is an easy fix)</p>

<p>You save all 5 images in the same output file, which means that you get only one image, <code>im4</code></p>

<pre><code>im0.save('pl_{}.tif'.format(index))
im1 = Image.fromarray(imary1xy)
im1.save('pl_{}.tif'.format(index))
im2 = Image.fromarray(imary2xy)
im2.save('pl_{}.tif'.format(index))
im3 = Image.fromarray(imary3xy)
im3.save('pl_{}.tif'.format(index))
im4 = Image.fromarray(imary4xy)
im4.save('pl_{}.tif'.format(index))
</code></pre>

<p>(note that index is not modified until next loop)</p>

<p>quickfix:</p>

<pre><code>im0.save('pl_{}0.tif'.format(index))
im1 = Image.fromarray(imary1xy)
im1.save('pl_{}1.tif'.format(index))
im2 = Image.fromarray(imary2xy)
im2.save('pl_{}2.tif'.format(index))
im3 = Image.fromarray(imary3xy)
im3.save('pl_{}3.tif'.format(index))
im4 = Image.fromarray(imary4xy)
im4.save('pl_{}4.tif'.format(index))
</code></pre>

python numpy pillow