here is the animation-transition.py \"\"\" This example demonstrates how to use
ID: 3712853 • Letter: H
Question
here is the animation-transition.py
"""
This example demonstrates how to use NumPy to do image transition.
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import sys
def image_load(filename):
return plt.imread(filename)
def image_gen(file1, file2, steps=30):
"""Generator for image arrays."""
img1 = image_load(file1) # load the two image files into ndarrays
img2 = image_load(file2)
if img1.shape != img2.shape:
print("Error: the two images have different shapes.", file=sys.stderr)
exit(2)
# go from img1 to img2 than back to img1. s varies from 0 to 1 and then back to 0:
svalues = np.hstack([np.linspace(0.0, 1.0, steps), np.linspace(1.0, 0, steps)])
# construct now the list of images, so that we don't have to repeat that later:
images = [np.uint8(img1 * (1.0 - s) + img2 * s) for s in svalues]
# get a new image as a combination of img1 and img2
while True: # repeat all images in a loop
for img in images:
yield img
fig = plt.figure()
# create image plot and indicate this is animated. Start with an image.
im = plt.imshow(image_load("florida-keys-800-480.jpg"), interpolation='none', animated=True)
# the two images must have the same shape:
imggen = image_gen("florida-keys-800-480.jpg", "Grand_Teton-800-480.jpg", steps=30)
# updatefig is called for each frame, each update interval:
def updatefig(*args):
global imggen
img_array = next(imggen) # get next image animation frame
im.set_array(img_array) # set it. FuncAnimation will display it
return (im,)
# create animation object that will call function updatefig every 60 ms
ani = animation.FuncAnimation(fig, updatefig, interval=60, blit=False)
plt.title("Image transformation")
plt.show()
Explanation / Answer
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
a.#def dog=data.puppy()
print("shape:", dog.shape0
print(values min/max : " , dog.min() , dog.max() )
plt.imshow(dog , interpolation =' nearest ');
shape :(400, 550,5)
values min/max: 0 231
dog[20:220, 20:220, :]=[255, 0 ,0]
# data types and image values
0 - 255 where 0 is black , 255 is white
0 - 1 where 0 is black , 1 is white
#Example
linear0 = np.linespace (0 ,1 , 2500).reshape( (50 , 50) )
linear1 = np.linespace (0 ,255 , 2500).reshape( (50 , 50) ).astype(np.uint8)
print("Linear0:", linear0.dtype, linear0.min() ,linear0.max( ) )
print("Linear1:", linear1.dtype, linear1.min() ,linear1.max( ) )
fig, (ax0 , ax1) = plt.subplots(1 , 2)
ax0. imshow(linear0,cmap='gray')
ax1. imshow(linear1,cmap='gray');
# images using matplotlib
from skimage import data
image = data.camera()
b.#default color map
plot.rcParams[image.cmap']='gray'
plt.rcParams['image.interpolation'] = ' nearest '
#grayscale imag
fig, (ax_jet, ax_gray)=plt.subplots(ncols=2,figsize=(10 , 5) )
ax_jet.imshow(image,cmap='jet')
ax_gray.imshow(image,cmap='gray');
# plot intensity(image, row):
red_values =...
green_values =....
blue_values =.....
plt.figure()
plt.plot(red_values, 'r')
plt.plot(green_values, 'g')
plt.plot(blue_values, 'b')
pass
#function
plot_intensity(dog,100)
plot_intensity(dog, 200)
c.# with parameter
imshow_params (image , cmap='jet', reverse_cmap=false, interpolation='nearest' ) :
fig, axes = plt .subplots (1 , 5 figsize=(15 , 4) )
if reverse_cmp:
cmap=cmap+ '_r '
axes[0].imshow(image, cmap='grey', interpolation='nearest ')
axes[0].set_title(original')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.