调整目录中图像的大小

发布于 2024-07-26 07:59:59 字数 108 浏览 6 评论 0原文

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

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

Cheers

Eef

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

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

Cheers

Eef

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

两相知 2024-08-02 07:59:59

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

newIm = im.resize((newW, newH)

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

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

我想出了一个示例脚本,我认为它适合您。 您可以改进它:也许使其图形化,添加更多选项,例如相同的扩展名或可能全部为 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)

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

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

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)
安稳善良 2024-08-02 07:59:59

使用 ImageMagick 的一部分 mogrify 怎么样? 如果你确实需要从 Perl 控制它,那么你可以使用 Image::MagickImage::Resize成像器

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

简单 2024-08-02 07:59:59

可以在壳里吗?

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

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

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

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

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

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:

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).

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.

岁月如刀 2024-08-02 07:59:59

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

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

久光 2024-08-02 07:59:59

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

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

PIL很强大,最近我发现了这个菜谱:
批量给图片添加水印

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

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

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

伊面 2024-08-02 07:59:59

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文