简体   繁体   English

调整文件夹中多个图像的大小 (Python)

[英]Resize multiple images in a folder (Python)

I already saw the examples suggested but some of them don't work.我已经看到了建议的示例,但其中一些不起作用。

So, I have this code which seems to work fine for one image:所以,我有这个代码似乎对一张图像工作正常:

im = Image.open('C:\\Users\\User\\Desktop\\Images\\2.jpg') # image extension *.png,*.jpg
new_width  = 1200
new_height = 750
im = im.resize((new_width, new_height), Image.ANTIALIAS)
im.save('C:\\Users\\User\\Desktop\\resized.tif') # .jpg is deprecated and raise error....

How can I iterate it and resize more than one image ?如何迭代它并调整多个图像的大小? Aspect ration need to be maintained.需要保持纵横比。

Thank you谢谢

# Resize all images in a directory to half the size.
#
# Save on a new file with the same name but with "small_" prefix
# on high quality jpeg format.
#
# If the script is in /images/ and the files are in /images/2012-1-1-pics
# call with: python resize.py 2012-1-1-pics

import Image
import os
import sys

directory = sys.argv[1]

for file_name in os.listdir(directory):
print("Processing %s" % file_name)
image = Image.open(os.path.join(directory, file_name))

x,y = image.size
new_dimensions = (x/2, y/2) #dimension set here
output = image.resize(new_dimensions, Image.ANTIALIAS)

output_file_name = os.path.join(directory, "small_" + file_name)
output.save(output_file_name, "JPEG", quality = 95)

print("All done")

Where it says它说的地方

new_dimensions = (x/2, y/2)

You can set any dimension value you want for example, if you want 300x300, then change the code like the code line below你可以设置你想要的任何尺寸值,例如,如果你想要 300x300,那么像下面的代码行一样更改代码

new_dimensions = (300, 300)

I assume that you want to iterate over images in a specific folder.我假设您想遍历特定文件夹中的图像。

You can do this:你可以这样做:

import os
from datetime import datetime

for image_file_name in os.listdir('C:\\Users\\User\\Desktop\\Images\\'):
    if image_file_name.endswith(".tif"):
        now = datetime.now().strftime('%Y%m%d-%H%M%S-%f')

        im = Image.open('C:\\Users\\User\\Desktop\\Images\\'+image_file_name)
        new_width  = 1282
        new_height = 797
        im = im.resize((new_width, new_height), Image.ANTIALIAS)
        im.save('C:\\Users\\User\\Desktop\\test_resize\\resized' + now + '.tif')

datetime.now() is just added to make the image names unique. datetime.now()只是为了使图像名称唯一。 It is just a hack that came to my mind first.这只是我首先想到的一个黑客。 You can do something else.你可以做点别的。 This is needed in order not to override each other.这是必要的,以免相互覆盖。

I assume that you have a list of images in some folder and you to resize all of them我假设您在某个文件夹中有一个图像列表,并且您可以调整所有图像的大小

from PIL import Image
import os

    source_folder = 'path/to/where/your/images/are/located/'
    destination_folder = 'path/to/where/you/want/to/save/your/images/after/resizing/'
    directory = os.listdir(source_folder)
    
    for item in directory:
        img = Image.open(source_folder + item)
        imgResize = img.resize((new_image_width, new_image_height), Image.ANTIALIAS)
        imgResize.save(destination_folder + item[:-4] +'.tif', quality = 90)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM