Advertisement
Guest User

Untitled

a guest
May 27th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. import numpy
  2. from PIL import Image
  3.  
  4.  
  5. def unpickle(file):
  6.     import pickle
  7.     with open(file, 'rb') as fo:
  8.         dict = pickle.load(fo, encoding='bytes')
  9.     return dict
  10.  
  11. def picProcessor(pic):
  12.     processed_pic=numpy.zeros((32,32,3), dtype=numpy.uint8)
  13.  
  14.     #colors are stored continously
  15.     step=0
  16.     for i in range(32):
  17.         for j in range(32):
  18.             processed_pic[i][j][0]=pic[step*32+j]
  19.             processed_pic[i][j][1]=pic[step*32+j+1024]
  20.             processed_pic[i][j][2]=pic[step*32+j+2048]
  21.         step=step+1
  22.  
  23.     return processed_pic
  24.  
  25. def generatePicture(file,pic):
  26.     img=Image.fromarray(pic,'RGB')
  27.     img.save(file)
  28.  
  29.  
  30.  
  31. def main():
  32.     #randomly opening a batch
  33.     batch_no=numpy.random.randint(5)+1
  34.     batch_location="D:\Programok\CIFAR\cifar-10-python\cifar-10-batches-py\data_batch_"+str(batch_no)
  35.     batch=unpickle(batch_location)
  36.  
  37.     #randomly getting a picture from the randomly chosen batch
  38.     picnum=numpy.random.randint(0,10000)
  39.  
  40.  
  41.     test_pic=picProcessor(batch[b'data'][picnum])
  42.     generatePicture('teszt.png',test_pic)
  43.     return 0
  44.  
  45. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement