Python3 图片水平镜像实现
# -*- 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 图片水平镜像实现的更多相关文章
- CSS制作图片水平垂直居中
所谓的图片水平垂直居中就是把图片放在一个容器元素中(容器大于图片尺寸或是指定了大小的容器),并且图片位居此容器正中间(中间是指元素容器的正中间),而图片不是以背景图片(background-image ...
- DIV里面的图片水平与垂直居中的方法
<div class=“box”> <img /> </div> 1.水平居中: 1)box设置 text-align:center ; text-alig ...
- DIV或者DIV里面的图片水平与垂直居中的方法
<div class=“box”> <img /> </div> 水平居中的常用方式: text-align:center ——这可以实现子元素字体,图片的水平居中 ...
- [iOS] UICollectionView实现图片水平滚动
最新更新: 简单封装了一下代码,参考新文章:UICollectionView实现图片水平滚动 先简单看一下效果: 新博客:http://wossoneri.github.io 准备数据 首先先加入一些 ...
- 图片水平垂直居中(兼容IE6,IE7,firefox,opera,safari,其中图片可以是任何块元素)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- css实现图片水平垂直居中
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 使用Recyclerview实现图片水平自动循环滚动
简介: 本篇博客主要介绍的是如何使用RecyclerView实现图片水平方向自动循环(跑马灯效果) 效果图: 思路: 1.准备m张图片 1.使用Recyclerview实现,返回无数个(实际Inter ...
- Win8Metro(C#)数字图像处理--2.19图像水平镜像
原文:Win8Metro(C#)数字图像处理--2.19图像水平镜像 [函数名称] 图像水平镜像函数MirrorXProcess(WriteableBitmap src) [函数代码] ...
- Python3图片处理头像
一. 简介: Python3图片处理头像右上角类似QQ,微信右上角未读信息效果,其实没有实质作用,只是简单练习. 1. 环境: python3.5 random模块:生成随机数 PIL模块:图像处理模 ...
随机推荐
- mysql 原理 ~ double write
一 简介:今天来聊聊double write 二 细节 1 Double write 是InnoDB在 tablespace(ibdata1)上的128个页(2个区)是2MB: 2 何谓页断裂 所谓页 ...
- Python 对图片进行人脸识别
import cv2 def detect(path): img = cv2.imread(path) cascade = cv2.CascadeClassifier("/vagrant/d ...
- python函数——形参中的:*args和**kwargs
python函数——形参中的:*args和**kwargs 多个实参,放到一个元组里面,以*开头,可以传多个参数:**是形参中按照关键字传值把多余的传值以字典的方式呈现 *args:(表示的就是将 ...
- Majority Element(169) && Majority Element II(229)
寻找多数元素这一问题主要运用了:Majority Vote Alogrithm(最大投票算法)1.Majority Element 1)description Given an array of si ...
- springboot系列六、springboot配置错误页面及全局异常
一.spring1.x中处理方式 @Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return new ...
- DNS详解: A记录,子域名,CNAME别名,PTR,MX,TXT,SRV,TTL
DNS DNS,Domain Name System或者Domain Name Service(域名系统或者域名服务).域名系统为Internet上的主机分配域名地址和IP地址.由于网络中的计算机都必 ...
- SPLAY,LCT学习笔记(六)
这应该暂时是个终结篇了... 最后在这里讨论LCT的一个常用操作:维护虚子树信息 这也是一个常用操作 下面我们看一下如何来维护 以下内容转自https://blog.csdn.net/neither_ ...
- python 全栈开发,Day125(HTML5+ 初识,HBuilder,夜神模拟器,Webview)
昨日内容回顾 1.增删改查: 增: db.collections.insert({a:1}) // 官方不推荐了 db.collections.insertMany([{a:1},{b:1}]) in ...
- python 全栈开发,Day27(复习, defaultdict,Counter,时间模块,random模块,sys模块)
一.复习 看下面一段代码,假如运行结果有问题,那么就需要在每一步计算时,打印一下结果 b = 1 c = 2 d = 3 a = b+c print(a) e = a + d print(e) 执行输 ...
- linux下安装ruby环境
安装步骤: ruby的shell叫irb,我们可以输入irb -v查看下irb的版本号 1.安装ruby解释器shell: yum install ruby-irb -y Installed: rub ...