如何批量调整数百万个图像的大小以适合最大宽度和高度? [英] How to batch resize millions of images to fit a max width and height?

查看:78
本文介绍了如何批量调整数百万个图像的大小以适合最大宽度和高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来批量调整大约1500万个不同文件类型的图像的大小,以适应特定的边界框分辨率(在这种情况下,图像不能大于1024 * 1024),而不会扭曲图像并因此保留正确的宽高比.所有文件当前都位于我具有sudo访问权限的Linux服务器上,因此,如果我需要安装任何东西,我很好.

I'm looking for a way to batch-resize approximately 15 million images of different file types to fit a certain bounding box resolution (in this case the image(s) cannot be bigger than 1024*1024), without distorting the image and thus retaining the correct aspect ratio. All files are currently located on a Linux server on which I have sudo access, so if I need to install anything, I'm good to go.

尝试使用Windows下的某些工具(Adobe Photoshop和其他工具)后,我不再愿意在自己的计算机上运行此工具,因为这在渲染时实际上使它无法使用.考虑到这项工作的规模,我真的在寻找一些可以直接在Linux上运行的命令行魔术,但是到目前为止,我对ImageMagick的努力没有给我任何帮助,因为我除了错误之外什么也没有. 老实说,ImageMagick的文档可能需要做一些工作...或者应该有人努力制作一个良好的Web界面,以创建这些神话般的图像转换单线之一.

After dabbling around with some tools under Windows (Adobe Photoshop and other tools) I am no longer willing to run this on my own machine as this renders it virtually unusable when rendering. Considering the size of this job, I'm really looking for some command-line magic to directly run it on Linux, but my endeavors with ImageMagick so far haven't given me anything to work with as I'm getting nothing but errors. To be honest, ImageMagick's documentation could use some work... or someone should put in the effort to make a good web-interface to create one of these mythical image convertion one-liners.

我需要将图像调整为相同的文件名,并且将其格式调整为一定的最大尺寸,例如1024 * 1024,这意味着:

I need the images to be resized to the same filename and of a format which will fit inside a certain maximum dimension, for example 1024*1024, meaning:

  • 2048 * 1024的JPG变为质量为75%的JPG * 1024 * 512
  • 1024 * 2048的PNG变为512 * 1024的PNG

生成的图像不应包含其他透明像素来填充剩余的像素;我只是在寻找一种将图像转换为有限分辨率的方法.

The resulting image should contain no additional transparent pixels to fill up the remaining pixels; I'm just looking for a way to convert the images to a limited resolution.

感谢您的帮助!

推荐答案

我发现转换成百万个这样的图像的最佳方法是创建一个简单的bash脚本,该脚本开始转换它找到的所有图像,例如下面列出的图像:

The best way I found to convert millions of images like these is by creating a simple bash script which starts converting all the images it finds, like the one listed below:

要编辑此bash脚本,如果您没有nano,请使用nano:对于Ubuntu/Debian,使用"apt-get install nano";对于CentOS/CloudLinux,使用"yum install nano".对于其他发行版:使用Google),但是您可以随意使用所需的任何编辑器.

To edit this bash script, I use nano if you don't have nano: "apt-get install nano" for Ubuntu/Debian or "yum install nano" for CentOS/CloudLinux.. for other distributions: use Google) but you're free to use any editor you want.

首先,通过启动您喜欢的编辑器(我的nano)来创建bash脚本:

First, create the bash script by starting your favorite editor (mine's nano):

nano -w ~/imgconv.sh

然后用以下内容填充它:

Then fill it with this content:

#!/bin/bash
find ./ -type f -iname "*.jpeg" -exec mogrify -verbose -format jpeg -layers Dispose -resize 1024\>x1024\> -quality 75% {} +
find ./ -type f -iname "*.jpg" -exec mogrify -verbose -format jpg -layers Dispose -resize 1024\>x1024\> -quality 75% {} +
find ./ -type f -iname "*.png" -exec mogrify -verbose -format png -alpha on -layers Dispose -resize 1024\>x1024\> {} +

然后要做的就是使其可以通过chmod +x ~/imgconv.sh执行,并从要在其中调整所有子目录中图像大小的主图像目录中运行它:

Then all you need to do is make it executable with chmod +x ~/imgconv.sh and run it from the main images directory where you want to resize the images in all subdirectories:

cd /var/www/webshop.example.com/public_html/media/
~/imgconv.sh

那应该开始转换过程.

该脚本的工作方式是使用find查找具有任何大写字母的扩展名.jpeg的文件,然后运行命令:

The way the script works is that it uses find to find the file with extension .jpeg of any capitalization and then runs a command:

find ./ -type f -iname "*.jpeg" -exec <COMMAND> {} +

..然后使用"-exec {} +"参数执行适当的转换作业:

.. and then execute the appropriate convert job using the "-exec {} +" parameter:

mogrify -verbose -format jpeg -layers Dispose -resize 1024\>x1024\> -quality 75% <### the filename goes here, in this case *.jpeg ###>

如果您正在使用比今天更早的文件,并且想要防止重做今天已经转换过的文件,您甚至可以通过添加选项<来告诉'find'命令仅转换比今天更早的文件. c5>像这样:

If you're working with files older than today and you want to prevent re-doing files you've already converted today, you could even tell the 'find' command only convert the files older than today by adding the option -mtime +1 like so:

#!/bin/bash
find ./ -type f -mtime +1 -iname "*.jpeg" -exec mogrify -verbose -format jpeg -layers Dispose -resize 1024\>x1024\> -quality 75% {} +
find ./ -type f -mtime +1 -iname "*.jpg" -exec mogrify -verbose -format jpg -layers Dispose -resize 1024\>x1024\> -quality 75% {} +
find ./ -type f -mtime +1 -iname "*.png" -exec mogrify -verbose -format png -alpha on -layers Dispose -resize 1024\>x1024\> {} +

性能

使用更多内核执行此过程的一种非常简单的方法是,通过在每行之后添加一个&,将每个作业派生到后台.另一种方法是使用GNU Parallel,尤其是使用-X参数,因为它将使用您所有的CPU内核,并使工作更快地完成很多次.

Performance

A really simple way to use more cores to perform this process is to fork each job to the background by adding a & after each line. Another way would be to use GNU Parallel, especially with the -X parameter as it will use all your CPU cores and get the job done many times quicker.

但是,无论您要使用哪种并行化技术,请确保仅在自己的系统上执行此操作,而不要在生产平台所在的共享磁盘系统上执行此操作,因为要获得最佳性能将使您的硬件瘫痪或虚拟机监控程序性能.

But no matter what kind of parallelization technique you'll be using, be sure only to do that on your own system and not on a shared disk system where your production platform resides, since going for maximum performance will bog down your hardware or hypervisor performance.

此工作将花费一些时间,因此请确保事先设置没有超时/空转数据包的屏幕或终端.在我的系统上,它每分钟可以处理约5000个文件,因此整个工作应该花费不到50-60小时的时间...听起来像是在周末运行一项不错的工作.

This job is going to take a while, so be sure to set up a screen or a terminal without timeout/noop packets beforehand. On my system, it churned through about 5000 files per minute, so the entire job should take less than ~50-60 hours... sounds like a fine job to run over the weekend.

只要确保通过编写单独的命令将所有文件扩展名彼此分开即可;将所有选项叠加在一起并使用所有图像格式的所有选项进行迁移"将无法正常工作.

Just be sure to separate all file extensions from each other by writing separate commands; Piling all options on top of each other and having 'mogrify' using all options for all image formats won't work.

ImageMagick是右手的强大工具.

ImageMagick is a powerful tool in the right hands.

这篇关于如何批量调整数百万个图像的大小以适合最大宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆