如何通过终端调整图像大小?

我有Ubuntu 10.04。我通过终端使用以下命令转换了图像:
convert myfigure.png myfigure.jpg

但是我想调整转换后图像的高度和宽度。有没有办法做到这一点?
4个回答

同样的命令,加上一个额外的选项:

convert myfigure.png -resize 200x100 myfigure.jpg

或者

convert -resize 50% myfigure.png myfigure.jpg

要调整多个文件的大小,您可以尝试以下命令(如@test30所建议):
find . -maxdepth 1 -iname "*.jpg" | xargs -L1 -I{} convert -resize 30% "{}" _resized/"{}"

17你也可以使用百分比,例如 convert -resize 50% myfigure.png myfigure.jpg - January
@一月太棒了 :) - Rinzwind
如果我想将文件夹中的所有图片转换,最好的命令是什么?convert -resize 50% *.JPG 这个命令可以吗?谢谢。 - Ehsan M. Kermani
7将当前目录中的所有图像缩小50% find -maxdepth 1 . -iname "*.jpg" | xargs -l -i convert -resize 50% {} /tmp/{} 来源:https://www.perturb.org/display/632_ImageMagick_resize_images.html 我添加了maxdepth :) - test30
你在脚本中使用的字符 × 是 ASCII 字符 0xD7(乘号);代码需要将其替换为实际的 x 才能正常运行。请参考 StackOverflow: Invalid argument for option '-resize' in shell script - IQAndreas
抱歉,我试图编辑答案来修复这个问题,但是编辑至少需要改变6个字符。 - IQAndreas
@IQAndreas 谢谢并已编辑 :) (下次试着加上5个空格 :D) - Rinzwind
如果你是开发者并且安装了XCode和/或ImageMagick,那么很可能可以在OSX上运行。 - Seth Bro
5发现要处理所有的图片,必须将“.”放在“-maxdepth”之前,所以应该是这样的:find . -maxdepth 1 -iname "*.JPG" | xargs -l -i convert -resize 25% {} email/{} - Andrew Stern
另请参阅:https://guides.wp-bullet.com/batch-resize-images-using-linux-command-line-and-imagemagick/ - Gabriel Staples
使用sudo apt install imagemagick安装convert - user677955
你也可以使用循环来转换所有的图片,for i in *.jpg; do convert $i -resize 宽度x高度 $i; done; - ahmed galal
for f in *.jpg; do echo "正在处理文件 $f.."; convert -resize 25% $f small$f; done - Paloha
嗯,这样做并不起作用。图片的比例仍然保持不变。例如,将4x1转换为2x2是行不通的。 - foki
为了让它在我的Ubuntu系统下正常工作,我不得不改变xargs的参数顺序,像这样:find . -maxdepth 1 -iname "*.png" | xargs -L1 -I{} convert "{}" -resize 30% ./small/"{}" - Alex

如果你只想要命令行界面(CLI):
sudo apt-get install imagemagick
mogrify -resize 320x240 Image.png 
mogrify -resize 50% Image.png
mogrify -resize 320x240 *.jpg

如果您想尝试GUI:
安装nautilus-image-converter
sudo apt-get install nautilus-image-converter

它在Nautlius中添加了两个上下文菜单项,这样您就可以右键点击并选择“调整图像”(另一个是“旋转图像”)。
如果您愿意,您甚至可以一次处理整个目录中的所有图像,而无需打开任何应用程序。

1有没有比这个更强大的命令行软件? 它经常崩溃,并且只能处理大约80%的图像。 - Luka

imgp是一个相对较新的工具,可以进行图像调整和旋转。它比Nautilus图像转换器功能更多。

例如:

imgp -x 1366x768 *

由於Ubuntu內建了Python,您也可以使用Python腳本來實現這一點,並對所發生的情況有更多控制 - 可以參考這個stackoverflow問題中的示例腳本。這些示例僅使用標準庫。

腳本#1

import os, sys
import Image

size = 128, 128

for infile in sys.argv[1:]:
    outfile = os.path.splitext(infile)[0] + ".thumbnail"
    if infile != outfile:
        try:
            im = Image.open(infile)
            im.thumbnail(size, Image.ANTIALIAS)
            im.save(outfile, "JPEG")
        except IOError:
            print "cannot create thumbnail for '%s'" % infile

另一个例子是只需指定宽度(作为宽度变量)的情况:
from PIL import Image
import sys

filename = sys.argv[1:]
basewidth = 300
img = Image.open(filename)
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
img.save(filename) 

现在,如何通过终端完成这个操作...

sudo nano resizescript.py

将其中一个代码块粘贴到文本编辑器中。按Ctrl+x退出(保存更改时选择是)。
使用脚本#1的方法:
python resizescript.py yourfilenamehere.jpg

使用脚本#2:

python resizescript.py yourfilenamehere.jpg

你必须与这两个脚本的图片文件位于同一个目录中。第一个脚本将图像缩小为128x128像素。第二个脚本将其调整为300像素宽,并计算相应的高度。这更多是一个Python的解决方案,但从技术上讲,它完全通过终端完成。

请提供示例。 - Zanna
请按照问题要求通过终端提供示例。 - kenorb
谢谢,我在第一个回答中做了太多的假设。我已经根据问题进行了修改。 - freeworld
请注意,PIL仅适用于Python 2;对于3.x版本的支持计划在稍后进行,具体信息请参考http://www.pythonware.com/products/pil/。 - arp
对于Python 3,有一个叫做Pillow的库,它是PIL的一个分支。令人困惑的是,它在Debian软件源中以python3-pil的包名存在。除此之外,你还可以通过pip3软件包管理器来获取。 - Sergiy Kolodyazhnyy