generators.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import io
  2. import random
  3. from hashlib import sha1
  4. import requests
  5. import numpy
  6. from PIL import Image
  7. # @TODO Update this to include code point ranges to be sampled
  8. include_ranges = [
  9. ( 0x0021, 0x0021 ),
  10. ( 0x0023, 0x0026 ),
  11. ( 0x0028, 0x007E ),
  12. ( 0x00A1, 0x00AC ),
  13. ( 0x00AE, 0x00FF ),
  14. ( 0x0100, 0x017F ),
  15. ( 0x0180, 0x024F ),
  16. ( 0x2C60, 0x2C7F ),
  17. ( 0x16A0, 0x16F0 ),
  18. ( 0x0370, 0x0377 ),
  19. ( 0x037A, 0x037E ),
  20. ( 0x0384, 0x038A ),
  21. ( 0x038C, 0x038C ),
  22. ]
  23. def random_utf8_string(length):
  24. alphabet = [
  25. chr(code_point) for current_range in include_ranges
  26. for code_point in range(current_range[0], current_range[1] + 1)
  27. ]
  28. return ''.join(random.choice(alphabet) for i in range(length))
  29. def random_image(name, ts=8, ims=256):
  30. imarray = numpy.random.rand(ts, ts, 3) * 255
  31. im = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
  32. im = im.resize((ims, ims), Image.NEAREST)
  33. imf = io.BytesIO()
  34. im.save(imf, format='png')
  35. imf.seek(0)
  36. hash = sha1(imf.read()).hexdigest()
  37. return {
  38. 'content' : imf,
  39. 'hash' : hash,
  40. 'filename' : random_utf8_string(32) + '.png'
  41. }