当前位置: 首页 > ai >正文

图片压缩工具 | 按指定高度垂直切割图片

OPEN-IMAGE-TINY,一个基于 Electron + VUE3 的图片压缩工具,项目开源地址:https://github.com/0604hx/open-image-tiny

ℹ️ 需求描述

在上一篇文章一段代码利用 snapdom 将 CSDN 博文转化为长图片(PNG/JPG/PDF)中,我截取到一张长图,想要垂直切割成多张高度一致的小图。虽然用美图秀秀之类的软件很快就能做到,奈何我觉得图片压缩工具也应该有这样的一个功能,于是就有了这篇文章😄。

功能说明:

  • 只需要按设定的高度垂直切割
  • 最后一张小图高度不足时填充指定颜色

🧑‍💻 核心代码

后端

//electron\tool.js
/*** @typedef {Object} SplitConfig - 切割配置* @property {Number} height - 高度* @property {Boolean} fit - 是否自动填充* @property {String} bgColor - 填充颜色** 垂直切割图片** @param {String} origin - 原图片* @param {SplitConfig} config - 配置*/
exports.splitImageVertical = async (origin, config)=>{const { width, height } = await this.readImgSize(origin)config.fit ??= trueconst ext = path.extname(origin)const count = Math.ceil(height / config.height)const outputDir = path.join(path.dirname(origin), `${path.basename(origin, ext)}-${config.height}px`)if(!existsSync(outputDir))mkdirSync(outputDir)const image = sharp(origin)let fileCount = 0let started = Date.now()for(let i=0;i<count;i++){const top = i * config.heightconst curHeight = Math.min(config.height, height - top)let chunk = image.clone().extract({ left:0, top, width, height: curHeight })//自动填充白色背景if(config.fit === true && config.height > curHeight){chunk = chunk.extend({ top:0, bottom: config.height - curHeight, left:0, right:0, background: config.bgColor||"#ffffff" })}let outFile = path.join(outputDir, `切割-${i+1}.${ext}`)await chunk.toFile(outFile)console.debug(`切割图片 > ${outFile}`)fileCount ++}return { total:fileCount, dir: outputDir, used: Date.now() - started }
}

注册 ipcMain 处理函数:

const handlers = {/**** @param {Electron.IpcMainInvokeEvent} e* @param {String} filePath* @param {import("./tool").SplitConfig} config* @returns {Object}*/'split': async (e, filePath, config)=> await splitImageVertical(filePath, config)
}

UI界面

<template><n-form :show-feedback="false" label-placement="left"><n-flex vertical><n-form-item label="切割高度"><n-input-number class="cell" :min="0" :step="50" v-model:value="config.height"><template #suffix><Tag>px</Tag></template></n-input-number></n-form-item><n-form-item label="自动填充"><n-switch v-model:value="config.fit" /></n-form-item><n-form-item v-if="config.fit==true" label="填充颜色"><n-color-picker v-model:value="config.bgColor" :show-alpha="false" /></n-form-item><n-button block type="primary" secondary :loading @click="toSplit">开始切割</n-button></n-flex></n-form>
</template><script setup>const props = defineProps({img:{ type:Object }})const config = reactive({ height:1000, fit:true, bgColor:"#ffffff" })const loading = ref(false)const toSplit=()=>{if(config.height >= props.img.height) return M.warn(`切割高度不能大于图片原高度`)loading.value = trueH.action('split', props.img.path, toRaw(config)).then(v=>{loading.value = falselet { total, dir, used } = vM.dialog({maskClosable: false, showIcon: true,title: `切割完成`,content: `共生成 ${total} 张小图,耗时 ${used} 毫秒。`,positiveText:"打开图片文件夹",onPositiveClick: ()=>H.action('open', dir)})})}
</script>

🚀 界面实现

http://www.xdnf.cn/news/14422.html

相关文章:

  • 归一化:深度学习的隐藏加速器,解密数据标准化的魔力
  • Spring 事务传播行为全景分析表
  • Java设计模式之创建型模式( 工厂方法模式)介绍与说明
  • 智能跃迁:企业大模型落地方法论与路径最佳实践
  • 逆向知识点
  • 5.5.2_2并查集的进一步优化
  • 运算符与优先级
  • Docker环境下的EFK日志分析实践:从Filebeat采集到Kibana可视化的完整部署指南
  • 【LeetCode 207】课程表(有向无环图 DAG、拓扑排序)
  • 在C++中进程间通信(IPC)
  • 数据库学习(七)——MySQL执行引擎
  • Google机器学习实践指南(非线性特征工程解析)
  • 人工智能学习37-Keras手写识别预测
  • 对于数据库触发器自动执行的理解
  • Java类的继承
  • Luckfox Pico Pi RV1106学习<3>:支持IMX415摄像头
  • BeckHoff <---> Keyence (MD-X)激光 刻印机 Profient 通讯
  • Elasticsearch:什么是混合搜索?
  • AIGC 基础篇 高等数学篇 06 向量代数与空间解析几何
  • 人月神话-学习记录
  • SQL Developer 表复制
  • Python安装与使用教程
  • Maven在依赖管理工具方面的内容
  • Java多线程通信:wait/notify与sleep的深度剖析(时序图详解)
  • Spring是如何实现有代理对象的循环依赖
  • 【SQLAlchemy系列】 SQLAlchemy 中的多条件查询:or*与 in*操作符
  • 智能土木通 - 土木工程专业知识问答系统02-RAG检索模块搭建
  • AC耦合与DC耦合
  • 体验AI智能投资!AI Hedge Fund了解一下
  • Java可变参数方法的常见错误与最佳实践