1

I have a function that I want to count how many times it is called (and save a image with that counter value as its name). I made a global function called counter . But if I do counter = counter+1. It says Unresolved reference. Am I missing something ?

Here is the code :

import numpy as np
import cv2
counter = 0
def saveImage(img):
    counter = counter+1

    imgs = str(counter) + '.jpg'
    cv2.imwrite('images/'+imgs, img)
2
  • 1
    add global counter inside your function before assigning value to it . Commented Aug 6, 2018 at 5:36
  • use of global keyword Commented Aug 6, 2018 at 5:37

1 Answer 1

1
import numpy as np
import cv2
counter = 0
def saveImage(img):
    global counter  # to modify global variable, you need to explicitly declare so... 
    counter = counter+1

    imgs = str(counter) + '.jpg'
    cv2.imwrite('images/'+imgs, img)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.