位图(Bitmap)是由像素组成的图像格式,每个像素包含颜色信息。调整位图大小是指改变图像的像素尺寸,这会直接影响图像的质量和文件大小。
from PIL import Image
def resize_image(input_path, output_path, width, height):
with Image.open(input_path) as img:
# 保持宽高比的调整
img.thumbnail((width, height))
# 或者强制调整到指定尺寸
# resized_img = img.resize((width, height))
img.save(output_path)
# 使用示例
resize_image('input.jpg', 'output.jpg', 800, 600)
function resizeImage(file, maxWidth, maxHeight, callback) {
const reader = new FileReader();
reader.onload = function(e) {
const img = new Image();
img.onload = function() {
const canvas = document.createElement('canvas');
let width = img.width;
let height = img.height;
// 保持比例调整
if (width > maxWidth) {
height *= maxWidth / width;
width = maxWidth;
}
if (height > maxHeight) {
width *= maxHeight / height;
height = maxHeight;
}
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, width, height);
callback(canvas.toDataURL('image/jpeg'));
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
问题1:调整后图像模糊
问题2:文件大小过大
问题3:调整后出现锯齿
问题4:色彩失真
通过理解这些概念和方法,您可以更有效地调整位图大小,满足不同场景的需求。
没有搜到相关的文章