1、图片尺寸修改
from PIL import Image
def resizeImage(image_in, image_out):
"""
修改jpg 图片尺寸到 200kb 左右
"""
width = 1600
img = Image.open(image_in)
w, h = img.size
# print(w, h)
height = 1600 * h // w
# print(height)
out = img.resize((width, height), Image.ANTIALIAS) # resize image with high-quality
out.save(image_out)
def test1():
import ocr
rpa.win32.win_activate("金蝶")
sleep(2)
x1, y1 = rpa.win32.pos("应收单底部金额1_img")
x2, y2 = rpa.win32.pos("应收单底部金额2_img")
img_in = r"D:\img_in.png"
img_out = r"D:\img_out.png"
import pyautogui
pyautogui.FAILSAFE = False
pyautogui.screenshot(imageFilename=img_in, region=(x1, y1, int(x2) - int(x1), int(y2) - int(y1)))
resizeImage(img_in, img_out)
#默认aliyun ocr获取结果,access_key_id及access_key_secret替换成自己的
text = ocr.text(img_out, app_code='129fe845778e40a69fdf84c09b601be8')
2、图片二值话,把有颜色的图片变成黑白图片
from PIL import Image
def convert_Binary_image(filepath, newfilepath):
# 自定义灰度界限,大于这个值为黑色,小于这个值为白色
threshold = 200
img = Image.open(filepath)
# 灰度化
img_gray = img.convert("L")
img_binary = img_gray.point(lambda x: 0 if x < threshold else 255)
img_binary.show()
img_binary.save(newfilepath)
return img_binary
convert_Binary_image(r"D:\img_in.png", r"D:\imag_in.png")