0

I am doing a question of enhancing contrast of an image. Please have a look on the following piece of code

# this is a test cell
@exit_after(4)
def testContrastSharpening():
    # load an image
    i0 = loadImage('Data/bokehCircular.jpg', 100)

    # run student code
    s0 = contrastSharpen(i0, 3, 0.5)

    # collect test data
    rTest = s0[:, int(i0.shape[1]/2), 0]
    gTest = s0[:, int(i0.shape[1]/4), 1]
    bTest = s0[int(i0.shape[0]/5), :, 2]

    # load and compare to reference result
    l0 = load('Data/t0.npz')

    return allclose(rTest, l0['r']) and allclose(gTest, l0['g']) and allclose(bTest, l0['b'])

try:
    assert(testContrastSharpening())
    print('Contrast sharpening seems to work!')
except:
    raise Exception("Contrast sharpening is not working... please try again...")

i0 = loadImage('Data/bokehCircular.jpg', 100) This line of code is calling pre written ffunction to load an image, that function is given below

def loadImage(path, scale):
    image = Image.open(path, mode = 'r')
    image = image.resize((int(image.width * scale / 100), int(image.height * scale / 100)), resample=Image.LANCZOS)
    image = array(image.getdata()).reshape(image.size[1], image.size[0], 3)
    image = image.astype(float)
    image = image / max(image)

    return image

when I run the code, i get error on the following line image = image.astype(float) The error is given below

KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-8-bf381c328e33> in <module>
     20 try:
---> 21     assert(testContrastSharpening())
     22     print('Contrast sharpening seems to work!')

<ipython-input-1-4c3172e491d3> in inner(*args, **kwargs)
     44             try:
---> 45                 result = fn(*args, **kwargs)
     46             finally:

<ipython-input-8-bf381c328e33> in testContrastSharpening()
      4     # load an image
----> 5     i0 = loadImage('Data/bokehCircular.jpg', 100)
      6 

<ipython-input-3-545d6e0c0f38> in loadImage(path, scale)
      5     image = array(image.getdata()).reshape(image.size[1], image.size[0], 3)
----> 6     image = image.astype(float)
      7     image = image / max(image)

KeyboardInterrupt:

I have written code for enhancing the contrast of an image and I have tested it separately by loading ana image through same LoadImage function, but In the above case, LoadImage function isn't working properly Please help, if someone can , thanks

arnaud
  • 3,293
  • 1
  • 10
  • 27
Ahsan Niaz
  • 19
  • 1
  • 6

1 Answers1

1

I think this is because of your decorator @exit_after(4). Indeed, using @exit_after(4) after 4 seconds, your test cell will be stopped by a KeyboardInterrupt error.

From the source code of @exit_after()

def exit_after(s):
   '''
   use as decorator to exit process if 
   function takes longer than s seconds
   '''

There are two solutions:

  • Modify the test: Make your test more tolerant and raise that number? e.g. @exit_after(10) to see if this works.
  • Modify your script: Make your functions faster so that they work given the 4 seconds constraint. You could start by making the fonctions almost empty, and check that it works.
arnaud
  • 3,293
  • 1
  • 10
  • 27