使用Python批量旋转,镜像图片

[复制链接]
查看1589 | 回复0 | 2023-8-23 12:15:58 | 显示全部楼层 |阅读模式
媒介

当我们举行大量图像处理时,常常需要旋转或镜像图像。但一张张处理图片费神又费力,有什么好的办法可以资助我们快速搞定这个问题呢?这时间我们可以写个简单的python程序来搞定它!~

在Python中,我们可以使用Pillow库(Python Imaging Library)举行图像处理。Pillow库提供了很多函数和类,使得图像旋转和镜像变得容易。

本文将会以一下三张图片为例,在每一部门将会简单先容函数后附上批量处理图片的代码,零底子的小同伴也可以轻松使用嗷~





〇、准备工作,PIL库安装

一、图像旋转

二、图片镜像


〇、准备工作,PIL库安装

可以通过以下命令在Python中安装Pillow库(它是Python Imaging Library的分支版本):
  1. pip install Pillow
复制代码
别的,还可以通过Anaconda或Miniconda等科学计算发行版举行安装。如果你使用Anaconda,可以使用以下命令来安装Pillow库:
  1. conda install pillow
复制代码
一、图像旋转

对于图像旋转,可以使用Pillow库中的rotate()函数,可以通过指定旋转角度来实现。详细来说,我们可以使用Image.rotate(angle)函数(其中angle为旋转角度,单元为度)。比方,我们想将图像旋转90度,可以如许做:
  1. from PIL import Image
  2. image = Image.open("path/to/image.jpg") # 读取图片,这里要注意图片的格式
  3. rotated_image = image.rotate(90, expand=True) #确定旋转角度
复制代码


在这个例子中,我们打开了一张图片,然后使用rotate()函数将其旋转了90度。我们可以使用show()函数来查察或是用.save(path)直接生存旋转后的图像:
  1. rotated_image.show() #查看图片
  2. rotated_image.save('rotated_image.jpg') #保存图片
复制代码

当我们渴望可以批量旋转一个文件夹里的图片90°180°270°时,我们可以这么做:

  1. from PIL import Image
  2. import os
  3. # 设置输入和输出目录
  4. input_dir = '/path/to/input/directory/'
  5. output_dir = '/path/to/output/directory/'
  6. # 循环输入目录中的所有JPEG图像
  7. for filename in os.listdir(input_dir):
  8.     #只可以处理jpg和jpeg后缀下的程序,如果你的图片是别的后缀,请直接更改点后的文件格式
  9.     if filename.endswith('.jpg') or filename.endswith('.jpeg'):
  10.         
  11.         # 打开图像
  12.         img = Image.open(os.path.join(input_dir, filename))
  13.         # 旋转图像并保存 这里可以直接添加或删除以选取你需要的旋转角度
  14.         for angle in [90, 180, 270]:
  15.             rotated_img = img.rotate(angle, expand=True)
  16.             rotated_img.save(os.path.join(output_dir, f'{angle}_{filename}'))
复制代码
制品如下~!








二、图片镜像

对于图像镜像,可以使用Pillow库中的transpose()函数,可以通过指定翻转模式来实现。详细来说,我们可以使用Image.transpose(mode)函数,其中mode为翻转模式,可以为Image.FLIP_LEFT_RIGHT(水平翻转)或Image.FLIP_TOP_BOTTOM(垂直翻转)。比方,如果我们想将图像水平翻转,可以如许做:
  1. from PIL import Image
  2. image = Image.open("path/to/image.jpg")
  3. mirrored_image = image.transpose(Image.FLIP_LEFT_RIGHT)
复制代码


在这个例子中,我们打开了一张图像,然后使用transpose()函数将其水平翻转。我们可以使用show()或是用.save(path)函数来查察或生存翻转后的图像:
  1. mirrored_image.show()
  2. mirrored_image.save('mirrored_image.jpg')
复制代码
当我们渴望可以批量水平和垂直镜像一个文件夹里的图片时,我们可以这么做:

  1. from PIL import Image
  2. import os
  3. # 设置输入和输出目录
  4. input_dir = '/path/to/input/directory/'
  5. output_dir = '/path/to/output/directory/'
  6. # 循环输入目录中的所有JPEG图像, 这里如果你是别的类型格式图片,直接更改点后图片类型即可
  7. for filename in os.listdir(input_dir):
  8.     if filename.endswith('.jpg') or filename.endswith('.jpeg'):
  9.         
  10.         # 打开图像
  11.         img = Image.open(os.path.join(input_dir, filename))
  12.         # 水平镜像并保存
  13.         mirrored_img = img.transpose(Image.FLIP_LEFT_RIGHT)
  14.         mirrored_img.save(os.path.join(output_dir, f'horiz_{filename}'))
  15.         # 垂直镜像并保存
  16.         mirrored_img = img.transpose(Image.FLIP_TOP_BOTTOM)
  17.         mirrored_img.save(os.path.join(output_dir, f'vert_{filename}'))
复制代码
制品如下~!






来源:https://blog.csdn.net/weixin_49030835/article/details/129103215
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则