将图像大小调整为一个分辨率

我有一堆图像,分辨率不同。 还有混合的风景和肖像照片。我需要将图像调整为一个分辨率(1024x768)。如果我有一张肖像照片,最大高度需要为768,而我的风景照片最大宽度必须为1024。 结束的空间必须是黑色的。 现在我使用
mogrify -resize 1024x768 -verbose *.jpg
我知道我可以使用1024x!768,但就像我说我正在使用不同类型的图片。 我的exif信息也不包含有关图片是否为横向的信息。     
已邀请:
我使用ImageMagick执行此类任务。安装后,您将拥有“转换”命令,这是非常常见的,并且可以轻松完成您的任务。     
您必须裁剪图像以获得相同的宽高比,然后您可以调整图像大小以获得所需的分辨率。使用nodejs的示例代码(imagemagick命令行工具):
var width = 166;
var height = 117;
var ratio_new = width/height;
var ratio_old = image_file.width_orig/image_file.height_orig;
var pixels_too_much = 0;
var geometry = '';

if (ratio_old > ratio_new)
{
    config.debug && console.log ("remove horizontal pixel!");
    pixels_too_much = parseInt(image_file.width_orig - (image_file.height_orig * ratio_new))-1;
    geometry = parseInt(image_file.height_orig * ratio_new + 0.5) + 'x' + image_file.height_orig;
    geometry += "+" + parseInt(pixels_too_much/2) + "+0!";
}
else if (ratio_old < ratio_new)
{
    config.debug && console.log ("remove vertikal pixel");
    pixels_too_much = parseInt(image_file.height_orig - (image_file.width_orig / ratio_new));
    geometry = image_file.width_orig + 'x' + (image_file.width_orig / ratio_new);
    geometry += "+0+" + parseInt(pixels_too_much/2)+"!";
}
im.convert([image_file.path, '-crop', geometry, '-resize', width + 'x' + height, thumb_path],function(){});
    

要回复问题请先登录注册