python合并图片
因项目需求需要将图片合并故写了一个python脚本,在这里放个笔记
#!/usr/bin/env python
#coding=utf-8 import Image
import os
import sys
import glob
import time
import shutil def merge_thumb(files, output_file):
imgs = []
width = 0
height = 0 index = 0 # 计算总宽度和长度
for file in files:
print ("file name : %s" % (str(file)))
img = Image.open(file)
imgs.append(img)
if img.size[0] > width:
width = img.size[0]
height = img.size[1] # 新建一个白色底的图片
merge_img = Image.new('RGB', (width, height), 0xffffff)
for img in imgs:
# 把图片粘贴上去
merge_img.paste(img, (0, 0), img) merge_img.save(output_file, quality=70) def getPngName(filename):
begin = filename.index('thumbs/') + 7
end = len(filename)- 4
result = filename[begin:end]
return int(result) def coverFiles(sourceFile, targetDir):
filename = os.path.split(sourceFile)[-1]
targetFile = os.path.join(targetDir,filename)
if os.path.isfile(sourceFile):
open(targetFile, "wb").write(open(sourceFile, "rb").read()) if __name__ == '__main__':
ROOT_PATH = os.path.abspath(os.path.dirname(__file__))
#IMG_PATH = os.path.join(ROOT_PATH, 'img')
THUMB_PATH = os.path.join(ROOT_PATH, 'thumbs') print ("thumb_path:%s" % str(THUMB_PATH))
if not os.path.exists(THUMB_PATH):
os.makedirs(THUMB_PATH) files = glob.glob(os.path.join(ROOT_PATH,'*.png'))
for srcFile in files:
targetFile = "thumbs/"+os.path.split(srcFile)[-1]
shutil.copy(srcFile,targetFile)
RESULT_PATH = os.path.join(THUMB_PATH, 'result')
if not os.path.exists(RESULT_PATH):
os.makedirs(RESULT_PATH) files = glob.glob(os.path.join(THUMB_PATH, '*.png'))
files_len = len(files) for i in range(0,files_len):
# 合并图片
files = glob.glob(os.path.join(THUMB_PATH, '*.png')) files_new = []
index = 0 #print(files)
for file in files:
files_new_len = len(files_new)
insert_index = 0
for j in range(0,files_new_len):
if (getPngName(files_new[j])>getPngName(files[index])):
insert_index = j+1;
files_new.insert(insert_index,file)
index += 1 #get put out file name
index = len(files) - len(files_new)
index = len(files_new) - index - 1 begin = files_new[index].index('thumbs/') + 7
end = len(files_new[index])
output = 'result/'+files_new[index][begin:end]
print ('output=%s index = %s' % (output,index)) merge_output = os.path.join(THUMB_PATH, output)
begin_time = time.clock()
merge_thumb(files_new, merge_output)
end_time = time.clock()
print ('merge_thumb time:%s**********output:%s*******remove:%s' % (str(end_time - begin_time),output,files_new[index])) os.remove(files_new[index])
我所做的事情是讲一个文件夹下面的所有图片如(1.png 2.png ... 10.png)按照一定规律合并
合并规律为1-10合并为1.png 2-10合并为2.png 以此类推
python合并图片的更多相关文章
- python接收图片变成缩略图
python图像处理库:Pillow初级教程 Image类 Pillow中最重要的类就是Image,该类存在于同名的模块中.可以通过以下几种方式实例化:从文件中读取图片,处理其他图片得到,或者直接创建 ...
- 用DIV+CSS切割多背景合并图片 CSS Sprites 技术
很久之前就在互联网网站和一些js插件中见过这种技术的应用,当时觉得很麻烦,就没有用,也没有去深究. 近段时间一直在做前台的一些东西,涉及到很多div+css的问题.这个东东我又碰到了,所以我花了点时间 ...
- c# 根据窗口截图,合并图片
c# 根据窗口截图,合并图片 public class CaptureWindows { #region 类 /// <summary> /// Helper class containi ...
- Python提取图片的ROI
图像处理经常需要提取图片的ROI,本文使用Python提取图片的ROI. 使用的Module是PIL (Pillow),一个图像处理库,用到的函数为类 Image 中的 crop 方法. 函数原型为: ...
- python 读取图片的尺寸、分辨率
#需要安装PIL模块 #encoding=gbk#--------------------------------------------------------------------------- ...
- 减少HTTP请求之合并图片详解(大型网站优化技术)
原文:减少HTTP请求之合并图片详解(大型网站优化技术) 一.相关知识讲解 看过雅虎的前端优化35条建议,都知道优化前端是有多么重要.页面的加载速度直接影响到用户的体验.80%的终端用户响应时间都花在 ...
- C#放缩、截取、合并图片并生成高质量新图的类
原文:C#放缩.截取.合并图片并生成高质量新图的类 using System;using System.Drawing;using System.Drawing.Imaging;using Syste ...
- python 对比图片相似度
最近appium的使用越来越广泛了,对于测试本身而言,断言同样是很重要的,没有准确的断言那么就根本就不能称之为完整的测试了.那么目前先从最简单的截图对比来看.我这里分享下python的图片相似度的代码 ...
- C#一些常用的图片操作方法:生成文字图片 合并图片等
生成文字图片: /// <summary> /// 生成文字图片 /// </summary> /// <param name="text">& ...
随机推荐
- ORACLE 中如何截取到时间的年月日中的年、月、日
在Oracle中,要获得日期中的年份,例如把sysdate中的年份取出来,并不是一件难事.常用的方法是:Select to_number(to_char(sysdate,'yyyy')) from d ...
- makefile使用笔记(二)变量
By francis_hao Oct 30,2017 makefile中可以使用变量,变量有多种类型,下面分别介绍 简单变量 简单变量的命名规则和c语言一致. 给变量赋值就表示创建了这个变量 ...
- js中字符串全部替换
废话不多说,直接发结果 在js中字符串全部替换可以用以下方法: str.replace(/需要替换的字符串/g,"新字符串") 比如: "yyyy-MM-dd-hh-mm ...
- Codeforces 938.D Buy a Ticket
D. Buy a Ticket time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- php 性能优化之opcache - 让你的php性能提升 50%
性能提升原理:减少文件解析的时间. 我们都知道,程序要运行,得有一个编译或者解析的过程,编译或解析之后的代码才是机器可以运行的. 而 php 是一种解析性语言,在使用php来处理http请求的时候,每 ...
- JSP2 的自定义标签
在 JSP 中开发标签库只需如下几个步骤 1.开发自定义标签处理类 2.建立一个 *.tld 文件,每个 *.tld 文件对应一个标签库,每个标签库可包含多个标签 3.在 JSP 文件中使用自定义标签 ...
- lightoj 1020 (博弈水题)
lightoj 1020 A Childhood Game 链接:http://lightoj.com/volume_showproblem.php?problem=1020 题意:一堆石子有 m 个 ...
- reset password for local admin on Windows2016 by Powershell
上脚本吧,找半天 $password = "yourpassword" $pwd = $password | ConvertTo-SecureString -asPlainText ...
- 解决CodeBlocks无法自动补全的问题
在Deepin下安装的CB,输入printf.scanf的时候不会自动补全,这样就很难受. 解决办法是在Setting -> Editor -> Syntax highlighting - ...
- ZOJ 3774 二次剩余
LINK 题意:简单粗暴,求菲波那契数列每个数的m次的前n项和模1e9+7 思路:斐波那契通项式, 注意到有很多根号5,求二次剩余为5模1e9+7的解,显然我们可以直接找一个(383008016),然 ...