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
  using threading.Timer for function korenron 1 1,155 Dec-20-2022, 01:09 PM
Last Post: ndc85430
  Move column to the right if it starts with a letter mfernandes 0 648 Oct-25-2022, 11:22 AM
Last Post: mfernandes
  Setup Portable Python on Windows for script starts with double clicks? pstein 0 1,778 Feb-18-2022, 01:29 PM
Last Post: pstein
  Checking the number of arguments a function takes Chirumer 3 2,111 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Regex: a string does not starts and ends with the same character Melcu54 5 2,367 Jul-04-2021, 07:51 PM
Last Post: Melcu54
  email timer/rss feed timer ndiniz 1 2,041 Feb-02-2021, 07:18 PM
Last Post: nilamo
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,195 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  Code starts slowing down? MemoryError AshkanDev 1 2,118 May-19-2020, 11:38 PM
Last Post: AshkanDev
  Upload takes too long. Makada 29 7,350 Apr-06-2020, 08:06 PM
Last Post: Makada
  Need help with a function that calls other functions. skurrtboi 4 2,459 Sep-30-2019, 09:28 PM
Last Post: stullis

Forum Jump:

User Panel Messages

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