为什么图片裁剪后尺寸会变大

探讨了在使用JPEG格式进行图片裁剪时,文件大小反而增加的现象。解释了这是由于JPEG的压缩特性,尤其是当裁剪偏移量为奇数时,会导致颜色通道重新采样,使得图像复杂度提高,进而降低压缩率。

原文地址:http://graphicdesign.stackexchange.com/questions/51631/why-does-a-jpegs-file-size-increase-after-cropping-and-saving-for-web-in-photosh


8
down voteaccepted

Although the question was asked about Adobe Photoshop, the behavior is due to the lossy JPEG format and would be similar with any image editor.

Cropping a JPEG can make it less compressible, especially when the x and y offsets of the cropped area are odd numbers. This causes a re-subsampling of the color channels that can make the cropped image more complex and thus less compressible.

Also if the cropping offsets are not divisible by 8, the 8x8 tiles used in JPEG compression are now made up of parts of several adjacent tiles from the original JPEG, and that can increase the image complexity.

There are other things that can come into play (addition of a color profile, etc.) but without access to your before-and-after images it's hard to tell what exactly happened. Most likely, though, it's the cropping offset effect described above.

Results would depend upon the nature of the image. I experimented with a 500x500-pixel checkerboard of 100x100-pixel squares, compressed with quality 50, then cropped to 490x490 pixels and recompressed with quality 50.

The resulting sizes are:

original:                     3968 bytes
cropped with 1-pixel offsets: 5445 bytes
cropped with 2-pixel offsets: 5279 bytes
cropped with 3-pixel offsets: 5481 bytes
cropped with 4-pixel offsets: 4426 bytes
cropped with 8-pixel offsets: 3917 bytes

For a photograph, on the other hand, results are more in line with intuition, the cropped photo having a slightly smaller file size than the original:

comressed with quality 50:    57413 bytes
cropped with 3-pixel offsets: 56576 bytes
cropped with 4-pixel offsets: 56170 bytes

I used ImageMagick, not Photoshop, for these experiments, but I'm sure that Photoshop would exhibit similar behavior for these tests.


提供的引用内容中未提及uniapp裁剪图片变大原因及解决办法。不过,从常见的技术角度分析,uniapp裁剪图片变大可能有以下原因及对应的解决方法: ### 原因 - **编码格式与质量**:裁剪后采用了更高质量的编码格式或者默认的编码质量较高,导致文件体积增大。例如,从JPEG的低质量转为高质量,或者采用了无损的PNG格式。 - **元数据增加**:裁剪过程中可能添加了额外的元数据,如拍摄设备信息、地理位置等,从而使文件大小增加。 - **算法问题**:裁剪算法可能在处理图片时进行了不必要的填充或调整,导致图片尺寸或数据量增大。 ### 解决方法 - **调整编码质量**:在保存裁剪后的图片时,选择合适的编码格式和质量参数。对于JPEG格式,可以适当降低质量参数以减小文件大小。以下是使用`uni.canvasToTempFilePath`保存图片并设置质量的示例代码: ```javascript uni.canvasToTempFilePath({ canvasId: 'myCanvas', fileType: 'jpg', quality: 0.8, // 设置图片质量,范围0 - 1 success: function (res) { console.log('裁剪图片路径:', res.tempFilePath); }, fail: function (err) { console.error('保存图片失败:', err); } }); ``` - **去除元数据**:在保存图片前,使用相关工具去除不必要的元数据。可以使用第三方库如`exif-js`来处理图片的元数据。 ```javascript import EXIF from 'exif-js'; // 获取图片元素 const img = document.getElementById('myImage'); // 读取图片元数据 EXIF.getData(img, function () { // 去除元数据 const newImg = new Image(); newImg.src = img.src; // 后续处理新图片 }); ``` - **优化裁剪算法**:检查裁剪代码,确保算法不会进行不必要的填充或调整。可以参考官方文档或使用成熟的裁剪库来优化裁剪过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值