-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshuffle.py
More file actions
57 lines (42 loc) · 1.23 KB
/
shuffle.py
File metadata and controls
57 lines (42 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding: utf-8 -*-
import sys
import os
import numpy as np
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import random
import Image
if len(sys.argv) < 4:
print "引数が間違っています。"
print "python shuffle.py 元ファイル 横分割数 縦分割数 [シード値]"
sys.exit(-1)
filename = sys.argv[1]
cols = int(sys.argv[2])
rows = int(sys.argv[3])
seed = 10
if len(sys.argv) >= 5:
seed = int(sys.argv[4])
random.seed(seed)
def split(img, w, h):
cols = np.hsplit(img, w)
return [np.vsplit(a, h) for a in cols]
def addNum(splitImgs):
return [[((x, y), img) for y, img in enumerate(col)] for x, col in enumerate(splitImgs)]
# 破壊的
def shuffle(splitImgs):
[random.shuffle(col) for col in splitImgs]
random.shuffle(splitImgs)
def connect(splitImgs):
return np.hstack([np.vstack([img for pos, img in col]) for col in splitImgs])
orig = mpimg.imread(filename)
print orig
split = addNum(split(orig, cols, rows))
shuffle(split)
newImg = connect(split)
pil = Image.fromarray(np.uint8(newImg * 255))
pil.save(os.path.splitext(filename)[0] + ".ppm")
for x, col in enumerate(split):
for y, img in enumerate(col):
print "moved %s to %s" % (img[0], (x, y))
plt.imshow(newImg)
plt.show()