Skip to content

Instantly share code, notes, and snippets.

@CookiePLMonster
Last active February 21, 2019 21:01
Show Gist options
  • Save CookiePLMonster/f845e40a644fb198c645602736e9df9d to your computer and use it in GitHub Desktop.
Save CookiePLMonster/f845e40a644fb198c645602736e9df9d to your computer and use it in GitHub Desktop.
Random shuffle on image pixels (based on https://stackoverflow.com/a/52437794/9214270)
import scipy.ndimage
import scipy.misc
import numpy as np
import sys
import os.path
if len(sys.argv) < 2:
exit(1)
numShuffles = 1 if len(sys.argv) < 3 else int(sys.argv[2])
#Loads an arbitrary RGB image from the misc library
rgbImg = scipy.ndimage.imread(sys.argv[1])
# doc on shuffle: multi-dimensional arrays are only shuffled along the first axis
# so let's make the image an array of (N,3) instead of (m,n,3)
rndImg2 = np.reshape(rgbImg, (-1, rgbImg.shape[2]))
for i in range(numShuffles):
#now shuffle
np.random.shuffle(rndImg2)
#and reshape to original shape
rdmImg = np.reshape(rndImg2, rgbImg.shape)
path, filename = os.path.split(sys.argv[1])
scipy.misc.imsave(os.path.join(path, 'shuffled' + str(i+1) + '_' + filename), rdmImg)
rndImg2 = rdmImg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment