-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Blurring_a_part_of_an_image_in_Python.py
124 lines (61 loc) · 2.63 KB
/
Blurring_a_part_of_an_image_in_Python.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# coding: utf-8
# # Table of Contents
# <p><div class="lev1 toc-item"><a href="#Blurring-a-part-of-an-image-in-Python" data-toc-modified-id="Blurring-a-part-of-an-image-in-Python-1"><span class="toc-item-num">1 </span>Blurring a part of an image in Python</a></div><div class="lev2 toc-item"><a href="#Blur-all-the-image" data-toc-modified-id="Blur-all-the-image-11"><span class="toc-item-num">1.1 </span>Blur all the image</a></div><div class="lev2 toc-item"><a href="#Blur-only-an-area-of-the-image" data-toc-modified-id="Blur-only-an-area-of-the-image-12"><span class="toc-item-num">1.2 </span>Blur only an area of the image</a></div><div class="lev2 toc-item"><a href="#Conclusion" data-toc-modified-id="Conclusion-13"><span class="toc-item-num">1.3 </span>Conclusion</a></div>
# # Blurring a part of an image in Python
#
# This very short notebook shows how to open an image (eg a PNG image), and nicely blur a part of it.
# In[1]:
import numpy as np
import skimage
# In[2]:
get_ipython().run_line_magic('load_ext', 'watermark')
get_ipython().run_line_magic('watermark', '-v -m -a "Lilian Besson (Naereen)" -p numpy,skimage -g')
# ## Blur all the image
# Let's import one of the example image, and blur all of it using [`skimage.filters.gaussian`](http://scikit-image.org/docs/stable/api/skimage.filters.html#skimage.filters.gaussian).
# In[9]:
from skimage import data, io, filters
image = data.astronaut()
# In[10]:
def imshow(image):
io.imshow(image)
io.show()
# In[11]:
imshow(image)
# In[5]:
from skimage.filters import gaussian
# In[12]:
filtered_img = gaussian(image, sigma=1, multichannel=True)
imshow(filtered_img)
# In[13]:
filtered_img = gaussian(image, sigma=2, multichannel=True)
imshow(filtered_img)
# ## Blur only an area of the image
# In[17]:
image.shape
# In[71]:
def blur(image, x0, x1, y0, y1, sigma=1, imshowall=False):
x0, x1 = min(x0, x1), max(x0, x1)
y0, y1 = min(y0, y1), max(y0, y1)
im = image.copy()
sub_im = im[x0:x1,y0:y1].copy()
if imshowall: imshow(sub_im)
blur_sub_im = gaussian(sub_im, sigma=sigma)
if imshowall: imshow(blur_sub_im)
blur_sub_im = np.round(255 * blur_sub_im)
im[x0:x1,y0:y1] = blur_sub_im
return im
# In[72]:
filtered_img = blur(image, 80, 180, 170, 270, sigma=1)
imshow(filtered_img)
# In[76]:
filtered_img = blur(image, 80, 180, 170, 270, sigma=5)
imshow(filtered_img)
# In[73]:
filtered_img = blur(image, 80, 180, 170, 270, sigma=10)
imshow(filtered_img)
# In[74]:
filtered_img = blur(image, 80, 180, 170, 270, sigma=20)
imshow(filtered_img)
# ## Conclusion
#
# That's it.