常用python脚本

2022-02-19 09:23:00 浏览数 (1)

二维码识别

代码语言:javascript复制
import pyzbar.pyzbar as pyzbar
from PIL import Image,ImageEnhance

image = "139_00.png"
img = Image.open(image)
#img = ImageEnhance.Brightness(img).enhance(2.0)#增加亮度
#img = ImageEnhance.Sharpness(img).enhance(17.0)#锐利化
#img = ImageEnhance.Contrast(img).enhance(4.0)#增加对比度
#img = img.convert('L')#灰度化
img.show()
barcodes = pyzbar.decode(img)
for barcode in barcodes:
    barcodeData = barcode.data.decode("utf-8")
    print(barcodeData)

验证码识别

代码语言:javascript复制
import time
import muggle_ocr
import os
sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.OCR)
root_dir = r"./imgs"
for i in os.listdir(root_dir):
    n = os.path.join(root_dir, i)
    with open(n, "rb") as f:
        b = f.read()
    st = time.time()
    text = sdk.predict(image_bytes=b)
    print(i, text, time.time() - st)

图片webp转jpg

代码语言:javascript复制
import os
from PIL import Image
path = "1.webp"
im=Image.open(path)
im.load()
im.save("1.jpg")
os.remove(path)

图片jpg转webp

代码语言:javascript复制
#coding=utf-8
from PIL import Image
im = Image.open('3.jpeg').convert("RGB")
im.save("3.webp", "WEBP")

python实现浏览器截图

代码语言:javascript复制
from selenium import webdriver
import time

# 设置你自己的chormedriver存放路径,并把chromedriver.exe放到该路径下
driver_path = r"E:GoogleChromeApplicationchromedriver.exe"
# 传入chromedriver路径
driver = webdriver.Chrome(executable_path=driver_path)
driver.maximize_window()
driver.implicitly_wait(10)
driver.get("https://ctf.bugku.com/login")
time.sleep(1)
erweima=driver.find_element_by_xpath('//*[@id="vcode"]')
# 指定元素截图
erweima.screenshot("55.png")

# # 整个浏览器截图
# driver.save_screenshot("3.png")
driver.close()

读取某目录下的文件名

代码语言:javascript复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
import os
def file_name(file_dir):
    for root, dirs, files in os.walk(file_dir):
        print(root)  # 当前目录路径
        print(dirs)  # 当前路径下所有子目录
        print(files)  # 当前路径下所有非目录子文件
file_name('./')

0 人点赞