Python3 图片水平镜像实现

 1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Sun Feb  4 12:15:38 2018
 4 
 5 @author: markli
 6 """
 7 from PIL import Image;
 8 import os;
 9 
10 class Mirror:
11     def __init__(self):
12         #self.path = path;
13         self.formats = ['.png','.jpg','.jpeg','.bmp','.gif'];
14     
15     def ImageMirror(self,fp,savepath):
16         filepath = fp;
17         f_format = '';
18         if(os.path.exists(filepath) == False):
19             print("路径不存在");
20             return;
21         f_format = os.path.splitext(filepath)[1].lower();
22         if(f_format not in self.formats):
23             print("图片格式不正确");
24             return;
25             
26         img = Image.open(filepath);
27         img_pixel = img.load();
28         mirror = Image.new(img.mode,img.size,"white");
29         
30         width, height = img.size;
31         """水平镜像转换,遍历每个像素点,将后列变前列"""
32         for y in range(height):
33             for x in range(width):
34                 pixel = img_pixel[width-1-x,y];
35                 mirror.putpixel((x,y),pixel);
36         
37         sp,f = os.path.splitext(savepath);
38         if(f != f_format):
39             savepath = sp + f_format;
40         mirror.save(savepath);
41     
42     def TranslateAll(self,filedir,savedir):
43         """
44         将目标图片集全部进行镜像处理
45         filedir 目标图片集所在的文件夹路径
46         savedir 镜像图片保存的文件夹路径
47         """  
48         filelist = self.Getfile(filedir);
49         if(os.path.exists(savedir) == True):
50             print("保存路径已存在,请重新设定路径");
51             return;
52         os.mkdir(savedir);
53         for f in filelist:
54             fn,ext = os.path.splitext(os.path.split(f)[1]);
55             fn = fn + "mirror"; #给定镜像图片的名称
56             filename = fn + ext; #镜像图片与原图具有相同的扩展名
57             savefile = os.path.join(savedir,filename); #构造出完整的保存路径
58             self.ImageMirror(f,savefile);
59     
60     def Getfile(self,filedir):
61         """获得文件夹filedir目录下所有的文件路径"""
62         filepath = [];
63         if(os.path.exists(filedir) == False):
64             print("路径不存在");
65             return filepath;
66         if(os.path.isdir(filedir) == False):
67             print("该路径不是文件夹");
68             return filepath;
69         filelist = os.listdir(filedir);
70         
71         for f in filelist:
72             f = os.path.join(filedir,f);
73             if(os.path.isfile(f) == True):
74                 filepath.append(f);
75             elif(os.path.isdir(f) == True):
76                 filepath.extend(self.Getfile(f));
77             else:
78                 continue;
79         return filepath;
80 
81 #fp = "C:\\Users\\yangp\\Desktop\\python_b_blue.jpg";
82 #m = Mirror();
83 #savep = "C:\\Users\\yangp\\Desktop\\python_b_blue_m.jpg";
84 #m.ImageMirror(fp,savep);
85         
86 filedir = "C:\\Users\\yangp\\Desktop\\mirror";
87 savedir = "C:\\Users\\yangp\\Desktop\\mirror2";
88 m = Mirror();
89 m.TranslateAll(filedir,savedir);

 

转载于:https://www.cnblogs.com/FightLi/p/8413375.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值