Advertisement
GSlacker

Image Scrambler

Sep 28th, 2022 (edited)
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | Source Code | 0 0
  1. import os
  2. import random
  3. from PIL import Image
  4.  
  5. def get_axes(image, seed):
  6.     random.seed(seed)
  7.     x_axis = random.sample(range(image.width), image.width)
  8.     y_axis = random.sample(range(image.height), image.height)
  9.     return x_axis, y_axis
  10.  
  11. def scramble(image, seed):
  12.     x_axis, y_axis = get_axes(image, seed)
  13.     dest_image = Image.new('RGB', (image.width, image.height))
  14.     for i in range(image.width):
  15.         for j in range(image.height):
  16.             pixel = image.getpixel(xy=(i, j))
  17.             dest_image.putpixel((
  18.                 x_axis[i],
  19.                 y_axis[j]
  20.             ), pixel)
  21.     return dest_image
  22.  
  23. def unscramble(image, seed):
  24.     x_axis, y_axis = get_axes(image, seed)
  25.     dest_image = Image.new('RGB', (image.width, image.height))
  26.     for i in range(image.width):
  27.         for j in range(image.height):
  28.             pixel = image.getpixel(xy=(x_axis[i], y_axis[j]))
  29.             dest_image.putpixel((i, j), pixel)
  30.     return dest_image
  31.  
  32. def get_rand_nums(image, seed):
  33.     random.seed(seed)
  34.     return random.sample(
  35.         range(image.width * image.height), image.width * image.height)
  36.  
  37. def scramble2(image, seed):
  38.     rand_nums = get_rand_nums(image, seed)
  39.     dest_image = Image.new('RGB', (image.width, image.height))
  40.     index = 0
  41.     for i in range(image.width):
  42.         for j in range(image.height):
  43.             pixel = image.getpixel(xy=(i, j))
  44.             dest_image.putpixel((
  45.                 rand_nums[index] % image.width,
  46.                 rand_nums[index] // image.width
  47.             ), pixel)
  48.             index += 1
  49.     return dest_image
  50.  
  51.  
  52. def unscramble2(image, seed):
  53.     rand_nums = get_rand_nums(image, seed)
  54.     dest_image = Image.new('RGB', (image.width, image.height))
  55.     index = 0
  56.     for i in range(image.width):
  57.         for j in range(image.height):
  58.             pixel = image.getpixel(
  59.                 xy=(rand_nums[index] % image.width, rand_nums[index] // image.width))
  60.             dest_image.putpixel((i, j), pixel)
  61.             index += 1
  62.     return dest_image
  63.  
  64.  
  65. if __name__ == "__main__":
  66.     image_names = os.listdir(path='./source/')
  67.     seed=123
  68.     for image_name in image_names:
  69.         origin_image = Image.open('./source/'+image_name)
  70.         dest_image = scramble(origin_image, seed)
  71.         dest_image.save('./dist/v1/'+image_name)
  72.         dest_image2 = scramble2(origin_image,seed)
  73.         dest_image2.save('./dist/v2/'+image_name)
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement