Shorzinator
Shorzinator

Reputation: 26

NameError in the snippet

#prepare degraded images by introducing quality distortion by resizing images
def prepare_images(factor):
    path = './SRCNN/source'
    #loop through the file in the directory
    for files in os.listdir(path):
        #open the file
        img = cv2.imread(path + '/' + files)

        #find old and new image dimaensions
        h, w, c = img.shape
        new_height = h /factor
        new_width = w / factor

        #resize the image - down 
        img = (cv2.resize(img, (int(new_width), int(new_height)), interpolation=cv2.INTER_LINEAR))

        #resize the image - up
        img = (cv2.resize(img, (w, h), interpolation = INTER_LINEAR))

        # save the image

        print('Saving {}'.format(files))

        cv2.imwrite('images/{}'.format(files), img)




---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-77-6ba532c6503e> in <module>()
----> 1 prepare_images(2)
      2 #os.listdir('./SRCNN/source')

<ipython-input-76-4a7882c5ab03> in prepare_images(factor)
     16 
     17         #resize the image - up
---> 18         img = (cv2.resize(img, (w, h), interpolation = INTER_LINEAR))
     19 
     20         # save the image

NameError: name 'INTER_LINEAR' is not defined

The first part is the function. The second part is the error in that function. How can I resolve this? I am writing this code on Google Colab and have tried on Jupyter as well...

Upvotes: 1

Views: 41

Answers (1)

Gabio
Gabio

Reputation: 9484

In order to use INTER_LINEAR, you should import it specifically. So you can add an import:

from cv2 import INTER_LINEAR

or

change interpolation = INTER_LINEAR to be interpolation = cv2.INTER_LINEAR

Upvotes: 1

Related Questions