首页 > 编程语言 >nodejs 基于sharp + smartcrop 实现图片的智能提取排版

nodejs 基于sharp + smartcrop 实现图片的智能提取排版

时间:2023-11-05 22:15:06浏览次数:54  
标签:nodejs top smartcrop input sharp 图片 left

属于一个简单的demo 示例,主要是学习下sharp 包对于图片的处理,以及基于smartcrop.js 实现智能图片抠图
结合sharp提供的图片组合能力,实现一个基于模版的图片组合,代码很简单

简单任务描述

就是有一个图片,我们需要智能的提取核心信息,并生成一个确定大小的图片,然后基于将生成的图片填充到一个模版图片中
实现类似日常中大家的一寸照片生成效果

  • 参考代码
    package.json
 
{
  "name": "nodejs-images-sharp",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "sharp": "^0.32.6",
    "smartcrop-sharp": "^2.0.8"
  }
}

utils.js 一个简单的工具类,利用了smartcrop-sharp 进行图片的智能提取并转换为一个标准大小的图片

const sharp = require('sharp');
const smartcrop = require('smartcrop-sharp');
 
function applySmartCrop(src, width, height) {
  return smartcrop.crop(src, { width: width, height: height })
    .then(function(result) {
      const crop = result.topCrop;
      return sharp(src)
        .extract({ width: crop.width, height: crop.height, left: crop.x, top: crop.y })
        .resize(width, height)
        .toBuffer();
    })
}
 
module.exports = {
  applySmartCrop
};

com.js 基于组合能力进行图片的模版填充,位置部分使用了硬编码,实际可以自己调整

const sharp = require('sharp');
const util = require('./utils')
 
async function templateImage(source, width, height, output) {
    let smartcrop = await util.applySmartCrop(source, width, height)
    let items = [
        {
            input: smartcrop,
            top: 25,
            left: 25,
        },
        {
            input: smartcrop,
            top: 25,
            left: 340,
        },
        {
            input: smartcrop,
            top: 25,
            left: 655,
        },
        {
            input: smartcrop,
            top: 360,
            left: 25,
        },
        {
            input: smartcrop,
            top: 360,
            left: 340,
        },
        {
            input: smartcrop,
            top: 360,
            left: 655,
        },
        {
            input: smartcrop,
            top: 700,
            left: 25,
        },
        {
            input: smartcrop,
            top: 700,
            left: 340,
        },
        {
            input: smartcrop,
            top: 700,
            left: 655,
        }
    ];
    sharp("template.png").composite(items)
        .toFile(output)
}
 
templateImage("v3.png", 283, 300, "output.png")

效果

实际图片是一个非规则,长度比较大的图片

模版填充效果

说明

实际上sharp 也包含一个简单的智能裁剪方法,但是不是很好,比如smartcrop-sharp,所以利用几个工具的集成,可以实现一个相对可靠的智能裁剪,以及模版填充,当然也可以自己集合业务处理加上智能裁剪进行原始图片的处理(比如用户选择需要裁剪的部分图片,然后再利用库进行一些处处理,同时也可以添加一些其他抠图算法),实现更加灵活的图片处理

参考资料

https://github.com/lovell/sharp
https://github.com/libvips/libvips
https://github.com/jwagner/smartcrop.js
https://sharp.pixelplumbing.com/api-composite
https://github.com/jwagner/smartcrop-sharp
https://github.com/rongfengliang/sharp_smartcrop_learning

标签:nodejs,top,smartcrop,input,sharp,图片,left
From: https://www.cnblogs.com/rongfengliang/p/17811311.html

相关文章

  • nodejs 新版本build问题
    nodejs、vue问题记录【vue下载地址】https://unpkg.com/vue/dist/vue.jshttps://unpkg.com/vue@nexthttps://unpkg.com/vue@3/dist/vue.global.js【npm创建vue项目】npmcreatevue@latest 1、node新版本问题参考下:https://www.jianshu.com/p/edbad6875b7eNode.j......
  • NodeJS系列(13)- Next.js 框架 (六) | Node.js + Next.js + Prisma/Sequelize (ORM) + M
    Next.js是一个用于构建Web应用程序的框架。Next.js是一个用于生产环境的React框架,是一个React服务端渲染应用框架。NextJS:https://nextjs.org/Prisma是一个基于promise的Node.js和TypeScript的ORM,目前支持Mysql,MariaDB,SQLite,PostgreSQL,AWSAuroraServerles......
  • 无法加载文件 E:\nodejs\node_global\npm.ps1,因为在此系统上禁止运行脚本。有关详
    npminstall报错解决办法打卡windospowershell并且以管理员运行输入命令set-executionpolicyremotesignedY......
  • Nodejs安装教程
    1.下载 在https://nodejs.cn/download/current/ 下载安装包,我下载的是  node-v18.18.0-x64.msi2.双击安装包,一路点击下一步,我选择安装到D:\nodejs\ 目录3.验证安装  在cmd命令行窗口中,运行  node-v 及 npm-v 将显示版本号4 修改全局模块下载路径4.1......
  • 新手使用nodejs的SerialPort获取数据时需要注意的一个小点
    onData(callback:(data:Buffer)=>void):void{if(this.serialPort!=null){this.serialPort.on("data",(data:Buffer)=>{callback(data);})}}​ 上面的代码相当于当串口接收到数据之后就通知......
  • 【nodejs】Windows环境 ffmpeg添加水印
    一、Windows下面获取到的字体路径需要做处理,否则无法执行路径中:改为\:路径中:\改为/不要使用中文的名称 原路径:D:\Users\670493228\Desktop\public\font\default.ttf  使用水印命令(-logleveldebug可以看到执行日志,方便定位问题)ffmpeg-i1.mp4-vf"draw......
  • 【nodejs】批量获取文件的时长
    因为ffmpeg.ffprobe是异步的,需要使用promise同步,然后Promise.all将所有的Promise执行结果处理后返回/***获取文件列表时长(同步获取返回)*@param{Object}fileList*/asyncgetFileDuration(fileList){//初始化letpromises=fileList.map(fil......
  • Error: EPERM: operation not permitted, mkdir 'C:\Program Files\nodejs\cache\
    使用下面命令创建react项目爆出的错误npxcreate-react-appreact-basic显示nodejs里面的文件权限不够,需要进行文件夹的权限更改,改为完全控制就可以了。 ......
  • 如何使用nodejs对接【企查查开放平台API服务】
    下面分享一下如何对接企查查API接口相关内容。API服务对接流程:1、登录|注册:先打开企查查开放平台网站(https://openapi.qcc.com),菜单栏中右上角【登录|注册】;2.打开API服务列表通过顶部导航菜单【API】,可以查看所有服务列表,里面包含了很多API接口,此处不具体介绍,有兴趣的可以自己......
  • graalvm 23.1.0 独立nodejs docker 镜像&简单试用
    graaljsdocker镜像很简单就是下载官方包,集成下,然后进行一些简单的配置DockerfileFROMdebian:bullseye-backportsLABELauthor="rongfengliang"LABELemail="[email protected]"WORKDIR/opt/RUN/bin/cp/usr/share/zoneinfo/Asia/Shanghai/etc/localtime\&&am......