# 实现对375*500的图片进行分割
import numpy as np
from PIL import Image
img = Image.open("1.jpg")
img = img.resize((500,376))
# img.show()
img_data = np.array(img)
# print(img_data.shape)
img_data = img_data.reshape(2,376//2,2,500//2,3)
img_data = img_data.transpose(0,2,1,3,4)
# print(img_data.shape)
img_data = img_data.reshape(-1,188,250,3)
# print(img_data.shape)
imgs = np.split(img_data,4,axis=0)
for i ,img_d in enumerate(imgs):
print(img_d.shape)
print(img_d[0,:,:,:].shape)
img = Image.fromarray(img_d[0])
img.save(f"s{i}.jpg")
# 将图片进行组合
import numpy as np
from PIL import Image
img0 = np.array(Image.open("s0.jpg"))
img1 = np.array(Image.open("s1.jpg"))
img2 = np.array(Image.open("s2.jpg"))
img3 = np.array(Image.open("s3.jpg"))
imgs = np.concatenate([img0[None, ...], img1[None, ...], img2[None, ...], img3[None, ...]], axis=0)
# print(imgs.shape)
imgs = imgs.reshape(2, 2, 188, 250, 3)
imgs = imgs.transpose(0, 2, 1, 3, 4)
imgs = imgs.reshape(-1, 250 * 2, 3)
img = Image.fromarray(imgs)
img.show()
# print(imgs.shape)
'''
numpy 如何在算术运算期间处理具有不同形状的数组。受某些约束
的影响,较小的数组在较大的数组上“广播”,以便它们具有兼容的形状。广播提供了一种矢量化数组操
作的方法,以便在C而不是Python中进行循环。它可以在不制作不必要的数据副本的情况下实现这一
点,通常导致高效的算法实现
'''
import numpy as np
a = np.array([5])
print(np.tile(a,[3,1]))
b = np.array([5,6])
print(np.tile(b,[2,3]))