Python Forum
Run a timer when a functions starts to see how long the function takes to complete - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Run a timer when a functions starts to see how long the function takes to complete (/thread-26027.html)



Run a timer when a functions starts to see how long the function takes to complete - Pedroski55 - Apr-19-2020

I made a little Python to remove watermarks from pdfs. It works OK.

After splitting the pdf into jpgs, I look for grey pixels and paint them white.

I just changed it to take the lowest grey range as a variable.

The smaller the variable lowestGrey = int(Grey), and of course, the greater the number of pixels, the longer the function takes to complete.

Also, it's a trade-off: the Chinese characters in the pdf contain grey pixels too. If lowestGrey is too small, the characters get very scruffy.

Just out of interest, I'd like to time the function.

How can I set a timer? The timer should start when the function is called and print its result when the function is done.

def removeWM(path, aJPG, grey):    
    picture = Image.open(path + aJPG)
    saveas = aJPG.split('.')
    savename = saveas[0] + 'WMgone.jpg'    
    width, height =  picture.size    
    for x in range(0, width):
        for y in range(0, height):
           r,g,b = picture.getpixel( (x,y) )       
           if r and g and b in range(grey, 253):           
               picture.putpixel( (x,y), (255, 255, 255)) # white is 3 x 255
    picture.save(pathToWMgone + savename)
    print(aJPG + '  watermark gone and saved')



RE: Run a timer when a functions starts to see how long the function takes to complete - michael1789 - Apr-19-2020

You could use the built-in time() module.

import time

start = time.time() #put this as the first line of the function 

end = time.time()  #put this at the end of the function
print(end - start) 
That said, if your intention is to steal artwork, I encourage you to try making some or use the literally billions of free images that exist.


RE: Run a timer when a functions starts to see how long the function takes to complete - Pedroski55 - Apr-19-2020

Thanks a lot!

No, no stealing here!

These are old exams, watermarked by various education agencies, to whom they do not belong. They are rightly the property of the university that created them.

The gf asked me to get rid of the watermark and then watermark them for the agency she works for.

Definitely not art.

Worked great! Result:
Quote:Now getting the jpgs to remove the wm ...
Got 00319-1-11.jpg
jpg is 2245 wide 3417 high
Start time was 1587277575.5664425 end time was 1587277595.3091455
elapsed time is: 19.742702960968018
00319-1-11.jpg watermark gone and saved as /home/pedro/babystuff/wmGone/00319-1-11WMgone.jpg
Got 00319-1-12.jpg
jpg is 2287 wide 3475 high
Start time was 1587277595.3092086 end time was 1587277615.3963969
elapsed time is: 20.087188243865967
00319-1-12.jpg watermark gone and saved as /home/pedro/babystuff/wmGone/00319-1-12WMgone.jpg
now joining up the jpgs without wm ...