opencv 如何从一个文件夹中水平或垂直翻译所有的jpeg图像,并使用python保存?[已关闭]

xzabzqsa  于 2023-01-13  发布在  Python
关注(0)|答案(1)|浏览(117)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

3天前关闭。
Improve this question
我有一个包含600张图片的文件夹。我想从一个文件夹中水平或垂直地翻译它们,并将它们保存在同一个文件夹中。我该怎么做呢?
我试图从一个文件夹中翻译图像,但找不到一种方法将所有图像翻译在一起并以相同的名称保存。

fnx2tebb

fnx2tebb1#

据我所知,我认为你只是想使用python一次性地水平或垂直翻转它们(图像),并以相同的名称保存它们--如果是这样的话,你可以通过

import os # just for getting name from file path
from glob import glob # read file in directories
from PIL import Image, ImageOps # reading & processing image
from tqdm import tqdm # loading bar (unnecessary but it help visualizing waiting time when process high amount of files)

horizon_or_vertical = 'horizon' # option to choose between horizon/vertical flip
for file_path in tqdm(glob('./folder/*.jpeg')): # using *.jpeg exclude other format Note: that tqdm can be removed
    file_name, ext = os.path.splitext(file_path) # get old file name by split the path
    with Image.open(file_path) as image: # read image
        if horizon_or_vertical == 'horizon':
            translated_image = ImageOps.mirror(image) # horizon flip
        else:
            translated_image = ImageOps.flip(image) # vertical flip

        image.save(file_name, "JPEG") # save in JPEG format with the same name in the same folder

相关问题