Python发送邮件
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.utils import formataddr from email.header import Header from configparser import ConfigParser def send_message(receiver_email, subject, content, pic_path=[]): global server config = ConfigParser() config.read('config.ini',encoding='utf-8') sender_email = config.get('Settings', 'send_email') auth_code = config.get('Settings', 'code') smtp_server = config.get('Settings', 'smtp_server') smtp_port = config.get('Settings', 'smtp_port') sender_name = config.get('Settings', 'sender_name') receiver_name = config.get('Settings', 'receiver_name') try: if len(pic_path) == 0: # 创建邮件对象 msg = MIMEText(content, 'plain', 'utf-8') # 邮件内容,plain表示纯文本 msg['From'] = formataddr([sender_name, sender_email]) # 发件人信息 msg['To'] = formataddr([receiver_name, receiver_email]) # 收件人信息 msg['Subject'] = Header(subject, 'utf-8') # 邮件标题 # 使用SSL连接到SMTP服务器 server = smtplib.SMTP_SSL(smtp_server, smtp_port) # 登录QQ邮箱 server.login(sender_email, auth_code) # 发送邮件 server.sendmail(sender_email, [receiver_email], msg.as_string()) print("邮件发送成功") else: # 创建邮件对象 msg = MIMEMultipart() msg['From'] = formataddr([sender_name, sender_email]) # 发件人信息 msg['To'] = formataddr([receiver_name, receiver_email]) # 收件人信息 msg['Subject'] = Header(subject, 'utf-8') # 邮件标题 # 添加邮件正文 msg.attach(MIMEText(content, 'plain', 'utf-8')) # # 打开图片文件并将其附加为图片附件 # with open(pic_path, 'rb') as img_file: # img = MIMEImage(img_file.read()) # img.add_header('Content-Disposition', 'attachment', filename=pic_path.split('/')[-1]) # msg.attach(img) for image_path in pic_path: with open(image_path, 'rb') as img_file: img = MIMEImage(img_file.read()) img.add_header('Content-Disposition', 'attachment', filename=image_path.split('/')[-1]) msg.attach(img) # 使用SSL连接到SMTP服务器 server = smtplib.SMTP_SSL(smtp_server, smtp_port) # 登录QQ邮箱 server.login(sender_email, auth_code) # 发送邮件 server.sendmail(sender_email, [receiver_email], msg.as_string()) print("图片邮件发送成功") except Exception as e: print(f"Error occurred: {e}") finally: # 关闭服务器连接 server.quit() file_path = ["./pic/吗喽.jpg", "./pic/tree.png"] send_message(receiver_email='[email protected]', subject="1", content="222", pic_path=file_path)
识别图片文字并计算坐标
from PIL import Image from cnocr import CnOcr # 打开原图并裁剪 image = Image.open('img/test001.png') select_name = "Nic" # 裁剪区域 crop_area = (0, 603, 1211, 2526) cropped_image = image.crop(crop_area) # OCR识别获取数据 ocr = CnOcr() # 使用默认OCR设置 out = ocr.ocr(cropped_image) for text_cl in out: if select_name in text_cl["text"]: point_data = text_cl["position"] x_point = (point_data[0][0] + point_data[2][0]) // 2 y_point = (point_data[0][1] + point_data[2][1]) // 2 # 缩放回原图中的坐标 x_abs = int(x_point + crop_area[0]) y_abs = int(y_point + crop_area[1]) print("原图中的绝对坐标:", x_abs, y_abs)
水印
(1)图片水印
import cv2 import numpy as np img = cv2.imread("img/img.jpg") cv2.namedWindow("img", 0) cv2.resizeWindow("img", 720, 960) cv2.createTrackbar("x", "img", 0, img.shape[0], lambda x: x) cv2.createTrackbar("y", "img", 0, img.shape[1], lambda x: x) while True: x = cv2.getTrackbarPos("x", "img") y = cv2.getTrackbarPos("y", "img") # 在画布上绘制文本 img_copy = img.copy() text = 'OpenCV' # 文本内容 position = (y, x) # 文本位置 font = cv2.FONT_HERSHEY_TRIPLEX font_scale = 1.5 # 文本大小 color = (255, 255, 255) # 白色文本 thickness = 2 cv2.putText(img_copy, text, position, font, font_scale, color, thickness) cv2.imshow('img', img_copy) k = cv2.waitKey(1) & 0xFF if k == 27: cv2.imwrite("img/watermark.jpg", img_copy) break cv2.destroyAllWindows()
(2)视频水印
import cv2 cap = cv2.VideoCapture("video/test.mp4") FPS_show = True # 当前帧是否展示 cv2.namedWindow("video", 0) # cv2.resizeWindow("video", int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = int(cap.get(cv2.CAP_PROP_FPS)) fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('video/watermark_output.avi', fourcc, fps, (frame_width, frame_height)) class WaterText: text = None # 文字 position = None # 位置 font = None # 字体 font_scale = None # 文本大小 font_color = None # 颜色 font_thickness = None def __init__(self, img): self.img = img def draw(self): cv2.putText(self.img, WaterText.text, WaterText.position, WaterText.font, WaterText.font_scale, WaterText.font_color, WaterText.font_thickness) # 循环读取视频帧 while True: ret, frame = cap.read() # ret 表示是否成功读取,frame 是帧数据 if not ret: # 读取失败或视频结束 print("Video has ended or cannot be read.") break if FPS_show: cv2.createTrackbar("x", "video", 0, frame.shape[0], lambda x: x) cv2.createTrackbar("y", "video", 0, frame.shape[1], lambda x: x) while True: x = cv2.getTrackbarPos("x", "video") y = cv2.getTrackbarPos("y", "video") video_copy = frame.copy() WaterText.text = "Watermark" WaterText.position = (y, x) WaterText.font = cv2.FONT_HERSHEY_TRIPLEX WaterText.font_scale = 1.5 WaterText.font_color = (255, 255, 255) WaterText.font_thickness = 2 WaterText(video_copy).draw() cv2.imshow('video', video_copy) if cv2.waitKey(1) & 0xFF == 27: # esc break FPS_show = False cv2.destroyWindow("video") # continue # 打开则跳过首帧显示 WaterText(frame).draw() cv2.imshow("video", frame) out.write(frame) if cv2.waitKey(20) & 0xFF == ord('q'): break # 释放资源 cap.release() cv2.destroyAllWindows()