In [42]:
!pip install six pypng


Requirement already satisfied: six in /opt/conda/lib/python3.6/site-packages (1.11.0)
Collecting pypng
  Downloading https://files.pythonhosted.org/packages/2f/b1/c8dfcf50feb12a30be7d95c5f45d638704682487d8e50419ef41463febcd/pypng-0.0.18.tar.gz (377kB)
    100% |████████████████████████████████| 378kB 5.9MB/s ta 0:00:01
Building wheels for collected packages: pypng
  Running setup.py bdist_wheel for pypng ... done
  Stored in directory: /home/jovyan/.cache/pip/wheels/ed/30/6c/21dd4d267f47ea09fb57881b30fe16f7231b71bd26dea38afc
Successfully built pypng
Installing collected packages: pypng
Successfully installed pypng-0.0.18

In [62]:
import PIL
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import six
from six.moves import cPickle as pickle
import png
import os

In [69]:
def extractImagesAndLabels(path, file):
    f = open(path+file, 'rb')
    dict = cPickle.load(f, encoding='latin1')
    images = dict['data']
    print (images.shape)
    #images = np.reshape(images, (10000, 3, 32, 32))
    #return as (10000, 3072)
    labels = dict['labels']
    return images, labels

images, labels = extractImagesAndLabels("data/CIFAR-10/cifar-10-batches-py/", "data_batch_1")

def extractCategories(path, file):
    f = open(path+file, 'rb')
    dict = cPickle.load(f, encoding='latin1')
    return dict['label_names']

categories = extractCategories("data/CIFAR-10/cifar-10-batches-py/", "batches.meta")

def getImage(images, id):
    image = images[id]
    image = image.transpose([1, 2, 0])
    image = image.astype('float32')
    image /= 255
    return image

def showImage(id):
    image = getImage(images,id)
    %matplotlib inline
    imgplot = plt.imshow(image)
    labelid = labels[id]
    category = categories[labelid]
    print("category : "+category)


(10000, 3072)

In [93]:
#Get data from cifar dataset
imagedata = images[4];

#create png from data
png.from_array(images[4].reshape((32, 32, 3), order='F').swapaxes(0,1), mode='RGB').save('out4.png')
img = Image.open( 'out4.png' )
img.load()
#resize image
img = img.resize((126, 126), Image.ANTIALIAS)

#get data back from image
data = np.asarray( img, dtype="int32" )
#flatten data for storage
data = data.flatten()

#reshape the data as 126x126 RGB
data = np.reshape(data, (126, 126, 3))
#data = data.transpose([1, 2, 0])



%matplotlib inline
imgplot = plt.imshow(data)


(126, 126, 3)
[[[171 181 199]
  [170 179 197]
  [170 179 197]
  ..., 
  [158 178 213]
  [158 178 213]
  [158 178 213]]

 [[171 181 199]
  [170 180 198]
  [170 180 198]
  ..., 
  [157 177 212]
  [157 177 212]
  [157 177 212]]

 [[171 181 199]
  [170 180 198]
  [170 180 198]
  ..., 
  [157 177 212]
  [157 177 212]
  [157 177 212]]

 ..., 
 [[ 67  75  78]
  [ 67  75  78]
  [ 67  75  78]
  ..., 
  [ 73  77  80]
  [ 73  77  80]
  [ 74  78  81]]

 [[ 67  75  78]
  [ 67  75  78]
  [ 67  75  78]
  ..., 
  [ 73  77  80]
  [ 73  77  80]
  [ 74  78  81]]

 [[ 67  75  78]
  [ 67  75  78]
  [ 67  75  78]
  ..., 
  [ 73  77  80]
  [ 73  77  80]
  [ 74  78  81]]]

In [ ]: