import matplotlib.pyplot as plt
import numpy as np
img=plt.imread('test.jpg')
print(type(img))
print(img.shape)
plt.imshow(img)
plt.show()
plt.imsave('test-saved.jpg',img)
def crop (image,x1,y1,x2,y2):
return image[x1:x2,y1:y2]
plt.imshow(crop(img,100,300,300,500))
plt.show()
def flip(image, h=True):
""" Flip function, default horizontally, h=False to flip vertically"""
if h:
return image[:,::-1,:]
else:
return image[::-1,:,:]
fig=plt.figure
fig(figsize=(8, 6), dpi=80)
plt.subplot(1,2,1)
plt.imshow(flip(img))
plt.subplot(1,2,2)
plt.imshow(flip(img,h=False))
plt.show()
help (flip)
#our comments are useful ;)
plt.imshow(img[::2,::,::])
plt.show()
plt.imshow(img[::,::2,::])
plt.show()
#from now on we'll work only using 0:1 float values as image data to reduce code and increase readability.
img=img/255 #from now on we'll move to the float type format
def brightness (image,factor):
""" brightness change function, factor must be positive"""
if factor<0:
raise NameError('only positive values for factor')
i=image.copy()
i*=factor
i[i>1]=1
return i
fig(figsize=(10, 7), dpi=80)
plt.subplot(1,2,1)
plt.imshow(img)
plt.subplot(1,2,2)
plt.imshow(brightness(img,1.5))
plt.show()
blue_component=img.copy()
blue_component[::,::,0:2]=0
plt.imshow(blue_component)
plt.show()
red_component=img.copy()
red_component[::,::,1:3]=0
green_component=img.copy()
green_component[::,::,2]=0
green_component[::,::,0]=0
fig,ax=plt.subplots(1,3,figsize=(15,7))
i=0
for _ in (red_component,green_component,blue_component):
ax[i].imshow(_)
ax[i].axis('off')
i+=1
plt.show()
img3=img[::,::,0]
img4=img[::,::,1]
img5=img[::,::,2]
from matplotlib.pyplot import figure
fig,ax=plt.subplots(1,3,figsize=(15,5))
i=0
for _ in (img3,img4,img5):
ax[i].imshow(_,cmap='gray')
ax[i].axis('off')
i+=1
plt.show()
imgRed=img[::,::,0]
imgGreen=img[::,::,1]
imgBlue=img[::,::,2]
imgLum=(imgRed*.2126+imgGreen*.7152+imgBlue*.0722)
imgAvg=(imgRed+imgGreen+imgBlue)/3
fig,ax=plt.subplots(1,3,figsize=(16,6))
ax[0].title.set_text('Original')
ax[0].imshow(img)
ax[1].title.set_text('Averaged')
ax[1].imshow(imgAvg[::,::],cmap='gray')
ax[2].title.set_text('Luminance')
ax[2].imshow(imgLum[::,::],cmap='gray')
plt.show()
for i in range(10,23,4):
figure(figsize=(3,3))
plt.imshow(img[::i,::i],cmap='gray')
plt.axis('off')
plt.show()
def contrast(image,t):
x=image.copy()
x[x<t]=0
x[x>=t]=1
return x
plt.imshow(contrast(imgLum,0.5),cmap='gray')
def sigma(x,k):
return 1/(1+np.exp(-(x-0.5)*k))
f,ax =plt.subplots(3,3,figsize=(12,8))
r=0
c=0
for i in range(1,10):
z=sigma(imgLum,i*2)
ax[r,c].imshow(z,cmap='gray')
c+=1
if c>2:
c=0
r+=1
#same thing here on the full color image, note how the numpy broadcast the sigmoid to all color layer seamlessly
f,ax =plt.subplots(3,3,figsize=(12,8))
r=0
c=0
for i in range(1,10):
z=sigma(img,i*2)
ax[r,c].imshow(z)
c+=1
if c>2:
c=0
r+=1
import numpy as np
def filmgrain(image,g):
im=image.copy()
noise=(np.random.random(image.shape)*g)-g/2
im=im+noise
im[im<0]=0
im[im>1]=1
return im
fig,ax=plt.subplots(1,2,figsize=(16,6))
ax[0].imshow(filmgrain(imgLum,0.5),cmap='gray')
ax[1].imshow(imgLum,cmap='gray')
plt.show()
def filmgrainNormal(image,g):
im=image.copy()
x,y=im.shape
noise=np.random.randn(x,y)*g
im=im+noise
im[im<0]=0
im[im>1]=1
return im
plt.imshow(filmgrainNormal(imgLum,0.2),cmap='gray')
dithering=contrast(filmgrainNormal(imgLum,0.1),0.5)
plt.imshow(dithering,cmap='gray')
#look at the content of our image, only 0s and 1s are present
dithering
def nuance(img, channel, correction):
""" Nuance function, channel possible values 0,1 or 2, correction must be a positive"""
imcopy=img.copy()
imcopy[:,:,channel]=imcopy[:,:,channel]*correction
imcopy[imcopy>1]=1 #clip control
return imcopy
#let's increase the blue channel +80%
plt.imshow(nuance(img,2,1.8))
plt.show()
f, axarr = plt.subplots(3,3,figsize=(12,8))
for channel in (0,1,2):
x=0
for i in (2.5,.75,0.2):
#plt.subplot(1,1+i//3,1+i//3)
axarr[channel,x].imshow(nuance(img,channel,i))
axarr[channel,x].axis('off')
x+=1
f.tight_layout()