跳到主要内容

图像处理

from ascript.ios.screen.gp_tasks import GrayImage, Threshold, Erode, Dilate, MorphClose, MorphOpen, GaussianBlur, Resize, Rotate, Canny

AScript iOS 内置了基于 OpenCV 的图像处理工具类,可以对截图或图片进行预处理,常用于提高 OCR 和找色的准确率。

图色工具链

这些类可以在图色助手中可视化调参,也可以在脚本中直接调用。所有类的 run() 方法接收 OpenCV 格式的图像(numpy.ndarray),返回 Result 对象。

获取 OpenCV 格式图像​

from ascript.ios import screen

# 方式1:直接获取 CV 格式截图
cv_img = screen.capture(format=screen.FORMAT_CV_MAT)

# 方式2:PIL 图片转 CV
import numpy as np
import cv2
pil_img = screen.capture()
cv_img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)

灰度化​

将彩色图像转为灰度图,是二值化等操作的前置步骤。

GrayImage()
  • 示例
from ascript.ios import screen
from ascript.ios.screen.gp_tasks import GrayImage
import numpy as np, cv2

cv_img = screen.capture(format=screen.FORMAT_CV_MAT)
gray = GrayImage()
result = gray.run(cv_img)
# result.image 为灰度图

二值化​

将图像转为黑白两色,常用于文字提取和轮廓检测。

Threshold(threshold, inv=False)
参数类型默认值说明
thresholdint-阈值 0-255,像素值大于阈值为白色,小于为黑色
invboolFalse是否反转,True 时大于阈值为黑色
  • 示例
from ascript.ios import screen
from ascript.ios.screen.gp_tasks import GrayImage, Threshold

cv_img = screen.capture(format=screen.FORMAT_CV_MAT)

# 先灰度化再二值化
gray = GrayImage().run(cv_img)
binary = Threshold(127).run(gray.image)

# 反转二值化(白底黑字 → 黑底白字)
binary_inv = Threshold(127, inv=True).run(gray.image)

腐蚀​

缩小图像中的白色区域,用于去除小噪点。

Erode(kernel_size=5, iterations=1)
参数类型默认值说明
kernel_sizeint5核大小,越大腐蚀效果越强
iterationsint1腐蚀次数
  • 示例
from ascript.ios.screen.gp_tasks import Erode

# 在二值化图像上腐蚀去噪
eroded = Erode(3, iterations=2).run(binary.image)

膨胀​

扩大图像中的白色区域,用于连接断裂的线条。

Dilate(kernel_size=5, iterations=1)
参数类型默认值说明
kernel_sizeint5核大小,越大膨胀效果越强
iterationsint1膨胀次数
  • 示例
from ascript.ios.screen.gp_tasks import Dilate

# 膨胀连接断裂的文字笔画
dilated = Dilate(3).run(binary.image)

开运算​

先腐蚀后膨胀,用于去除小噪点同时保持大区域形状不变。

MorphOpen(kernel_size=5, iterations=1)
参数类型默认值说明
kernel_sizeint5核大小
iterationsint1运算次数
  • 示例
from ascript.ios.screen.gp_tasks import MorphOpen

opened = MorphOpen(5).run(binary.image)

闭运算​

先膨胀后腐蚀,用于填充小孔洞和连接相近的区域。

MorphClose(kernel_size=5, iterations=1)
参数类型默认值说明
kernel_sizeint5核大小
iterationsint1运算次数
  • 示例
from ascript.ios.screen.gp_tasks import MorphClose

closed = MorphClose(5).run(binary.image)

高斯模糊​

对图像进行高斯模糊处理,用于降噪和平滑。

GaussianBlur(ksize=5, sigmaX=0)
参数类型默认值说明
ksizeint5核大小(必须为奇数)
sigmaXint0X方向标准差,0为自动计算
  • 示例
from ascript.ios.screen.gp_tasks import GaussianBlur

blurred = GaussianBlur(5).run(cv_img)

缩放​

调整图像大小。

Resize(width=0, height=0, scale=0)
参数类型默认值说明
widthint0目标宽度(0=自动按比例计算)
heightint0目标高度(0=自动按比例计算)
scalefloat0缩放比例,如0.5为缩小一半(优先级最高)
  • 示例
from ascript.ios.screen.gp_tasks import Resize

# 按比例缩小一半
resized = Resize(scale=0.5).run(cv_img)

# 指定宽度,高度自动按比例
resized = Resize(width=640).run(cv_img)

旋转​

旋转图像。

Rotate(angle=90, expand=True)
参数类型默认值说明
anglefloat90旋转角度(逆时针)
expandboolTrue是否扩展画布以容纳整张图
  • 示例
from ascript.ios.screen.gp_tasks import Rotate

rotated = Rotate(45).run(cv_img)

边缘检测 Canny​

检测图像中的轮廓和边缘,返回检测到的轮廓信息列表。

Canny(low_threshold=50, high_threshold=150)
参数类型默认值说明
low_thresholdint50低阈值
high_thresholdint150高阈值
algostr"canny"算法:"canny" / "sobel" / "laplacian" / "adaptive"
blurint3预处理高斯模糊核大小,0为不模糊
area_rangelist[0, 0]面积过滤范围 [min, max],0=不限制
width_rangelistNone宽度过滤范围 [min, max]
height_rangelistNone高度过滤范围 [min, max]
detect_shapeboolFalse是否识别形状(circle/rectangle/triangle等)

静态方法​

快速调用,自动截图:

Canny.detect(low_threshold=50, high_threshold=150, image=None)
参数类型默认值说明
low_thresholdint50低阈值
high_thresholdint150高阈值
imagePIL.Image/ndarrayNone要检测的图片,默认自动截图
  • 返回值

轮廓信息列表:

[
{
"x": 100, "y": 200, # 左上角坐标
"w": 50, "h": 30, # 宽高
"area": 1500, # 面积
"perimeter": 160.0, # 周长
"cx": 125, "cy": 215, # 质心坐标
"vertices": 4, # 顶点数
"shape": "rectangle", # 形状(需开启 detect_shape)
"aspect": 1.67 # 宽高比
}
]
  • 示例
from ascript.ios.screen import Canny

# 快速检测当前屏幕轮廓
results = Canny.detect()
for r in results:
print(f"位置: ({r['x']},{r['y']}) 大小: {r['w']}x{r['h']} 面积: {r['area']}")

# 指定阈值和面积过滤
results = Canny.detect(low_threshold=30, high_threshold=100, area_range=[500, 50000])

综合示例:OCR 预处理​

from ascript.ios import screen
from ascript.ios.screen import Ocr
from ascript.ios.screen.gp_tasks import GrayImage, Threshold, Erode, Dilate

# 1. 截图并转 CV 格式
cv_img = screen.capture(format=screen.FORMAT_CV_MAT)

# 2. 灰度化
gray = GrayImage().run(cv_img)

# 3. 二值化(调整阈值以适应背景)
binary = Threshold(140).run(gray.image)

# 4. 膨胀让文字更清晰
dilated = Dilate(3).run(binary.image)

# 5. 转回 PIL 进行 OCR
from PIL import Image
import cv2
pil_img = Image.fromarray(dilated.image)
result = Ocr.paddleocr(image=pil_img)
for r in result:
print(r['text'])