-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_npy_files.py
33 lines (30 loc) · 1.25 KB
/
create_npy_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import numpy as np
import os
from PIL import Image
# Generating numpy array format from folder with .jpgs
# https://stackoverflow.com/questions/50772128/pytorch-handling-picutres-and-jpeg-files-beginners-questions
def load_image(infilename):
"""This function loads an image into memory when you give it
the path of the image
"""
img = Image.open(infilename)
img.load()
data = np.asarray(img, dtype="float64")
return data
def create_npy_from_image(images_folder, output_name, num_images, image_dim):
"""Loops through the images in a folder and saves all of them
as a numpy array in output_name
"""
image_matrix = np.empty((num_images, image_dim, image_dim, 3), dtype=np.float64)
for i, filename in enumerate(os.listdir(images_folder)):
if filename.endswith(".jpg"):
data = load_image(images_folder + filename)
image_matrix[i] = data
else:
continue
np.save(output_name, image_matrix)
DIM = 64 # The images have the size 256x256
CAGE_SIZE = 4178 # Number of Cage images
TRUMP_SIZE = 9864 # Number of Trump images
create_npy_from_image("./resized_data_new/cage/", "cage.npy", CAGE_SIZE, DIM)
create_npy_from_image("./resized_data_new/trump/", "trump.npy", TRUMP_SIZE, DIM)