因项目需求需要将图片合并故写了一个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合并图片的更多相关文章

  1. python接收图片变成缩略图

    python图像处理库:Pillow初级教程 Image类 Pillow中最重要的类就是Image,该类存在于同名的模块中.可以通过以下几种方式实例化:从文件中读取图片,处理其他图片得到,或者直接创建 ...

  2. 用DIV+CSS切割多背景合并图片 CSS Sprites 技术

    很久之前就在互联网网站和一些js插件中见过这种技术的应用,当时觉得很麻烦,就没有用,也没有去深究. 近段时间一直在做前台的一些东西,涉及到很多div+css的问题.这个东东我又碰到了,所以我花了点时间 ...

  3. c# 根据窗口截图,合并图片

    c# 根据窗口截图,合并图片 public class CaptureWindows { #region 类 /// <summary> /// Helper class containi ...

  4. Python提取图片的ROI

    图像处理经常需要提取图片的ROI,本文使用Python提取图片的ROI. 使用的Module是PIL (Pillow),一个图像处理库,用到的函数为类 Image 中的 crop 方法. 函数原型为: ...

  5. python 读取图片的尺寸、分辨率

    #需要安装PIL模块 #encoding=gbk#--------------------------------------------------------------------------- ...

  6. 减少HTTP请求之合并图片详解(大型网站优化技术)

    原文:减少HTTP请求之合并图片详解(大型网站优化技术) 一.相关知识讲解 看过雅虎的前端优化35条建议,都知道优化前端是有多么重要.页面的加载速度直接影响到用户的体验.80%的终端用户响应时间都花在 ...

  7. C#放缩、截取、合并图片并生成高质量新图的类

    原文:C#放缩.截取.合并图片并生成高质量新图的类 using System;using System.Drawing;using System.Drawing.Imaging;using Syste ...

  8. python 对比图片相似度

    最近appium的使用越来越广泛了,对于测试本身而言,断言同样是很重要的,没有准确的断言那么就根本就不能称之为完整的测试了.那么目前先从最简单的截图对比来看.我这里分享下python的图片相似度的代码 ...

  9. C#一些常用的图片操作方法:生成文字图片 合并图片等

    生成文字图片: /// <summary> /// 生成文字图片 /// </summary> /// <param name="text">& ...

随机推荐

  1. 【bzoj4810】【ynoi2018】由乃的玉米田

    4810: [Ynoi2017]由乃的玉米田 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 1090  Solved: 524[Submit][Sta ...

  2. oracle中 trunc(),round(),ceil(),floor的使用

    oracle中 trunc(),round(),ceil(),floor的使用 原文: http://www.2cto.com/database/201310/248336.html 1.round函 ...

  3. ORACLE获取某个时间段之间的月份列表

    返回1-31,或者1-12,或者某个 select rownum   from dual   connect by rownum<31 就是connect by http://marcospri ...

  4. HTML 页面源代码布局介绍

    http://www.cnblogs.com/polk6/archive/2013/05/10/3071451.html 此介绍以google首页源代码截图为例: 从上到下依次介绍: 1.<!D ...

  5. Libevent学习笔记(四) bufferevent 的 concepts and basics

    Bufferevents and evbuffers Every bufferevent has an input buffer and an output buffer. These are of ...

  6. gitlab之邮箱配置

    一.gitlab配置邮件通知功能 编辑/etc/gitlab/gitlab.rb 文件   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #配置如下   gitlab_rai ...

  7. Java修饰符 public、protected、default、private

    2.访问修饰符public,private,protected,以及不写(默认)时的区别?答: 修饰符 当前类 同 包 子 类 其他包 public √ √ √ √ protected √ √ √ × ...

  8. react-native安装react-navigation后出现package-lock.json文件的坑

    npm5.0开始安装后回生成一个新的package-lock.json文件.以致初始化好的react-native项目引入的依赖被删除. 目前解决办法.使用facebook的yarn add 第三方组 ...

  9. 层级 z-index 透明opacity

    在正常情况下,层级的大小由顺序决定,后面的元素要比前面的元素的层级要高 有定位元素的层级要比没有定位元素层级要高 在都有定位的情况下,层级还是取决于书写顺序 z-index 层级(仅能在定位元素上奏效 ...

  10. [DeeplearningAI笔记]序列模型3.1基本的 Seq2Seq /image to Seq

    5.3序列模型与注意力机制 觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.1基础模型 [1] Sutskever I, Vinyals O, Le Q V. Sequence to Se ...