在线长图片自动裁剪工具

介绍

一个可将图片自动分段裁剪成多张图片的工具。

举个栗子

在线地址

Github站点:https://hu243285237.github.io/PictureCutter_Web/

国内站点: http://hu243285237.gitee.io/picturecutter_web

项目地址

https://hu243285237.github.io/PictureCutter_Web/

兼容性

请在 PC 端使用 Chrome、Firefox、Edge 等浏览器,不支持 IE 浏览器。

原理

主要使用了 Canvas Context 的 drawImage 方法,对图片进行分段绘制。

图片选取、分段绘制和导出都在本地浏览器执行,无额外请求资源消耗。

主要代码

/**
 * 以像素裁剪图片
 * @param imgURL 图片
 * @param pixelWidth 像素宽度
 * @param pixelHeight 像素高度
 * @param format 裁剪后的图片格式
 * @returns 裁剪后的图片数组
 */
export function pixelCut(
  imgURL: string,
  pixelWidth: number,
  pixelHeight: number,
  format: string
): Array<string> {
  const res = new Array<string>();
  const img = new Image();
  img.src = imgURL;
  const amountRow = Math.ceil(img.width / pixelWidth);
  const amountCol = Math.ceil(img.height / pixelHeight);
  for (let i = 0; i < amountCol; i++) {
    for (let j = 0; j < amountRow; j++) {
      const canvas = document.createElement('canvas');
      [canvas.width, canvas.height] = [pixelWidth, pixelHeight];
      const context = canvas.getContext('2d');
      context?.drawImage(
        img,
        pixelWidth * j,
        pixelHeight * i,
        pixelWidth,
        pixelHeight,
        0,
        0,
        pixelWidth,
        pixelHeight
      );
      res.push(canvas.toDataURL('image/' + format, 1.0));
    }
  }
  return res;
}

/**
 * 以数量均等裁剪
 * @param imgURL 图片
 * @param amountRow 横向切割成几份
 * @param amountCol 纵向切割成几份
 * @param format 裁剪后的图片格式
 * @returns 裁剪后的图片数组
 */
export function amountCut(
  imgURL: string,
  amountRow: number,
  amountCol: number,
  format: string
): Array<string> {
  const res = new Array<string>();
  const img = new Image();
  img.src = imgURL;
  const pixelWidth = img.width / amountRow;
  const pixelHeight = img.height / amountCol;
  for (let i = 0; i < amountCol; i++) {
    for (let j = 0; j < amountRow; j++) {
      const canvas = document.createElement('canvas');
      [canvas.width, canvas.height] = [pixelWidth, pixelHeight];
      const context = canvas.getContext('2d');
      context?.drawImage(
        img,
        pixelWidth * j,
        pixelHeight * i,
        pixelWidth,
        pixelHeight,
        0,
        0,
        pixelWidth,
        pixelHeight
      );
      res.push(canvas.toDataURL('image/' + format, 1.0));
    }
  }
  return res;
}

  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
在Excel VBA中,可以使用以下代码来自动压缩、删除、裁剪图片: 首先,如果要进行图片操作,需要在VBA编辑器中启用Microsoft PowerPoint和Microsoft PowerPoint对象库。在VBA编辑器中,点击"工具",然后选择"引用",勾选上"Microsoft PowerPoint"和"Microsoft PowerPoint对象库",然后点击"确定"。 下面是一个示例代码,将其放置在一个按钮的点击事件中: ``` Sub AutoCompressDeleteCrop() Dim picture As Shape Dim slide As Slide Dim pptApp As PowerPoint.Application Dim pptPres As PowerPoint.Presentation ' 创建一个PowerPoint应用程序对象 Set pptApp = New PowerPoint.Application ' 创建一个新的演示文稿 Set pptPres = pptApp.Presentations.Add ' 循环处理活动工作表中的所有图片 For Each picture In ActiveSheet.Shapes ' 将图片复制到PowerPoint演示文稿 Set slide = pptPres.Slides.Add(pptPres.Slides.Count + 1, ppLayoutBlank) picture.Copy slide.Shapes.Paste ' 压缩图片 slide.Shapes(slide.Shapes.Count).PictureFormat.Compression = 80 ' 删除原始图片 picture.Delete ' 裁剪图片 slide.Shapes(slide.Shapes.Count).PictureFormat.CropLeft = 10 slide.Shapes(slide.Shapes.Count).PictureFormat.CropTop = 10 slide.Shapes(slide.Shapes.Count).PictureFormat.CropRight = 10 slide.Shapes(slide.Shapes.Count).PictureFormat.CropBottom = 10 Next picture ' 显示PowerPoint应用程序窗口 pptApp.Visible = True ' 清空对象 Set pptApp = Nothing Set pptPres = Nothing End Sub ``` 这段代码将从活动工作表中的每个图片进行以下操作: 1. 将图片复制到一个新的PowerPoint演示文稿中。 2. 压缩图片的质量。 3. 删除原始的Excel图片。 4. 对图片进行裁剪。 请注意,这段代码假设只有图片对象在工作表中。如果工作表中还有其他对象,可能会引发错误。如果遇到此问题,您可以使用条件语句或循环语句来检查每个对象的类型,在必要时跳过非图片对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值