Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import sys
- import hashlib
- import numpy as np
- from PIL import Image
- from random import randint
- def generate(string):
- # control variables
- rows = 8
- cols = 8
- width = 256
- height = 256
- # generate hash
- m = hashlib.shake_256()
- m.update(string.encode())
- data = m.digest(rows * cols)
- # create a bit-field
- bits = []
- for n in data:
- for i in range(8):
- bits.append((n >> i) & 1)
- # end for
- # end for
- # get a color for the image
- colors = [
- [0, 0, 1],
- [0, 1, 0],
- [1, 0, 0],
- ]
- cc = randint(0, len(colors) - 1)
- # create an array
- arr = np.ones((rows + 2, cols + 2, 3))
- for i in range(rows):
- for j in range(cols):
- n = bits[i * cols + j]
- arr[i + 1, j + 1] = colors[cc] if n else [1, 1, 1]
- # end for
- # end for
- # save image
- arr = (arr * 255).astype(np.uint8)
- image = Image.fromarray(arr)
- image = image.resize((width, height), resample=Image.BOX)
- image.save('image.bmp')
- # end def
- def main():
- if len(sys.argv) <= 1:
- raise Exception('You should provide some strings to genrate images')
- # end if
- names = sys.argv[1:]
- for name in names:
- generate(name)
- # end for
- # end def
- if __name__ == '__main__':
- main()
- # end if
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement