create_rgb.py 1.75 KB
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Titouan Parcollet

from PIL import Image
import numpy as np
import sys
import os

def reconstruct(size):
    path = "Results/"+str(size)
    if not os.path.exists(str(path)):
        os.makedirs(str(path))

    if not os.path.exists(str(path)+"/TRAIN"):
        os.makedirs(str(path)+"/TRAIN")

    if not os.path.exists(str(path)+"/TEST"):
        os.makedirs(str(path)+"/TEST")

    ######## Process Train pictures
    data = open(path+'_train.dat')
    a = data.readlines()
    z=0
    for line in a:
        line = line.split(" ")
        img = Image.new( 'RGB', (32,32), "black" ) # create a new black image
        pixels = img.load()                        # create the pixel map
        for i in range(0,img.size[0]):             # for every pixel:
            for j in range(0,img.size[1]):
                
                pixels[j,i] = (int(float(line[(i*(img.size[0])*3)+(j*3)])), int(float(line[(i*(img.size[0])*3)+(j*3)+1])), int(float(line[(i*(img.size[0])*3)+(j*3)+2]))) 
        z+=1
        img.save(path+"/TRAIN/train"+str(z)+".jpg", "JPEG")

    ######## Process Test pictures
    data = open(path+'_test.dat')
    a = data.readlines()
    z=0
    for line in a:
        line = line.split(" ")
        img = Image.new( 'RGB', (32,32), "black" ) # create a new black image
        pixels = img.load()                        # create the pixel map
        for i in range(0,img.size[0]):             # for every pixel:
            for j in range(0,img.size[1]):
                pixels[j,i] = (int(float(line[(i*(img.size[0])*3)+(j*3)])), int(float(line[(i*(img.size[0])*3)+(j*3)+1])), int(float(line[(i*(img.size[0])*3)+(j*3)+2]))) 
        z+=1
        img.save(path+"/TEST/test"+str(z)+".jpg", "JPEG")
    return