Skip to content

Commit 9dd39d4

Browse files
code style corrections and few other small edits
Co-authored-by: Marianne Corvellec <[email protected]>
1 parent bb1bf0d commit 9dd39d4

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

episodes/fig/source/06-blurring/create_blur_animation.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
###
1010

1111
### USAGE
12-
# The script requires the following Python packages:
12+
# The script was written in Python 3.12 and required the following Python packages:
1313
# - numpy==2.2.3
1414
# - scipy==1.15.2
1515
# - matplotlib==3.10.1
@@ -83,21 +83,21 @@ def update(frame):
8383
# MAIN PROGRAM
8484
if __name__ == "__main__":
8585

86-
print("Creating blurred animation with kernel size:", kernel_size)
86+
print(f"Creating blurring animation with kernel size: {kernel_size}")
8787

8888
# Load image
8989
img = plt.imread(input_file)
9090

9191
### HERE WE USE THE CONVOLVE FUNCTION TO GET THE FINAL BLURRED IMAGE
9292
# I chose a simple mean filter (equal kernel weights)
93-
kernel = np.ones(shape=(kernel_size, kernel_size)) / kernel_size ** 2 # create kernel
94-
# convolve the image i.e. apply mean filter
95-
img_convolved = convolve(img, kernel, mode='constant', cval=0) # pad borders with zero like below for consistency
93+
kernel = np.ones(shape=(kernel_size, kernel_size)) / kernel_size ** 2 # create kernel
94+
# convolve the image, i.e., apply mean filter
95+
img_convolved = convolve(img, kernel, mode='constant', cval=0) # pad borders with zero like below for consistency
9696

9797

9898
### HERE WE CONVOLVE MANUALLY STEP-BY-STEP TO CREATE ANIMATION
99-
img_pad = np.pad(img, (int(np.ceil(kernel_size/2) - 1), int(np.ceil(kernel_size/2) - 1))) # Pad image to deal with borders
100-
new_img = np.zeros(img.shape, dtype=np.uint16) # this will be the blurred final image
99+
img_pad = np.pad(img, (int(np.ceil(kernel_size/2) - 1), int(np.ceil(kernel_size/2) - 1))) # Pad image to deal with borders
100+
new_img = np.zeros(img.shape, dtype=np.uint16) # this will be the blurred final image
101101

102102
# add first frame with complete blurred image for print version of GIF
103103
all_frames = [img_convolved]
@@ -107,7 +107,7 @@ def update(frame):
107107
for frame in range(total_frames):
108108
row = (frame % total_frames) // (img_pad.shape[0] - kernel_size + 1) # row index
109109
col = (frame % total_frames) % (img_pad.shape[1] - kernel_size + 1) # col index
110-
img_chunk = img_pad[row : row + kernel_size, col : col + kernel_size] # get current image chunk inside the kernel
110+
img_chunk = img_pad[row:row + kernel_size, col:col + kernel_size] # get current image chunk inside the kernel
111111
new_img[row, col] = np.mean(img_chunk).astype(np.uint16) # calculate its mean -> mean filter
112112
all_frames.append(new_img.copy()) # append to animation frames list
113113

@@ -116,16 +116,16 @@ def update(frame):
116116

117117
### FROM HERE WE START CREATING THE ANIMATION
118118
# Initialize canvas
119-
f, (ax1, ax2) = plt.subplots(1,2, figsize=(10,5))
119+
f, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10, 5))
120120

121121
# Display the padded image -> this one won't change during the animation
122-
ax1.imshow(img_pad, cmap='gray')
122+
ax1.imshow(img_pad, cmap="gray")
123123
# Initialize the blurred image -> this is the first frame with already the final result
124-
im = ax2.imshow(img_convolved, animated=True, cmap='gray')
124+
im = ax2.imshow(img_convolved, animated=True, cmap="gray")
125125

126126
# Define rectangular patches to identify moving kernel
127-
k_rect = p.Rectangle((-0.5,-0.5), kernel_size, kernel_size, linewidth=2, edgecolor=kernel_color, facecolor='none', alpha=0.8) # kernel rectangle
128-
c_rect1 = p.Rectangle(((kernel_size/2 - 1), (kernel_size/2 - 1)), 1, 1, linewidth=2, edgecolor=center_color, facecolor='none') # central pixel rectangle
127+
k_rect = p.Rectangle((-0.5, -0.5), kernel_size, kernel_size, linewidth=2, edgecolor=kernel_color, facecolor="none", alpha=0.8) # kernel rectangle
128+
c_rect1 = p.Rectangle(((kernel_size/2 - 1), (kernel_size/2 - 1)), 1, 1, linewidth=2, edgecolor=center_color, facecolor="none") # central pixel rectangle
129129
# Add them to the figure
130130
ax1.add_patch(k_rect)
131131
ax1.add_patch(c_rect1)
@@ -148,7 +148,7 @@ def update(frame):
148148
ani = FuncAnimation(
149149
f, update,
150150
frames=range(total_frames),
151-
interval=50, # we could change the animation speed
151+
interval=50, # we could change the animation speed
152152
init_func=init,
153153
blit=True
154154
)

0 commit comments

Comments
 (0)