stough 202-

Making an animation similar to the interactive slider in the spatial resolution demo.
Some pointers:
Had some issues trying to pad the figure correctly so that that animation showed the changing axis titles without putting too much padding on the other sides. Turns out setting the bottom of the rect argument to a slightly negative value allows the axis titles to be visible. Another issue was that the first frame of the saved gif had transparent background. As per here and here, I saw to add the savefig_kwargs dictionary to the gif saving code, forcing transparency off and sticking to white background.
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from skimage.transform import rescale
from matplotlib import animation, rc
from IPython.display import HTML, Imagerc('animation', html='html5')# Ripped from the spatial_resolution demo
def downandup(I, factor):
# downscale
sI = rescale(I, 1/factor,
order=0, # use nearest neighbor
anti_aliasing=False,
channel_axis=-1)
# rescale back up
reI = rescale(sI, factor,
order=0, # use nearest neighbor sampling
anti_aliasing=False,
channel_axis=-1)
return reI# Load the image
I = plt.imread('../dip_pics/grandCanyon.jpg')
Ire = downandup(I, factor=1)# First set up the figure, the axis, and the plot element we want to animate
fig, ax = plt.subplots(1,2, figsize=(10,4))
[a.axes.get_xaxis().set_visible(False) for a in ax];
[a.axes.get_yaxis().set_visible(False) for a in ax];
plt.tight_layout(rect=(0,-.1, 1,1))
ax[0].imshow(I, interpolation=None)
ax[0].set_title('Original')
# ax[1].imshow(Ire, interpolation=None)
# ax[1].set_title('Quarter Res')
rdisp = ax[1].imshow(I, interpolation=None)
rtext = ax[1].set_title(f'Factor: {1:d}')
def init():
rdisp.set_data(Ire)
rtext.set_text(f'Factor: {1:d}')
return (rdisp, rtext,)# animation function. This is called sequentially
def animate(i):
Ire = downandup(I, factor=2**i)
rdisp.set_data(Ire)
rtext.set_text(f'Factor: {2**i:d}')
return (rdisp, rtext,)# call the animator. blit=True means only re-draw the parts that
# have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=9, interval=350, blit=True)# Outputing this directly can create a lot of problems depending on your environment
# animanim.save('../dip_outs/spatial_res_animation.gif', writer='pillow', fps=2,
savefig_kwargs={'transparent': False, 'facecolor': 'white'})Image(url='../dip_outs/spatial_res_animation.gif')Loading...