from PIL import Image
import matplotlib.pyplot as plt
img=Image.open("lena.tiff")
img.save("test.tiff")
img.save("lena.jpg")
img.save("lena.bmp")
img1=Image.open("lena.jpg")
img2=Image.open("lena.bmp")
print("image:",img.format)
print("image1:",img1.format)
print("image2:",img2.format)
print("\n")
print("image:",img.size)
print("image1:",img1.size)
print("image2:",img2.size)
print("\n")
print("image:",img.mode)
print("image1:",img1.mode)
print("image2:",img2.mode)
plt.figure(figsize=(5,5))
plt.imshow(img)
plt.figure(figsize=(15,5))
plt.subplot(131)
plt.axis("off")
plt.imshow(img)
plt.title(img.format)
plt.subplot(132)
plt.axis("off")
plt.imshow(img1)
plt.title(img1.format)
plt.subplot(133)
plt.axis("off")
plt.imshow(img2)
plt.title(img2.format)
img_L=img.convert("L")
img_L.save("lena_L.bmp")
img_L=Image.open("lena_L.bmp")
plt.figure(figsize=(15,15))
plt.imshow(img_L)
img_r,img_g,img_b=img.split()
plt.figure(figsize=(10,10))
plt.subplot(221)
plt.axis("off")
plt.imshow(img_r,cmap="gray")
plt.title("R",fontsize=20)
plt.subplot(222)
plt.axis("off")
plt.imshow(img_g,cmap="gray")
plt.title("G",fontsize=20)
plt.subplot(223)
plt.axis("off")
plt.imshow(img_b,cmap="gray")
plt.title("B",fontsize=20)
img_rgb=Image.merge("RGB",[img_r,img_g,img_b])
plt.subplot(224)
plt.axis("off")
plt.imshow(img_rgb)
plt.title("RGB",fontsize=20)
import numpy as np
arr_img=np.array(img)
print("shape:",arr_img.shape,"\n")
print(arr_img)
img_L=Image.open("lena_L.bmp")
arr_img_L=np.array(img_L)
print("shape:",arr_img_L.shape)
print(arr_img_L)
arr_img_new=255-arr_img_L
plt.figure(figsize=(10,5))
plt.subplot(121)
plt.axis("off")
plt.imshow(arr_img_L,cmap="gray")
plt.subplot(122)
plt.axis("off")
plt.imshow(arr_img_new,cmap="gray")
plt.figure(figsize=(10,10))
img_small=img.resize((64,64))
plt.imshow(img_small)
plt.show
img_region=img.crop((100,100,400,400))
plt.imshow(img_region)
import tensorflow as tf
mnist=tf.keras.datasets.mnist
(train_x,train_y),(test_x,text_y)=mnist.load_data()
print("Training set:",len(train_x))
print("Testing set:",len(test_x))
print("\n")
print("train_x:",train_x.shape,train_x.dtype)
print("train_y:",train_y.shape,train_y.dtype)
train_x[0]
plt.figure(figsize=(1,1))
plt.imshow(train_x[0],cmap="gray")
plt.axis("off")
plt.show()
train_y[0]
for i in range(4):
num=np.random.randint(1,60000)
plt.subplot(1,4,i+1)
plt.axis("off")
plt.imshow(train_x[num],cmap="gray")
plt.title(train_y[num])
plt.show()