图片旋转并保存图片

let degree = this.rotate;
if (degree === 0) {
this.$message({ message: ‘图片未旋转’, offset: 100 });
return;
}

  const img = new Image();
  // 获取图片src
  img.src = this.currentBigPhoto.img;
  // 获取图片的宽高
  const imgInfo = this.$refs['detail'].getBoundingClientRect();
  if (!imgInfo) {
    return;
  }
  const imgWidth = imgInfo.width;
  const imgHeight = imgInfo.height;
  const canvas = document.createElement('canvas');
  const ctx = canvas.getContext('2d');
  // 获取要旋转的角度
  // 旋转360度 0度
  if (degree === 0) {
    canvas.width = imgWidth;
    canvas.height = imgHeight;
    // ctx.rotate((0 * Math.PI) / 180);
    ctx.drawImage(img, 0, 0, imgWidth, imgHeight);
  } else if (degree === 90) {
    // 旋转90度
    canvas.height = imgHeight;
    canvas.width = imgWidth;
    ctx.rotate((90 * Math.PI) / 180);
    ctx.drawImage(img, 0, -imgWidth, imgHeight, imgWidth);
  } else if (degree === 180) {
    // 旋转180
    canvas.width = imgWidth;
    canvas.height = imgHeight;
    ctx.rotate((180 * Math.PI) / 180);
    ctx.drawImage(img, -imgWidth, -imgHeight, imgWidth, imgHeight);
  } else if (degree === 270) {
    // 旋转270
    canvas.width = imgWidth;
    canvas.height = imgHeight;
    ctx.rotate((270 * Math.PI) / 180);
    ctx.drawImage(img, -imgHeight, 0, imgHeight, imgWidth);
  }
  // 生成图片的url

  const newImg = canvas.toDataURL();
  // this.a = newImg;
  // 重置页面图片的旋转度数
  this.rotate = 0;
  // 新图替换当前图片
  this.currentBigPhoto.img = newImg;
  // 替换存储中的图片
  this.replaceImg(this.currentBigPhoto.id);
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Python的PIL库(Pillow库)来实现图片旋转保存。以下是一个示例代码: ```python from PIL import Image # 打开图片 image = Image.open('example.jpg') # 旋转图片 rotate_image = image.rotate(45) # 保存旋转后的图片 rotate_image.save('example_rotated.jpg') ``` 在这个示例中,我们打开了一个名为“example.jpg”的图片,将其旋转45度,然后将旋转后的图片保存为“example_rotated.jpg”。你可以根据需要更改旋转的角度和文件名。 ### 回答2: 要实现Python图片旋转保存,可以使用PIL库(Pillow)来处理图像。以下是一个简单的300字中文回答: ``` 首先,确保你已经在Python环境中安装了PIL库。可以使用以下命令进行安装: pip install pillow 接下来,导入所需的库: from PIL import Image 然后,使用open()函数打开要旋转的图像文件,例如: image = Image.open('image.jpg') 通过调用rotate()函数并传递角度参数,对图像进行旋转。例如,要将图像旋转90度: rotated_image = image.rotate(90) 你可以根据需要调整旋转的角度。 最后,使用save()函数将旋转后的图像保存到指定的文件中。例如: rotated_image.save('rotated_image.jpg') ``` 请注意,保存的文件名可以自行定义,并且需要与旋转后的图像格式相匹配。 以上是一个简单的旋转保存图像的Python代码。通过调整角度参数,你可以旋转图像到任意角度。如果你需要更复杂的操作,例如旋转时保持图像的尺寸不变,可以进一步研究PIL库的详细文档。 ### 回答3: Python中可以使用Pillow库进行图片旋转保存操作。下面是一个使用Pillow库进行图片旋转的示例代码: ```python from PIL import Image # 打开图片 image = Image.open('input.jpg') # 旋转图片 rotate_image = image.rotate(90) # 保存旋转后的图片 rotate_image.save('output.jpg') ``` 以上代码中,我们首先使用`Image.open()`函数打开待旋转图片,然后使用`rotate()`函数对图片进行旋转,传入旋转的角度作为参数,这里我们旋转了90度。最后使用`save()`函数保存旋转后的图片,传入保存的文件路径作为参数。在这个例子中,旋转后的图片保存为`output.jpg`。 需要注意的是,使用这段代码前需要确保你已经安装了Pillow库。你可以在Python的命令行中使用`pip install Pillow`命令来安装Pillow库。 如果你想要选择不同的旋转角度或者保存的文件名,只需要修改相应的参数即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值