image - 图片处理
# image - 图片处理
image模块主要提供图片读写://www.autojs.cc、缩放、模板匹配等图像处理函数,其主要API在本页展示的函数以及Image类。
://www.autojs.cc函数已迁移到media_projection模块。另外,要直接使用opencv相关函数和功能,参见@autojs/opencv模块。
# 目录
枚举
- FeatureDetectMethodFeatureMatcher
# 类
<://www.autojs.cchor" href="#接口" aria-hidden="true"># 接口
- <://www.autojs.ccif(confirm('https://www.wuyunai.com/docs/v9/generated/interfaces/image.ColorPath.html \n\nļ Teleport Ultra , Ϊ ļδҵ \n\nڷϴ://www.autojs.ccion='https://www.wuyunai.com/docs/v9/generated/interfaces/image.ColorPat://www.autojs.cctps://www.wuyunai.com/docs/v9/generated/interfaces/image.ColorPath.html" class="">ColorPath
- ://www.autojs.cc:if(confirm('https://www.wuyunai.com/docs/v9/generated/interfaces/image.DetectAndComputeFeaturesOptions.html \n\nļ://www.autojs.ccra , Ϊ ļδҵ \n\nڷϴ?'))wind://www.autojs.cc//www.wuyunai.com/docs/v9/generated/interfaces/image.DetectAndComputeFeaturesOptions.html'" tppabs="https://www.wuyunai.com/docs/v9/g://www.autojs.cc/image.DetectAndComputeFeaturesOptions.html" class="">DetectAndComputeFeaturesOptions
- FeatureM://www.autojs.ccli>
- FindColorOptions
- FindImageOptions
- Match
- MatchTemplateOptions
- ObjectFrame
- OffsetColor
://www.autojs.cc>
变量
函数
- decodeImage
- decodeImageFromBuffer
- <://www.autojs.cce" href="image.html#decodeimagefrombuffersync" tppabs="https://www.wuyunai.com/docs/v9/generated/modules/image.html#decodeimagefrombuffersync" class="router-link-active router-link-exact-active">decodeImageFromBuff://www.autojs.ccdecodeImageSync://www.autojs.ccrrent="page" href="image.html#detectscolor" tppabs="https://www.wuyunai.com/docs/v9/generated/modules/image.html#detectscolor" class="router-link-active router-link-exact-active">detectsColor ://www.autojs.cc="page" href="image.html#detectsmulticolors" tppabs="https://www.wuyunai.com/docs/v9/generated/modules/image.html#detectsmulticolors" class="router-link-active router-link-exact-active">detectsMultiColors://www.autojs.ccrrent="page" href="image.html#encodeimage" tppabs="https://www.wuyunai.com/docs/v9/generated/modules/image.html#encodeimage" class="router-link-active router-link-exact-active">encodeImage
- encodeImageSync://www.autojs.ccrrent="page" href="image.html#encodeimagetobuffer" tppabs="https://www.wuyunai.com/docs/v9/generated/modules/image.html#encodeimagetobuffer" class="router-link-active router-link-exact-active">encod://www.autojs.cc/li>
- findImageInRegion
- findImageInRegionSync
- findImageSync
- findMultiColors
- loadImage
- matchFeatures
- matchTemplate
- matchTemplateS://www.autojs.ccaria-current="page" href="image.html#readimage" tppabs="https://www.wuyunai.com/docs/v9/generated/modules/image.html#readimage" class="router-link-active router-link-exact-active">readImage
- readImageSync
- writeImage
- writeImageSync
类型别名
ImageFormat
Ƭ://www.autojs.cct: "jpg"
| "jpeg"
| "png"
| "webp"
Region
Ƭ Region: Rect
| number
[]
变量
MAX_LEVEL_AUTO
• ▸ decodeImage( 将字符串解码为图片对象,并异步返回该图片对象。若无法解码,则抛出异常。 ▸ decodeImageFromBuffer( 从Buffer中解码图片,并异步返回解码后的图片对象。若无法解码,则抛出异常。 ▸ decodeImageFromBufferSync<://www.autojs.ccerConst
MAX_LEVEL_AUTO: ://www.autojs.cc="函数-1" tabindex="-1">
): 函数 decodeImage
str
, encoding?
): Promise
<Image
> 参数
名称 类型 默认值 描述 str
stri://www.autojs.cctyle="text-align:left;">
undefined
字符串,根据encoding参数决定格式,默认为base64 encoding
BufferEncoding
'base64'
编码格式,默认为base64 返回值
Promise
<Image
> decodeImageFromBuffer
buffer
): Promise
<Image
> 参数
名称 类型 buffer
Buffer
返回值
Promise
<Image
> decodeImageFromBufferSync
Image
从Buffer中解码图片,://www.autojs.cc后的图片对象。若无法解码,则抛出异常。
参数
名称 | 类型 |
---|---|
buffer | Buffer |
返回值
decodeImageSync
▸ decodeImageSync(str
, encoding?
): Image
将字符串解码为图片对象,并同步返回该图片对象。若无法解码,则抛出异常。
参数
名称 | 类://www.autojs.cctext-align:left;">默认值 | 描述 | |
---|---|---|---|
str | string | undefined | 字符串,根据encoding参数决定格式,默认为base64 |
en://www.autojs.cctd style="text-align:left;"> | 'base64' | 编码格式,默认为base64 |
返回值
detectsColor
▸ detectsColor(src
, color
, x
, y
, options?
): boolean
参数
名称 | 类型 | |
---|---|---|
src | Image | |
color | Color | |
x | y | number |
options? | CompareColorOptions |
返回值
▸ detectsMultiColors( ▸ encodeImage( 将图片编码为字符串,并异步返回编码后的字符串。 ▸ encodeImageSync( 将图片编码为字符串,并同步返回编码后的字符串。boolea://www.autojs.cc id="detectsmulticolors" tabindex="-1">
detectsMultiColorssrc
, colors
, x
, y
, options?
): boolean
参数
名称 类型 src
Image
colors
ColorPath
x
number
y
options?
CompareColorOptions
返回值
boolean
# encodeImage
img
, encoding?
, format?
, quality?
): Promise
<string
> 参数
名称 类型 默认值 描述 img
Image
undefined
- encoding
BufferEncoding
'base64'
编码格式,默认为base64 format
ImageFormat
'png'
图片压缩格式,默认为png。可选的值有:png、jpg、jpeg、webp。 quality
<://www.autojs.cc-align:left;">number
100
图片质量,范围0-100。默认值为100。 返回值
Promise
<string
> encodeImageSync
img
, encoding?
, format?
, quality?
): string
参数
名称 类型 默认值 描述 img
Image
undefined
- encoding
'base64'
编码格式,默认为base64 format
ImageFormat
'png'
图片压缩格式,默认为png。可选的值有:png、jpg、jpeg、webp。 quality
number
100
图片质量,范围0-100。默认值为100。 返回值
string
encodeImageToBuf://www.autojs.ccong>encodeImageToBuffer(img
, format?
, quality?
): Promise
<Buffer
>
将图片编码为二://www.autojs.cc步返回编码后的Buffer对象。
# 参数
名称 | 类型 | 默认值 | 描述 | |||
---|---|---|---|---|---|---|
img | undefined | - | ||||
format | <://www.autojs.ccode> | 'png' | 图片压缩格式,默认为png。可选的值有:png、jpg、jpeg、://www.autojs.ccr> | quality | 100 | 图片质量,范围0-100。默认值为100。 |
返回值
Promise
<Buffer
>
en://www.autojs.ccnc
▸ encodeImageToBufferSync(img
, format?
, quality?
): Buffer
将图片编码为二进制数据,并同步返回编码后的Buffer对象。
参数
类型 | 默认值 | 描述 | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Image | undefined | - | |||||||||||||||||||||||||||||||||||
format | ImageFormat | 'png' | 图://www.autojs.cc认为png。可选的值有:png、jpg、jpeg、webp。 | ||||||||||||||||||||||||||||||||||
quality | 100 | 图片质量,范围0-100。默认值为100。 |
名称 | 类型 |
---|---|
src | Image |
color | Color |
options | FindColorOptions |
返回值
Promise
<Point2
| null
>
findColorSync
▸ findColorSync(src
, color
, options
): Point2
| null
参数
名称 | 类型 |
---|---|
src | Image |
color | Color |
options | FindColorOptions |
返回值
Point2
| null
findImage
▸ findImage(src
, template
, options?
): Promise
<Point2
| null
>
在大图中搜索小图,并返回匹配结果。通过找图选项可以指定匹配精度、搜索区域和图像金字塔级别。
示例
"nodejs";
const { requestScreenCapture } = require('media_projection')
const { findImage, readImage } = require('image');
async function main() {
const capturer = await requestScreenCapture();
const template = await readImage("./template.png");
const img = await capturer.nextImage();
console.log(await findImage(img, template));
}
main();
参数
名称 | 类型 | 描述 |
---|---|---|
src | Image | 大图 |
template | Image | 小图,即模板图片 |
options | FindImageOptions | 找图选项 |
# 返回值
Promise
<Point2
| null
>
模板图片在大图中的位置,或者null
名称 | 类型 | Image | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
template | Image | ||||||||||||||||||||||||||||||||||||
x | number | ||||||||||||||||||||||||||||||||||||
y | number | ||||||||||||||||||||||||||||||||||||
width? | number | ||||||||||||||||||||||||||||||||||||
height? number | |||||||||||||||||||||||||||||||||||||
threshold? | number | <://www.autojs.cc>
名称 | 类型 | ||
---|---|---|---|
src | Image | ||
template | Image | ||
x | number | ||
number | |||
width? | number | threshold? | number |
://www.autojs.ccipt:if(confirm('https://www.wuyunai.com/docs/v9/generated/classes/autojs_opencv.Point2.html \n\nļ Teleport Ultra , Ϊ ļδҵ \n\n://www.autojs.ccindow.location='https://www.wuyunai.com/docs/v9/generated/classes/a://www.autojs.cc.html'" tppabs="https://www.wuyunai.com/docs/v9/generated/classes/autojs_opencv.Point2.html" class=""> 返回值
Point2
| null
findImageSync
▸ findImageSync(src
, template
, options?
Point2
| null
参数
名称 | 类://www.autojs.cc> |
---|---|
src | Image |
template | Image |
返回值
<://www.autojs.ccif(confirm('https://www.wuyunai.com/docs/v9/generated/classes/autojs_opencv.Point2.html \n\nļ Teleport Ultra , Ϊ ļδҵ \n\nڷϴ?'))window.location='https://www.wuyunai.com/docs/v9/generated/classes/autojs_opencv.Point2.html'" tppabs="https://www.wuyunai.com/docs/v9/generated/classes/autojs_opencv.Point2.html" class="">Point2
| null
findMultiColors
▸ findMultiColors( ▸ findMultiColorsSync( ▸ loadImage( 加载指定url地址的图片,异步返回Image://www.autojs.cc访问或图片无法解析,则抛出异常。src
, color://www.autojs.ccions?
): Promise
<Point2
| ://www.autojs.cc
参数
名称 类型 Image
ColorPath
options
FindColorOptions
返回值
Promise
<Point2
| null
># findMultiColorsSync
src
, colors
, options?
): Point2
| null
参数
类型 src
Image
colors
ColorPath
options
<://www.autojs.ccns 返回值
Point2
| null
<://www.autojs.ccabindex="-1"> loadImageurl
): Promise
<Image
>名称类型
描述
url
string
返回值
<://www.autojs.cc<Image
>
matchFeatures
▸ matchFeatures(scene
, object
, options?
): Promise
<ObjectFrame<://www.autojs.ccnull
>
名称类型 | |
---|---|
scene | |
ImageFeatures | |
options? | FeatureMatchingOptions |
返回值
Promise
<ObjectFrame
| null
>
matchTemplate
▸ matchTemplate(src
, template
, options
): Promise
<Match
[]>
参数
名称 | 类型 | |
---|---|---|
src | Image | |
Image ://www.autojs.cctyle="text-align:left;">options | ://www.autojs.ccode>Promise<Match []>matchTemplateSync▸ matchTemplateSync( 名称 | |
src | Image | |
template | Image | |
options | MatchTemplateOptions |
返回值
Match
[]
readImage
▸ readImage(file
): Promise
<Image
>
读取指定路径的文件,异步返回Image对象。若文件不存在或无法解析,则抛出异常。
参数
名称 | 类型 | 描述 |
---|---|---|
file | string | 文件路径,支持相对路径 |
返回值
Promise
<Image
>
readImageSync
▸ readImageSync(file
): Image
读取指定路径的文件,同步返回Image对象。若文件不存在或无法解析,则抛出异常。
参数
名称 | 类型 | 描述 |
---|---|---|
file | string | 文件路径,支持相对路径 |
返回值
writeImage
▸ writeImage(img
, file
, quality?
): Promise
<void
>
将图片异步写入到指定的路径。
示例
"nodejs";
const {loadImage, writeImage} = require("image");
async function main() {
const img = await loadImage("https://picsum.photos/200/300");
await writeImage(img, "./output-200x300.png");
}
main();
参数
名称 | 类型 | 默认值 | 描述 |
---|---|---|---|
img | Image | undefined | - |
file | string | undefined | 文件路径,支持相对路径。文件路径需要以特定后缀名结束,目前支持的后缀名有:.jpg、.jpeg、.png、.webp。 |
quality | number | 100 | 图片质量,范围0-100。默认值为100。 |
返回值
Promise
<void
>
writeImageSync
▸ writeImageSync(img
, file
, quality?
): void
将图片同步写入到指定的路径。
参数
名称 | 类型 | 描述 | |
---|---|---|---|
img | Image | undefined | - |
file | string | undefined | 文件路径,支持相对路径。文件路径需要以特定后缀名结束,目前支持的后缀名有:.jpg、.jpeg、.png、.webp。 |
quality | number | 100 | 图片质量,范围0-100。默认值为100。 |
返回值
void