将 256x256 图像调整为更大的尺寸 PIL

时间:2021-05-14 01:21:24

标签: python python-imaging-library

我想批量调整原始尺寸为 256x256 的图像。现在我希望它更大,尺寸为 400x400。

这是我的代码:

<块引用>
from PIL import Image
import pathlib

maxsize = (400, 400)
input_dir = "directory_string_here"
path_list = pathlib.Path(input_dir).rglob('*.jpg')

for path in path_list:
    output_dir = str(path).replace('input','output')
    
    with Image.open(path) as img:
       img.resize(maxsize)
       img.save(output_dir, dpi=(300,300))
       print(str(path) + ":     done")

现在,当我运行代码时,图像的尺寸不是 400x400,但它们仍然是原始的 256x256 尺寸。我尝试将 maxsize 设置为 (200, 200) 并将图像大小调整为 200x200。有什么办法可以达到这个 400x400 的尺寸吗?

1 个答案:

答案 0 :(得分:0)

将图像调整为更高尺寸的一种方法是通过插值。您可以使用 OpenCV 库通过以下代码执行此操作。

dim = (400, 400)
resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)