Python Forum
Run a timer when a functions starts to see how long the function takes to complete
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Run a timer when a functions starts to see how long the function takes to complete
#1
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')
Reply
#2
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.
Reply
#3
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 ...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code starts slowing down? MemoryError AshkanDev 1 371 May-19-2020, 11:38 PM
Last Post: AshkanDev
  Upload takes too long. Makada 29 1,500 Apr-06-2020, 08:06 PM
Last Post: Makada
  Need help with a function that calls other functions. skurrtboi 4 459 Sep-30-2019, 09:28 PM
Last Post: stullis
  sensor code that starts heater chano 3 848 Jun-05-2019, 10:54 AM
Last Post: michalmonday
  Help with Stopping a function after a timer SheeppOSU 0 532 Jan-28-2019, 10:13 PM
Last Post: SheeppOSU
  Cannot use function with timer/clock theangryprogrammer 1 1,293 Jan-22-2019, 04:22 PM
Last Post: Larz60+
  call a function from other functions ... evilcode1 2 962 Sep-05-2018, 09:07 AM
Last Post: evilcode1
  argument parser: to execute single function, and subsequent functions also raghu 10 2,240 Mar-12-2018, 06:57 AM
Last Post: wavic
  Function to Continuasly monitor serial port and control other functions ricardons 10 3,414 Mar-04-2018, 10:34 PM
Last Post: Larz60+
  How to define two functions run simultaneously within a function? Alberto 4 1,606 Feb-06-2018, 10:08 PM
Last Post: Alberto

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020