跳至主要內容

image - 图片处理

约 1813 字大约 6 分钟老猫

# image - 图片处理

image模块主要提供图片读写://www.autojs.cc、缩放、模板匹配等图像处理函数,其主要API在本页展示的函数以及Image类。

://www.autojs.cc函数已迁移到media_projection模块。另外,要直接使用opencv相关函数和功能,参见@autojs/opencv模块。

枚举

#

<://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>
  • ImageFormat<://www.autojs.ccrent="page" href="image.html#region" tppabs="https://www.wuyunai.com/docs/v9/generated/modules/image.html#region" class="router-link-active router-link-exact-active">Region
  • 变量

    函数

    类型别名

    ImageFormat

    Ƭ://www.autojs.cct: "jpg" | "jpeg" | "png" | "webp"


    Region

    Ƭ Region: Rect | number[]

    变量

    MAX_LEVEL_AUTO

    Const MAX_LEVEL_AUTO: ://www.autojs.cc="函数-1" tabindex="-1"> 函数

    decodeImage

    decodeImage(str, encoding?): Promise<Image>

    将字符串解码为图片对象,并异步返回该图片对象。若无法解码,则抛出异常。

    参数

    名称类型默认值描述
    strstri://www.autojs.cctyle="text-align:left;">undefined字符串,根据encoding参数决定格式,默认为base64
    encodingBufferEncoding'base64'编码格式,默认为base64

    返回值

    Promise<Image>


    decodeImageFromBuffer

    decodeImageFromBuffer(buffer): Promise<Image>

    从Buffer中解码图片,并异步返回解码后的图片对象。若无法解码,则抛出异常。

    参数

    buffer
    名称类型
    Buffer

    返回值

    Promise<Image>


    decodeImageFromBufferSync

    decodeImageFromBufferSync<://www.autojs.ccer): Image

    从Buffer中解码图片,://www.autojs.cc后的图片对象。若无法解码,则抛出异常。

    参数

    名称类型
    bufferBuffer

    返回值

    Image


    decodeImageSync

    decodeImageSync(str, encoding?): Image

    将字符串解码为图片对象,并同步返回该图片对象。若无法解码,则抛出异常。

    参数

    名称类://www.autojs.cctext-align:left;">默认值描述
    strstringundefined字符串,根据encoding参数决定格式,默认为base64
    en://www.autojs.cctd style="text-align:left;">BufferEncoding'base64'编码格式,默认为base64

    返回值

    Image


    detectsColor

    detectsColor(src, color, x, y, options?): boolean

    参数

    名称类型
    srcImage
    colorColor
    xynumber
    options?CompareColorOptions

    返回值

    boolea://www.autojs.cc id="detectsmulticolors" tabindex="-1"> detectsMultiColors

    detectsMultiColors(src, colors, x, y, options?): boolean

    参数

    名称类型
    srcImage
    colorsColorPath
    xnumber
    yoptions?CompareColorOptions

    返回值

    boolean


    encodeImage(img, encoding?, format?, quality?): Promise<string>

    将图片编码为字符串,并异步返回编码后的字符串。

    参数

    名称类型默认值描述
    imgImageundefined-
    encodingBufferEncoding'base64'编码格式,默认为base64
    formatImageFormat'png'图片压缩格式,默认为png。可选的值有:png、jpg、jpeg、webp。
    quality<://www.autojs.cc-align:left;">number100图片质量,范围0-100。默认值为100。

    返回值

    Promise<string>


    encodeImageSync

    encodeImageSync(img, encoding?, format?, quality?): string

    将图片编码为字符串,并同步返回编码后的字符串。

    参数

    名称类型默认值描述
    imgImageundefined-
    encoding'base64'编码格式,默认为base64
    formatImageFormat'png'图片压缩格式,默认为png。可选的值有:png、jpg、jpeg、webp。
    qualitynumber100图片质量,范围0-100。默认值为100。

    返回值

    string


    encodeImageToBuf://www.autojs.ccong>encodeImageToBuffer(img, format?, quality?): Promise<Buffer>

    将图片编码为二://www.autojs.cc步返回编码后的Buffer对象。

    名称类型默认值描述
    imgundefined-
    format<://www.autojs.ccode>'png'图片压缩格式,默认为png。可选的值有:png、jpg、jpeg、://www.autojs.ccr>quality100图片质量,范围0-100。默认值为100。

    返回值

    Promise<Buffer>


    en://www.autojs.ccnc

    encodeImageToBufferSync(img, format?, quality?): Buffer

    将图片编码为二进制数据,并同步返回编码后的Buffer对象。

    参数

    返回值

    Buffer


    findColor

    findColor(src, color, options): Promise<<://www.autojs.ccif(confirm('https://www.wuyunai.com/docs/v9/generated/classes/autojs_opencv.Point2.html \://www.autojs.cceport Ultra , Ϊ ļδҵ \n\nڷ://www.autojs.cclocation='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>

    参数

    类型默认值描述
    Imageundefined-
    formatImageFormat'png'图://www.autojs.cc认为png。可选的值有:png、jpg、jpeg、webp。
    quality100图片质量,范围0-100。默认值为100。
    名称类型
    srcImage
    colorColor
    optionsFindColorOptions

    返回值

    Promise<Point2 | null>


    findColorSync

    findColorSync(src, color, options): Point2 | null

    参数

    名称类型
    srcImage
    colorColor
    optionsFindColorOptions

    返回值

    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();
    

    参数

    名称类型描述
    srcImage大图
    templateImage小图,即模板图片
    optionsFindImageOptions找图选项

    Promise<Point2 | null>

    模板图片在大图中的位置,或者null


    Point2 | null://www.autojs.cc数-13" tabindex="-1"> 参数

    src<://www.autojs.cc>

    # 返回值

    Promise<Point2 | null>


    findImageInRegionSync

    ▸://www.autojs.ccnRegionSync(src, template, x, y, width?, height?, threshold?): Point2 | null<://www.autojs.cc4" tabindex="-1"> 参数

    名称类型
    Image
    templateImage
    xnumber
    ynumber
    width?number
    height?number
    threshold?number
    名称类型
    srcImage
    templateImage
    xnumber
    number
    width?numberheight?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>
    srcImage
    templateImage

    返回值

    <://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(src, color://www.autojs.ccions?): Promise<Point2 | ://www.autojs.cc

    参数

    名称类型
    Image
    ColorPath
    optionsFindColorOptions

    返回值

    Promise<Point2 | null>


    # findMultiColorsSync

    findMultiColorsSync(src, colors, options?): Point2 | null

    参数

    类型
    srcImage
    colorsColorPath
    options<://www.autojs.ccns

    返回值

    Point2 | null


    <://www.autojs.ccabindex="-1"> loadImage

    loadImage(url): Promise<Image>

    加载指定url地址的图片,异步返回Image://www.autojs.cc访问或图片无法解析,则抛出异常。

    名称类型描述urlstring图://www.autojs.cchttps或http开头

    返回值

    <://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[]>

    参数

    <://www.autojs.ccn:left;">template
    名称类型
    srcImage
    Image://www.autojs.cctyle="text-align:left;">options ://www.autojs.ccode>Promise<Match[]>


    matchTemplateSync

    matchTemplateSync(src, template, options): Match[]

    名称

    类型
    srcImage
    templateImage
    optionsMatchTemplateOptions

    返回值

    Match[]


    readImage

    readImage(file): Promise<Image>

    读取指定路径的文件,异步返回Image对象。若文件不存在或无法解析,则抛出异常。

    参数

    名称类型描述
    filestring文件路径,支持相对路径

    返回值

    Promise<Image>


    readImageSync

    readImageSync(file): Image

    读取指定路径的文件,同步返回Image对象。若文件不存在或无法解析,则抛出异常。

    参数

    名称类型描述
    filestring文件路径,支持相对路径

    返回值

    Image


    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();
    

    参数

    名称类型默认值描述
    imgImageundefined-
    filestringundefined文件路径,支持相对路径。文件路径需要以特定后缀名结束,目前支持的后缀名有:.jpg、.jpeg、.png、.webp。
    qualitynumber100图片质量,范围0-100。默认值为100。

    返回值

    Promise<void>


    writeImageSync

    writeImageSync(img, file, quality?): void

    将图片同步写入到指定的路径。

    参数

    名称类型描述
    imgImageundefined-
    filestringundefined文件路径,支持相对路径。文件路径需要以特定后缀名结束,目前支持的后缀名有:.jpg、.jpeg、.png、.webp。
    qualitynumber100图片质量,范围0-100。默认值为100。

    返回值

    void