python 调整目录中的图像大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1048658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:21:49  来源:igfitidea点击:

Resize images in directory

pythonperlimageresizeimage-scaling

提问by RailsSon

I have a directory full of images that I would like to resize to around 60% of their original size.

我有一个充满图像的目录,我想将其调整为原始大小的 60% 左右。

How would I go about doing this? Can be in either Python or Perl

我该怎么做呢?可以使用 Python 或 Perl

Cheers

干杯

Eef

埃夫

回答by Anurag Uniyal

If you want to do it programatically, which I assume is the case, use PIL to resize e.g.

如果您想以编程方式执行此操作,我认为是这种情况,请使用 PIL 调整大小,例如

newIm = im.resize((newW, newH)

then save it to same file or a new location.

然后将其保存到同一文件或新位置。

Go through the folder recursively and apply resize function to all images.

递归遍历文件夹并将调整大小功能应用于所有图像。

I have come up with a sample script which I think will work for you. You can improve on it: Maybe make it graphical, add more options e.g. same extension or may be all png, resize sampling linear/bilinear etc

我想出了一个我认为对你有用的示例脚本。你可以改进它:也许使它图形化,添加更多选项,例如相同的扩展名,或者可能都是 png,调整采样线性/双线性等

import os
import sys
from PIL import Image

def resize(folder, fileName, factor):
    filePath = os.path.join(folder, fileName)
    im = Image.open(filePath)
    w, h  = im.size
    newIm = im.resize((int(w*factor), int(h*factor)))
    # i am saving a copy, you can overrider orginal, or save to other folder
    newIm.save(filePath+"copy.png")

def bulkResize(imageFolder, factor):
    imgExts = ["png", "bmp", "jpg"]
    for path, dirs, files in os.walk(imageFolder):
        for fileName in files:
            ext = fileName[-3:].lower()
            if ext not in imgExts:
                continue

            resize(path, fileName, factor)

if __name__ == "__main__":
    imageFolder=sys.argv[1] # first arg is path to image folder
    resizeFactor=float(sys.argv[2])/100.0# 2nd is resize in %
    bulkResize(imageFolder, resizeFactor)

回答by mirod

How about using mogrify, part of ImageMagick? If you really need to control this from Perl, then you could use Image::Magick, Image::Resizeor Imager.

如何使用 Mogrify,ImageMagick 的一部分?如果你真的需要从 Perl 控制它,那么你可以使用Image::MagickImage::ResizeImager

回答by mirod

Can it be in shell?

它可以在外壳中吗?

mkdir resized
for a in *.jpg; do convert "$a" -resize 60% resized/"$a"; done

If you have > 1 core, you can do it like this:

如果你有 > 1 个内核,你可以这样做:

find . -maxdepth 1 -type f -name '*.jpg' -print0 | xargs -0 -P3 -I XXX convert XXX -resize 60% resized/XXX

-P3 means that you want to resize up to 3 images at the same time (parallelization).

-P3 意味着您要同时调整最多 3 个图像的大小(并行化)。

If you don't need to keep originals you can use mogrify, but I prefer to use convert, and then rm ...; mv ... - just to be on safe side if resizing would (for whatever reason) fail.

如果你不需要保留原件,你可以使用 mogrify,但我更喜欢使用 convert,然后 rm ...; mv ... - 只是为了安全起见,如果调整大小会(无论出于何种原因)失败。

回答by UncleZeiv

Use PerlMagick, it's an interface to the popular ImageMagicksuite of command line tools to do just this kind of stuff. PythonMagicis available as well.

使用PerlMagick,它是流行的ImageMagick命令行工具套件的接口,可以执行此类操作。PythonMagic也可用。

回答by Micha? Niklas

I use Python with PIL (Python Image Library). Of course there are specialized programs to do this.

我将 Python 与 PIL(Python 图像库)一起使用。当然,有专门的程序可以做到这一点。

Many people use PIL to such things. Look at: Quick image resizing with python

许多人使用 PIL 来做这样的事情。看:使用python快速调整图像大小

PIL is very powerful and recently I have found this recipe: Putting watermark to images in batch

PIL 非常强大,最近我发现了这个秘诀: 批量给图像添加水印

回答by Ganesh R.

do you need to just resize it or you want to resize programmatically? If just resize use PixResizer. http://bluefive.pair.com/pixresizer.htm

您需要调整它的大小还是想以编程方式调整大小?如果只是调整大小,请使用 PixResizer。http://bluefive.pair.com/pixresizer.htm