简体   繁体   中英

Resizing images of different sizes into 28x28 images and convert those into one csv-file

I have two folders full of images (around 2000 files each) of different sizes. I need all of them in 28x28 format. After that I need to convert all of those images of each folder into one csv-file. Any ideas how I could do that? I'm an absolute beginner in python so please be a little bit patient, if i need more time to understand the basics.

I tried a solution I found here : Converting images to csv file in python

Specifically :

import numpy as np
import cv2
import os

IMG_DIR = 'C:/Users/Anwender/Documents/Uni/KI/Trainingsdaten/Train'

for img in os.listdir(IMG_DIR):
        img_array = cv2.imread(os.path.join(IMG_DIR,img), cv2.IMREAD_GRAYSCALE)

        img_array = (img_array.flatten())

        img_array  = img_array.reshape(-1,1).T

        print(img_array)

        with open('train.csv', 'ab') as f:

            np.savetxt(f, img_array, delimiter=",")`

I hoped that changing img_array = img_array.reshape(-1,1).T into img_array = img_array.reshape(-1,28*28).T would give me the described result but instead delivers : "ValueError: cannot reshape array of size 2500 into shape (784)". I understand that there is no common denominator of both numbers so the dividing process without a remainder is not possible.

Use PIL to resize the image before converting into CSV.

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

IMG_DIR = 'C:/Users/Anwender/Documents/Uni/KI/Trainingsdaten/Train'

for img in os.listdir(IMG_DIR):
    img_array = cv2.imread(os.path.join(IMG_DIR,img), cv2.IMREAD_GRAYSCALE)

    img_pil = Image.fromarray(img_array)
    img_28x28 = np.array(img_pil.resize((28, 28), Image.ANTIALIAS))

    img_array = (img_28x28.flatten())

    img_array  = img_array.reshape(-1,1).T

    print(img_array)

    with open('train.csv', 'ab') as f:

        np.savetxt(f, img_array, delimiter=",")

You can use cv2.resize() to resize each image to 28X28 .

Try below code:

for img in os.listdir(IMG_DIR):
        img_array = cv2.imread(os.path.join(IMG_DIR,img), cv2.IMREAD_GRAYSCALE)
        resized_image = cv2.resize(img_array, (28, 28))
        img_flatten = resized_image.reshape(-1)
        #print(img_flatten)
        with open('train1.csv', 'ab') as f:
            np.savetxt(f, img_flatten, delimiter=",")

In data.reshape function , -1 is used as a placeholder for figure out what the given dimension should be. You can also use resized_image.flatten() in place of img_flatten = resized_image.reshape(-1) to flatten the numpy array to one dimension.

In this case, each image will be saved into the csv file as a numpy array of shape (784,). On adding img_flatten.reshape(-1, 1).T to the above code will convert its shape to (1, 784) . It's solely up to you how you want to save.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM