考勤表掃描,裁切,旋轉,合併 python + opencv + numpy
# 考勤表掃描,裁切,旋轉,合併 python + opencv + numpy
# 彩色掃描,200dpi,圖片,對準左上角,直向放置
# 先掃描上半個月,再掃描下半個月。
# pip install opencv-python
from pathlib import Path
import math
import cv2
import numpy as np
folder = Path("in")
files = sorted([f.name for f in folder.iterdir() if f.is_file()])
for i in range(int(math.floor(len(files)/2))):
f1 = files[i*2+0]
f2 = files[i*2+1]
img1 = cv2.imread("in/"+f1)
img2 = cv2.imread("in/"+f2)
x1, y1 = 0, 0
x2, y2 = 1471, 652
crop1 = img1[y1:y2, x1:x2]
crop2 = img2[y1:y2, x1:x2]
rotated1=cv2.rotate(crop1, cv2.ROTATE_90_CLOCKWISE)
rotated2=cv2.rotate(crop2, cv2.ROTATE_90_CLOCKWISE)
merged=np.hstack((rotated1, rotated2))
cv2.imwrite("out/"+str(i)+".jpg", merged)
print(f1,f2)
語音轉文字
cmd
whisper test.mp3 --model medium --language Chinese --device cuda --fp16 False --task transcribe --initial_prompt "以下內容為佛教講經,包含佛經原文、法師開示、佛學專有名詞,如:末學、蓮友、大德、阿彌陀、述要、菩提、般若、涅槃、阿賴耶識、因果、空性、佛說無量壽經、解行並進、信願行、資糧。請依原文逐字轉寫,不翻譯、不簡化。"
medium 好像比 large 好
tiny/base/small/medium/large
ubuntu + python + google maps
# ubuntu 20.04.6 + gtk 為基底
# python 3.8.10
# cpu 是 intel 系列
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('WebKit2', '4.0')
from gi.repository import Gtk, WebKit2, Gdk, GLib
import random
# ----------------------------
# 截圖函式
# ----------------------------
def take_screenshot(webview):
window = webview.get_window()
if window:
width, height = webview.get_allocated_width(), webview.get_allocated_height()
pb = Gdk.pixbuf_get_from_window(window, 0, 0, width, height)
if pb:
pb.savev("screenshot.png", "png", [], [])
print("截圖完成,存檔為 screenshot.png")
Gtk.main_quit()
# ----------------------------
# 抓 DOM 函式
# ----------------------------
def grab_dom(webview):
js_code = "document.getElementsByClassName('tUEI8e')[0].textContent;"
def js_callback(webview, result, user_data):
value = webview.run_javascript_finish(result).get_js_value()
print("抓到網頁 title:", value.to_string())
# 等 DOM 取完再截圖
take_screenshot(webview)
webview.run_javascript(js_code, None, js_callback, None)
# ----------------------------
# 網頁載入完成 callback
# ----------------------------
def on_load_changed(webview, event, user_data=None):
if event == WebKit2.LoadEvent.FINISHED:
print("網頁載入完成")
# 延遲 20~25 秒再抓 DOM,不阻塞主迴圈
delay = random.randint(20, 25)*1000
GLib.timeout_add(delay, lambda: grab_dom(webview))
# ----------------------------
# 主程式
# ----------------------------
win = Gtk.Window()
win.set_default_size(1920, 1080)
win.set_title("GTK WebView 非阻塞延遲範例")
webview = WebKit2.WebView()
win.add(webview)
win.show_all()
webview.connect("load-changed", on_load_changed)
webview.load_uri("https://www.google.com/maps/dir/嘉里醫藥烏日倉/台中榮民總醫院")
Gtk.main()
import subprocess
# 呼叫另一個 Python 腳本
result = subprocess.run(["python3", "other.py"], capture_output=True, text=True)
# 等待 other.py 執行完畢後再繼續
print("other.py 執行完畢")
print("輸出結果:")
print(result.stdout)
sudo apt update
sudo apt install lxqt-core lxqt-session
sudo apt install openbox obconf tint2 pcmanfm lxterminal
sudo apt install x2goserver x2goserver-xsession
python 查詢網站 ssl 的憑證到期日
import ssl
import socket
from datetime import datetime
def get_ssl_expiry_date(hostname, port=443):
context = ssl.create_default_context()
with socket.create_connection((hostname, port)) as sock:
with context.wrap_socket(sock, server_hostname=hostname) as ssock:
cert = ssock.getpeercert()
# 取得過期日期並轉為 datetime 格式
expiry_str = cert['notAfter']
expiry_date = datetime.strptime(expiry_str, '%b %d %H:%M:%S %Y %Z')
return expiry_date
# 測試
hostname = 'tw.yahoo.com'
expiry_date = get_ssl_expiry_date(hostname)
print(f"{hostname} 的 SSL 憑證到期日是:{expiry_date}")