Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import hashlib
  4. import numpy as np
  5. from PIL import Image
  6. from random import randint
  7.  
  8.  
  9. def generate(string):
  10. # control variables
  11. rows = 8
  12. cols = 8
  13. width = 256
  14. height = 256
  15.  
  16. # generate hash
  17. m = hashlib.shake_256()
  18. m.update(string.encode())
  19. data = m.digest(rows * cols)
  20.  
  21. # create a bit-field
  22. bits = []
  23. for n in data:
  24. for i in range(8):
  25. bits.append((n >> i) & 1)
  26. # end for
  27. # end for
  28.  
  29. # get a color for the image
  30. colors = [
  31. [0, 0, 1],
  32. [0, 1, 0],
  33. [1, 0, 0],
  34. ]
  35. cc = randint(0, len(colors) - 1)
  36.  
  37. # create an array
  38. arr = np.ones((rows + 2, cols + 2, 3))
  39. for i in range(rows):
  40. for j in range(cols):
  41. n = bits[i * cols + j]
  42. arr[i + 1, j + 1] = colors[cc] if n else [1, 1, 1]
  43. # end for
  44. # end for
  45.  
  46. # save image
  47. arr = (arr * 255).astype(np.uint8)
  48. image = Image.fromarray(arr)
  49. image = image.resize((width, height), resample=Image.BOX)
  50. image.save('image.bmp')
  51. # end def
  52.  
  53.  
  54. def main():
  55. if len(sys.argv) <= 1:
  56. raise Exception('You should provide some strings to genrate images')
  57. # end if
  58.  
  59. names = sys.argv[1:]
  60. for name in names:
  61. generate(name)
  62. # end for
  63. # end def
  64.  
  65.  
  66. if __name__ == '__main__':
  67. main()
  68. # end if
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement