Created
May 23, 2017 01:11
-
-
Save intrd/4cc3eab3a5d8cf815ca69b2616000f4a to your computer and use it in GitHub Desktop.
Solution for SHX10 : misc100-thx (script to vertical slice 1px lines, shuffle and rejoin)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Solution for SHX10 : misc100-thx (script to vertical slice 1px lines, shuffle and rejoin) | |
# @author intrd - http://dann.com.br/ | |
# @license Creative Commons Attribution-ShareAlike 4.0 International License - http://creativecommons.org/licenses/by-sa/4.0/ | |
from __future__ import division | |
import PIL, Image, random, math, os, time | |
import numpy as np | |
from shutil import copyfile | |
def rejoin(slices,outfilename): | |
list_im = slices | |
imgs = [ PIL.Image.open(i) for i in list_im ] | |
min_shape = sorted( [(np.sum(i.size), i.size ) for i in imgs])[0][1] | |
imgs_comb = np.hstack( (np.asarray( i.resize(min_shape) ) for i in imgs ) ) | |
# horizontal stacking | |
# imgs_comb = PIL.Image.fromarray( imgs_comb) | |
# imgs_comb.save(outfilename) | |
# vertical stacking | |
imgs_comb = np.vstack( (np.asarray( i.resize(min_shape) ) for i in imgs ) ) | |
imgs_comb = PIL.Image.fromarray(imgs_comb) | |
imgs_comb.save(outfilename) | |
def slice(image_path, out_name, outdir, slice_size): | |
img = Image.open(image_path) | |
width, height = img.size | |
upper = 0 | |
left = 0 | |
slices = int(math.ceil(height/slice_size)) | |
count = 1 | |
slcs=[] | |
for slice in range(slices): | |
if count == slices: | |
lower = height | |
else: | |
lower = int(count * slice_size) | |
bbox = (left, upper, width, lower) | |
working_slice = img.crop(bbox) | |
upper += slice_size | |
working_slice.save(os.path.join(outdir, "slice_"+out_name+"_"+str(count)+".png")) | |
slcs.append(os.path.join(outdir,"slice_"+out_name+"_"+str(count)+".png")) | |
count +=1 | |
return slcs | |
if __name__ == '__main__': | |
slices = slice("shuffled1.png","test1", os.getcwd()+"/slices", 1) | |
pieces=[] | |
for i in range(100): | |
print i | |
random.shuffle(slices) | |
path=os.path.join(os.getcwd()+"/slices","joined"+str(i)+".jpg") | |
pieces.append(path) | |
rejoin(slices,path) | |
rejoin(pieces,"flag.jpg") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment