使用以下代码,我将图像样本划分为带有声明边的正方形,并在以后对其进行洗牌。似乎要困难得多的任务是使用对角线及其随机替换将矩形图像分成四个三角形,其中上一个带底,右个带左。
我只想尝试使用PIL,numpy和matplotlib。 欢迎提供最简单的解决方案的任何想法。
from IPython.display import display
from PIL import Image
import random
imageOriginal = Image.open("lenargb.jpg")
width, height = imageOriginal.size
if(width > height):
size = height
else:
size = width
try:
ind = int(input('Number of squares aside: '))
except ValueError:
print('Bad input!')
sys.exit(1)
scale = size / ind
size = scale * ind
box = (0, 0, size, size)
cropped = imageOriginal.crop(box)
imageMove = imageOriginal.crop(box)
imageColor = imageMove
imageRotation = imageMove
tabMove = []
tabColor = []
tabRotation = []
px = 0
py = 0
numSquares = ind * ind
for x in range(numSquares):
tabMove.append(cropped.crop((px, py, px+scale, py+scale)))
tabColor.append(cropped.crop((px, py, px+scale, py+scale)))
tabRotation.append(cropped.crop((px, py, px+scale, py+scale)))
px = scale + px
if(px == size):
px = 0
py = py + scale
random.shuffle(tabMove)
px=0
py=0
for x in range(numSquares):
imageMove.paste(tabMove[x], (int(px), int(py), int(px+scale), int(py+scale)))
px = scale + px
if(px == size):
px = 0
py = py + scale
display(imageMove)