とらりもん - exercise 1 answer Diff

• Added parts are displayed like this.
• Deleted parts are displayed like this.

2018/05/28 Ishibashi

[[go back to Python training course exercise 1|http://pen.envr.tsukuba.ac.jp/~torarimon/?Python+training+course+-+exercise+1]]

! exercise 1.1
# Exercise 1.1.1
import PIL # import PIL module (library).
from PIL import Image # import Image module
img_PIL = Image.open("lena.png", 'r')

# Exercise 1.1.2
import matplotlib # import matplotlib module (library).
import matplotlib.pyplot as plt # import matplotlib.pyplot module.

# Exercise 1.1.3
type(img_PIL)
type(img_plt)

# Exercise 1.1.4
img_PIL_gray.save("lena.png"[:-4] + "_gray_PIL.png", 'PNG')

# Exercise 1.1.5
import numpy as np
img_size_x = img_plt.shape[0]
ex_5 = []
for i in range(0, img_size_x):
for p in range(0, img_size_x):
ex_5.append((img_plt[i][p][0]+img_plt[i][p][1]+img_plt[i][p][2])/3)

ex_5_np = np.array(ex_5)
img_size = ex_5_np.shape[0]
img_plt_result = []
for i in range(0, img_size):
img_plt_result.append(ex_5_np[i])
img_plt_result.append(ex_5_np[i])
img_plt_result.append(ex_5_np[i])

img_plt_result = np.array(img_plt_result)
img_plt_result = img_plt_result.reshape(512, 512, 3)

# plt.imshow(img_plt_result) # showing a image
# plt.show() # showing a image

plt.imshow(img_plt_result)
plt.savefig('lena_gray_plt')

! exercise 1.2
import PIL
from PIL import Image
import numpy as np
img = Image.open("lena.png", 'r')
print(img.size)

# Exercise 1.2.2
img_resize_Pr = img.resize((200,200))
img_resize_Pr.save("lena_PIL_resample.png", 'PNG')

# Exercise 1.2.3 (using PIL)
box = (158, 158, 358, 358)
img_resize_Pc = img.crop(box)
img_resize_Pc.save("lena_PIL_cutting.png", 'PNG')