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模块:图像处理模 ...
随机推荐
- oracle怎么给表和列加注释
oracle添加注释的语法为: comment on column 字段名 is '注释名' 举例: 创建表: CREATE TABLE t1{ id varchar2(32) primary ke ...
- np.savetxt()——将array保存到txt文件,并保持原格式
问题:1.如何将array保存到txt文件中?2.如何将存到txt文件中的数据读出为ndarray类型? 需求:科学计算中,往往需要将运算结果(array类型)保存到本地,以便进行后续的数据分析. 解 ...
- Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))
Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ...
- 关于出现Not an editor command: Bundle '**/*.vim'的解决方案【转】
转自:https://blog.csdn.net/YHM07/article/details/49717933 操作系统: $ uname -r 2.6.32-573.7.1.el6.x86_64 $ ...
- .netcore 整合 log4net
1.背景 前两天,曾经的一个同事咨询我,怎样将log4net以中间件的形式整合到core里边去.我不假思索的回答,这种问题应该有人做过吧,他说没有.于是,我去博客园搜了下,发现还真没有,全部都是传统. ...
- Ex 6_2 假设您准备一次长途旅行..._第五次作业
假设n个旅馆距离原点的距离存放在数组arr[0. . .n-1]中,punish[0. . .n-1]表示在某个旅馆停留时所受的的惩罚的最小值.当然在第一个旅馆停留时所受到的惩罚为0,即punish[ ...
- 解决Jmeter插件ERROR: java.io.IOException: Agent is unreachable via TCP的错误
今天在centos上搭建jmeter监控服务,服务正常启动,我点击run,就在一切看起来很美好的时候,报错了,ERROR: java.io.IOException: Agent is unreacha ...
- Java基础98 gson插件的使用
1.要用到的包 2.实例 实体类 people package com.shore.entity; /** * @author DSHORE/2019-4-21 * */ public class P ...
- 性能测试四十:Mysql存储过程造数据
性能测试是基于大量数据的,而进行性能测试之前肯定没那么多数据,所以就要自己准备数据 数据构造方法: 1.业务接口 -- 适合数据表关系复杂 -- 优点:数据完整性比较好2.存储过程 -- 适合表数量少 ...
- plsql developer连接Oracle报错ORA-12154: TNS:could not resolve the connect identifier specified
今日更改Oracle网络配置文件后使用plsql developer 尝试连接到Oracle出现报错 ORA-12154: TNS:could not resolve the connect iden ...