访问网站,并给网站截图,然后再给图片添加水印的python代码
import os import requests import time from PIL import Image, ImageDraw, ImageFont from selenium import webdriver from selenium.webdriver.chrome.options import Options def add_text_to_image(image, text, font_path): draw = ImageDraw.Draw(image) font = ImageFont.truetype(font_path, 32) text_bbox = draw.textbbox((0, 0), text, font=font) text_width = text_bbox[2] - text_bbox[0] text_height = text_bbox[3] - text_bbox[1] x = (image.width - text_width) // 2 y = (image.height - text_height) * 2 // 3 draw.text((x, y), text, fill=(255, 0, 0), font=font) return image def capture_screenshot(url, width, height, save_path): chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') chrome_options.add_argument('--user-agent=Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)') driver = webdriver.Chrome(options=chrome_options) driver.set_window_size(width, height) driver.get(url) time.sleep(5) # 等待页面加载完成 screenshot_path = os.path.join(save_path, 'screenshot.png') driver.save_screenshot(screenshot_path) driver.quit() return screenshot_path def main(): url = "https://www.baidu.com" save_path = "C:/Users/XuanJian/Desktop" font_path = "C:/Windows/Fonts/simsun.ttc" width = 1280 height = 1024 screenshot_path = capture_screenshot(url, width, height, save_path) image = Image.open(screenshot_path) image_with_text = add_text_to_image(image, "百度百度百度", font_path) image_with_text.save(os.path.join(save_path, 'screenshot_with_text.png')) # 删除原始截图文件 os.remove(screenshot_path) if __name__ == "__main__": main()
版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、文章来源:来自于网络收集。