# -*- 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. Apple Watch 开发详解

    Apple Watch 开发详解 Apple Watch现在对于第三方开发者来说更多的还是一块额外的屏幕.暂时WatchKit没有能给出足够的接口.现在Watch App的主要运算逻辑需要依赖iPho ...

  2. PHP - CentOS下开发运行环境搭建(Apache+PHP+MySQL+FTP)

    本文介绍如何在 Linux下搭建一个 PHP 环境.其中 Linux 系统使用是 CentOS 7.3,部署在阿里云服务器上.   1,连接登录服务器 拿到服务器的 ip.初始密码以后.我们先通过远程 ...

  3. Struts2自定义拦截器处理全局异常

    今天在整理之前的项目的时候想着有的action层没有做异常处理,于是想着自定义拦截器处理一下未拦截的异常. 代码: package cn.xm.exam.action.safeHat; import ...

  4. C++学习2--坦克大战编写-前置知识

    基础班学习的这一个多月里的前三周讲解基础的语法,最后一周需要做坦克大战的项目巩固提高自己掌握的语法知识.这个系列博文主要是为了把学习过程中的知识点总结并记录下来: 开发语言与开发工具:C++,VS20 ...

  5. word文档里打不开公式 打开后都是方框

    因为系统缺少一种字体,只要到网络上下载或到其他计算机中复制一种文件名为“symbol.ttf”的字体文件来安装上,就可以了.参考资料:Office之家 http://www.officejia.com ...

  6. CrossUI SPA Builder ---- feathers API框架

    CrossUI SPA Builder:   http://www.crossui.com/ 国产?   龙博(JSLINB)AJAX框架? CrossUI SPA Builderenables de ...

  7. js检测当前设备是移动端还是PC端

    加上下面js即可 硬核判断: <script type="text/javascript"> //平台.设备和操作系统 var system ={ win : fals ...

  8. vue-router两种模式,到底什么情况下用hash,什么情况下用history模式呢?

    转:https://segmentfault.com/q/1010000010340823/a-1020000010598395 为什么要有 hash 和 history 对于 Vue 这类渐进式前端 ...

  9. js数值进制互转

    十进制转换为二进制: var num = 100; console.log(num.toString(2)); toString()方法可把一个 Number 对象转换为一个字符串,并返回结果. 语法 ...

  10. zoj3195 联通树上三个点的路径长

    输出有个坑,两个月之前就没对,,今天又被坑了一次 求联通树上三个点的路径长度,只要求两两点对的最短路径,加起来除以二即可 #include<iostream> #include<cs ...