2

I have an image I want to make it the correct shape to fit into my keras model.

I wanted to use imread, imresize,imshow from scipy, but it seems that they are now deprecated.

The old code with the functionality I am trying to emulate is:

x = imread('output.png',mode='L')
x = np.invert(x)
x = imresize(x,(28,28))
imshow(x)
x = x.reshape(1,28,28,1)

My new code is as follows:

import numpy as np
from PIL import Image
import cv2

def load_image( infilename ) :
    img = Image.open( infilename )
    img.load()
    data = np.asarray( img, dtype="int32" )
    return data

x = load_image("output.png")

x = cv2.resize(x, dsize=(28, 28))
x = x.reshape(1,28,28,1)

I am currently attempting to use cv2 and numpy to handle the image when I get the following error:

x = cv2.resize(x, dsize=(28, 28))
cv2.error: OpenCV(3.4.2) C:\Miniconda3\conda-bld\opencv-suite_1534379934306\work\modules\imgproc\src\resize.cpp:3922: error: (-215:Assertion failed) func != 0 in function 'cv::hal::resize'

Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

0

here is a similiar question. Look like opencv resize method doesn't work with type int32. You can try to call it after resize.

Finally cv2.imread(infilename,-1) returns a numpy array:

import numpy as np
import cv2

def load_image( infilename ) :
    data = cv2.imread(infilename,-1)
    return data

x = load_image("output.png")

x = cv2.resize(x, dsize=(28, 28))
x = x.astype('int32')
x = x.reshape(1,28,28,1)
Andrea
  • 122
  • 4