How to add timestamp as wateramark in image or output name as current time

Viewed 648

This usually saves full screenshot of the webpage, how to add current time as watermark in image or if its too dificult, atleast change output stackoverflow.png as current time "08/10/2021/01:07:02" like this

'''

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import time

options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(ChromeDriverManager().install(), options = options)
driver.implicitly_wait(10)
path = 'C:/Users/Romeo/Downloads'
driver.get('https://stackoverflow.com/')
S= lambda X: driver.execute_script('return document.body.parentNode.scroll'+X)
driver.set_window_size(S('Width'), S('Height'))
driver.find_element_by_tag_name('body').screenshot('stackoverflow.png')
                                 

'''

1 Answers

To save the file name as the current date time, you can do this. Add the datetime module to your code, then change the last line.


from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import time
import datetime

options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(ChromeDriverManager().install(), options = options)
driver.implicitly_wait(10)
path = 'C:/Users/Romeo/Downloads'
driver.get('https://stackoverflow.com/')
S= lambda X: driver.execute_script('return document.body.parentNode.scroll'+X)
driver.set_window_size(S('Width'), S('Height'))
driver.find_element_by_tag_name('body').screenshot(f'{datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")}.png')

If you want to add a watermark, take a look at this library, the post explains quite well on how to do it. https://www.geeksforgeeks.org/python-pillow-creating-a-watermark/

All you would have to do then is, add the following code to your code,

from PIL import ImageFont
from PIL import ImageDraw
watermark_image = image.copy()
  
draw = ImageDraw.Draw(watermark_image)
font = ImageFont.truetype("arial.ttf", 50)
  
# add watermark
draw.text((0, 0), datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S"), 
          (0, 0, 0), font=font)
plt.subplot(1, 2, 1)
plt.title("black text")
plt.imshow(watermark_image)

Related