Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import random
- from PIL import Image
- def get_axes(image, seed):
- random.seed(seed)
- x_axis = random.sample(range(image.width), image.width)
- y_axis = random.sample(range(image.height), image.height)
- return x_axis, y_axis
- def scramble(image, seed):
- x_axis, y_axis = get_axes(image, seed)
- dest_image = Image.new('RGB', (image.width, image.height))
- for i in range(image.width):
- for j in range(image.height):
- pixel = image.getpixel(xy=(i, j))
- dest_image.putpixel((
- x_axis[i],
- y_axis[j]
- ), pixel)
- return dest_image
- def unscramble(image, seed):
- x_axis, y_axis = get_axes(image, seed)
- dest_image = Image.new('RGB', (image.width, image.height))
- for i in range(image.width):
- for j in range(image.height):
- pixel = image.getpixel(xy=(x_axis[i], y_axis[j]))
- dest_image.putpixel((i, j), pixel)
- return dest_image
- def get_rand_nums(image, seed):
- random.seed(seed)
- return random.sample(
- range(image.width * image.height), image.width * image.height)
- def scramble2(image, seed):
- rand_nums = get_rand_nums(image, seed)
- dest_image = Image.new('RGB', (image.width, image.height))
- index = 0
- for i in range(image.width):
- for j in range(image.height):
- pixel = image.getpixel(xy=(i, j))
- dest_image.putpixel((
- rand_nums[index] % image.width,
- rand_nums[index] // image.width
- ), pixel)
- index += 1
- return dest_image
- def unscramble2(image, seed):
- rand_nums = get_rand_nums(image, seed)
- dest_image = Image.new('RGB', (image.width, image.height))
- index = 0
- for i in range(image.width):
- for j in range(image.height):
- pixel = image.getpixel(
- xy=(rand_nums[index] % image.width, rand_nums[index] // image.width))
- dest_image.putpixel((i, j), pixel)
- index += 1
- return dest_image
- if __name__ == "__main__":
- image_names = os.listdir(path='./source/')
- seed=123
- for image_name in image_names:
- origin_image = Image.open('./source/'+image_name)
- dest_image = scramble(origin_image, seed)
- dest_image.save('./dist/v1/'+image_name)
- dest_image2 = scramble2(origin_image,seed)
- dest_image2.save('./dist/v2/'+image_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement