# -*- coding: utf-8 -*-
"""
Created on Sun Feb 4 12:15:38 2018 @author: markli
"""
from PIL import Image;
import os; class Mirror:
def __init__(self):
#self.path = path;
self.formats = ['.png','.jpg','.jpeg','.bmp','.gif']; def ImageMirror(self,fp,savepath):
filepath = fp;
f_format = '';
if(os.path.exists(filepath) == False):
print("路径不存在");
return;
f_format = os.path.splitext(filepath)[1].lower();
if(f_format not in self.formats):
print("图片格式不正确");
return; img = Image.open(filepath);
img_pixel = img.load();
mirror = Image.new(img.mode,img.size,"white"); width, height = img.size;
"""水平镜像转换,遍历每个像素点,将后列变前列"""
for y in range(height):
for x in range(width):
pixel = img_pixel[width-1-x,y];
mirror.putpixel((x,y),pixel); sp,f = os.path.splitext(savepath);
if(f != f_format):
savepath = sp + f_format;
mirror.save(savepath); def TranslateAll(self,filedir,savedir):
"""
将目标图片集全部进行镜像处理
filedir 目标图片集所在的文件夹路径
savedir 镜像图片保存的文件夹路径
"""
filelist = self.Getfile(filedir);
if(os.path.exists(savedir) == True):
print("保存路径已存在,请重新设定路径");
return;
os.mkdir(savedir);
for f in filelist:
fn,ext = os.path.splitext(os.path.split(f)[1]);
fn = fn + "mirror"; #给定镜像图片的名称
filename = fn + ext; #镜像图片与原图具有相同的扩展名
savefile = os.path.join(savedir,filename); #构造出完整的保存路径
self.ImageMirror(f,savefile); def Getfile(self,filedir):
"""获得文件夹filedir目录下所有的文件路径"""
filepath = [];
if(os.path.exists(filedir) == False):
print("路径不存在");
return filepath;
if(os.path.isdir(filedir) == False):
print("该路径不是文件夹");
return filepath;
filelist = os.listdir(filedir); for f in filelist:
f = os.path.join(filedir,f);
if(os.path.isfile(f) == True):
filepath.append(f);
elif(os.path.isdir(f) == True):
filepath.extend(self.Getfile(f));
else:
continue;
return filepath; #fp = "C:\\Users\\yangp\\Desktop\\python_b_blue.jpg";
#m = Mirror();
#savep = "C:\\Users\\yangp\\Desktop\\python_b_blue_m.jpg";
#m.ImageMirror(fp,savep); filedir = "C:\\Users\\yangp\\Desktop\\mirror";
savedir = "C:\\Users\\yangp\\Desktop\\mirror2";
m = Mirror();
m.TranslateAll(filedir,savedir);

Python3 图片水平镜像实现的更多相关文章

  1. CSS制作图片水平垂直居中

    所谓的图片水平垂直居中就是把图片放在一个容器元素中(容器大于图片尺寸或是指定了大小的容器),并且图片位居此容器正中间(中间是指元素容器的正中间),而图片不是以背景图片(background-image ...

  2. DIV里面的图片水平与垂直居中的方法

    <div class=“box”> <img /> </div> 1.水平居中: 1)box设置  text-align:center ;    text-alig ...

  3. DIV或者DIV里面的图片水平与垂直居中的方法

    <div class=“box”> <img /> </div> 水平居中的常用方式: text-align:center ——这可以实现子元素字体,图片的水平居中 ...

  4. [iOS] UICollectionView实现图片水平滚动

    最新更新: 简单封装了一下代码,参考新文章:UICollectionView实现图片水平滚动 先简单看一下效果: 新博客:http://wossoneri.github.io 准备数据 首先先加入一些 ...

  5. 图片水平垂直居中(兼容IE6,IE7,firefox,opera,safari,其中图片可以是任何块元素)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. css实现图片水平垂直居中

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 使用Recyclerview实现图片水平自动循环滚动

    简介: 本篇博客主要介绍的是如何使用RecyclerView实现图片水平方向自动循环(跑马灯效果) 效果图: 思路: 1.准备m张图片 1.使用Recyclerview实现,返回无数个(实际Inter ...

  8. Win8Metro(C#)数字图像处理--2.19图像水平镜像

    原文:Win8Metro(C#)数字图像处理--2.19图像水平镜像  [函数名称] 图像水平镜像函数MirrorXProcess(WriteableBitmap src) [函数代码]      ...

  9. Python3图片处理头像

    一. 简介: Python3图片处理头像右上角类似QQ,微信右上角未读信息效果,其实没有实质作用,只是简单练习. 1. 环境: python3.5 random模块:生成随机数 PIL模块:图像处理模 ...

随机推荐

  1. oracle修改日期格式

    查看默认的日期格式 select * from v$nls_parameters; 更改 alter session | system (范围) set xxxx=“yyyy-mm-dd” ;

  2. C++ explicit 关键字

    原文转自:http://www.cnblogs.com/ymy124/p/3632634.html 首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造 ...

  3. 非常干货之Python资源大全

    非常干货之Python资源大全

  4. DDR3基本知识及测试【转】

    转自:http://blog.csdn.net/myarrow/article/details/7847385 一.DDR3简介 DDR3(double-data-rate three synchro ...

  5. 【译】.NET Core 2.2 Preview 2 发布

    原文出自.Net Blog Announcing .NET Core 2.2 Preview 2 今天,我们宣布推出.NET Core 2.2 Preview 2.我们有很多重要改进要和你分享,而且我 ...

  6. day1 diff命令递归比较目录下的文件

    一.diff实战 (1)递归比较文件夹下所有的文件及目录的不同 diff --brief -Nr dir1/ dir2/                               Reference ...

  7. SQL表链接

  8. 1、Appium安装

    1.安装node.js 访问node js官网 https://nodejs.org/en/ 下载并安装node js,找到你系统适合的node js一步步安装即可 2.安装Appium 在cmd中执 ...

  9. Mac下brew安装与配置mysql

    一.打开mac控制台 $ brew install mysql 二.启动mysql服务 $ mysql.server start 三.初始化mysql配置 1 rainMacBook-Pro:~ co ...

  10. Webpack devServer中的 proxy 实现跨域

    Webpack dev server使用http-proxy解决跨域问题 文档资料 webpack关于webpack-dev-server开启proxy的官方介绍Vue-cli proxyTable ...