How to use MNIST model

After writing the code for MNIST handwritten digits, how can I use it to predict new images?
I read the image using PIL and now I’m stuck.
I don’t know how to convert it into 28*28 and also to gray scale in the script.
Is the any way to do this conversion it in the script or do I have to do it manually for every image?

Are you using the torchvision.datasets.MNIST dataset or your “own” handwritten images?
In the former case you could stick to the MNIST example. In the latter case, you could load your images as grayscale images using Image.open(PATH).convert('L') and apply your transformations on this image directly.

Oh okay… But what about converting it into 28*28

If you’re loading the images using PIL, you can use torchvision.transforms.Resize.
As you can see in the example, you can compose several transformations using torchvision.transforms.Compose.

Is it advisable to just resize or to crop and then resize? Because on just resizing the digit becomes too small, and if I crop and then resize,the model is not able to correctly predict the outcome.
Any comments?

This is my code to transform my handwriting picutures:

def changeData(path = "./mynum/num/"):
    # path = "./mynum/num/"
    dir_or_files = os.listdir(path)
    files = []
    for dir_file in os.listdir(path):
        files.append(dir_file)
        
    MNIST_SIZE = 28
    datas = []
    labels = []
    for file in files:
        # transform pictures
        # print(path+files[0])
        # read the image and trun to gray
        img = io.imread(path+file, as_gray=True)
        # resize to 28*28
        translated_img = transform.resize(img, (MNIST_SIZE, MNIST_SIZE))
        # change to 1*784 array
        flatten_img = np.reshape(translated_img, 784)
        # 1 is black,0 is whtie
        result = np.array([1 - flatten_img])
        # get the label
        labels.append([int(file[-5])])
        datas.append(result)
        print(file)
    datas = np.array(datas)
    labels = np.array(labels)
    return datas, labels

The picture’s name is set like “03_3.png”.The 03 before _ is ID, the 3 after _ 3 is the real number of the picture.
The full code link.