import imageio
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
def showimg(img, isgray=False):
plt.axis("off")
if isgray == True:
plt.imshow(img, cmap='gray')
else:
plt.imshow(img)
lena = imageio.imread("lena.jpg")
gray_lena = lena
gray_lena = np.array(gray_lena, dtype=np.int32)
gray_lena[...,0] = 0
gray_lena[...,2] = 0
gray_lena = np.sum(gray_lena, axis=2)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.title('灰度变换函数图像')
plt.xlabel('像素值')
plt.ylabel('变换后像素值')
x1 = np.arange(0, 256)
y1 = np.arange(0, 256)
f1, = plt.plot(x1, y1, '--')
y2 = 255 - x1
f2, = plt.plot(x1, y2, 'y')
y3 = (100.0/255)*x1 + 100
f3, = plt.plot(x1, y3, 'r:')
y4 = 255.0*(x1/255.0)**2
f4, = plt.plot(x1, y4, 'm--')
plt.legend((f1, f2, f3, f4), ('y=x','y=255-x','y=(100.0/255)*x+100','y=255.0*(x/255.0)**2'),loc='upper center')
plt.show()
g1 = 255 - gray_lena
g2 = (100.0/255)*gray_lena +100
plt.subplot(221)
showimg(lena,True)
plt.title("Source Image")
plt.subplot(222)
showimg(gray_lena,True)
plt.title("Gray Image")
plt.subplot(223)
showimg(g1,True)
plt.title("g1 Image")
plt.subplot(224)
showimg(g2,True)
plt.title("g2 Image")
plt.show()
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
def getimg():
return Image.open("D:\Pycharm\example\lena.jpg")
def showimg(img, isgray=False):
plt.axis("off")
if isgray == True:
plt.imshow(img, cmap='gray')
else:
plt.imshow(img)
plt.show()
showimg(getimg())
im = getimg()
im_gray = im.convert('L')
showimg(im_gray,True)
im = getimg()
im = np.array(im)
im[:,:,0] = im[:,:,0]*0.3
im[:,:,1] = im[:,:,1]*0.59
im[:,:,2] = im[:,:,2]*0.11
im = np.sum(im, axis=2)
showimg(Image.fromarray(im), True)
im4 = getimg()
im4 = np.array(im4, dtype=np.int32)
im4[...,1] = 0
im4[...,2] = 0
im4 = np.sum(im4, axis=2)
showimg(Image.fromarray(im4), True)