python批量给图加水印
2016-04-14 10:34
429 查看
以上是用python批量给图片添加水印
# -*- coding: utf8 -*-
import os,Image, ImageEnhance
def set_opacity(im, opacity):
"""设置透明度"""
assert opacity >=0 and opacity < 1
if im.mode != "RGBA":
im = im.convert('RGBA')
else:
im = im.copy()
alpha = im.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
im.putalpha(alpha)
return im
def watermark(im, mark, position, opacity=1):
"""添加水印"""
try:
if opacity < 1:
mark = set_opacity(mark, opacity)
if im.mode != 'RGBA':
im = im.convert('RGBA')
#if im.size[0] < mark.size[0] or im.size[1] < mark.size[1]:
#print "The mark image size is larger size than original image file."
#return False
#设置水印位置
if position == 'left_top':
x = 0
y = 0
elif position == 'left_bottom':
x = 0
y = im.size[1] - mark.size[1]
elif position == 'right_top':
x = im.size[0] - mark.size[0]
y = 0
elif position == 'right_bottom':
x = im.size[0] - mark.size[0]+5
y = im.size[1] - mark.size[1]-16
else:
x = (im.size[0] - mark.size[0]) / 2
y = (im.size[1] - mark.size[1]) / 2
layer = Image.new('RGBA', im.size,)
layer.paste(mark,(x,y))
return Image.composite(layer, im, layer)
except Exception as e:
print ">>>>>>>>>>> WaterMark EXCEPTION: " + str(e)
return False
def water(filename,mark):
try:
im = Image.open(filename) #原图
image = watermark(im, mark, 'right_bottom', 1)
image.save(filename)
#image.show()
except Exception as e:
print ">>>>>>>>>>> WaterMark save: " + str(e)
return False
def dirlist(path,mark):
global num
filelist = os.listdir(path)
for filename in filelist:
filepath = os.path.join(path, filename)
if os.path.isdir(filepath):
dirlist(filepath,mark)
else:
water(filepath,mark)
num += 1
print filepath, num
num = 0
if __name__ == '__main__':
water_path = "E:/water/logo.jpg"
dir_path = "H:/hx/bj/"
mark = Image.open(water_path) #水印
dirlist(dir_path,mark)
# -*- coding: utf8 -*-
import os,Image, ImageEnhance
def set_opacity(im, opacity):
"""设置透明度"""
assert opacity >=0 and opacity < 1
if im.mode != "RGBA":
im = im.convert('RGBA')
else:
im = im.copy()
alpha = im.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
im.putalpha(alpha)
return im
def watermark(im, mark, position, opacity=1):
"""添加水印"""
try:
if opacity < 1:
mark = set_opacity(mark, opacity)
if im.mode != 'RGBA':
im = im.convert('RGBA')
#if im.size[0] < mark.size[0] or im.size[1] < mark.size[1]:
#print "The mark image size is larger size than original image file."
#return False
#设置水印位置
if position == 'left_top':
x = 0
y = 0
elif position == 'left_bottom':
x = 0
y = im.size[1] - mark.size[1]
elif position == 'right_top':
x = im.size[0] - mark.size[0]
y = 0
elif position == 'right_bottom':
x = im.size[0] - mark.size[0]+5
y = im.size[1] - mark.size[1]-16
else:
x = (im.size[0] - mark.size[0]) / 2
y = (im.size[1] - mark.size[1]) / 2
layer = Image.new('RGBA', im.size,)
layer.paste(mark,(x,y))
return Image.composite(layer, im, layer)
except Exception as e:
print ">>>>>>>>>>> WaterMark EXCEPTION: " + str(e)
return False
def water(filename,mark):
try:
im = Image.open(filename) #原图
image = watermark(im, mark, 'right_bottom', 1)
image.save(filename)
#image.show()
except Exception as e:
print ">>>>>>>>>>> WaterMark save: " + str(e)
return False
def dirlist(path,mark):
global num
filelist = os.listdir(path)
for filename in filelist:
filepath = os.path.join(path, filename)
if os.path.isdir(filepath):
dirlist(filepath,mark)
else:
water(filepath,mark)
num += 1
print filepath, num
num = 0
if __name__ == '__main__':
water_path = "E:/water/logo.jpg"
dir_path = "H:/hx/bj/"
mark = Image.open(water_path) #水印
dirlist(dir_path,mark)
相关文章推荐
- python 类属性
- 详解:Python2中的urllib、urllib2与Python3中的urllib以及第三方模块requests
- Python中*args 和**kwargs的用法
- Python之sys模块
- Python模块之os.path
- 初学python(对比java语言不同) 第十篇
- 人脸检测新手入门篇
- python爬虫入门实践
- python线程的一些疑问
- Python教程1-7
- python非阻塞启动进程
- python 小模块--platform
- python 牛顿迭代小游戏
- Show me the code之Python练习册 Q8~9 html解析
- Show me the code之Python练习册 Q4~7
- Show me the code之Python练习册 Q1~3 优惠券
- python中if __name__ == '__main__': 的解析
- python读txt文件的编码问题
- python基本数据类型-数字
- 开始写blog记录下学习python的过程