#coding=utf-8
import Image
import os
#print list[0]
#exit()
def getlogo(x1,y1):
im =Image.open("./"+"shuiyin.jpg")
z1=int(x1)
z2=int(y1)
in2=im.resize((z1,z2))
#in2.show()
return in2
list=os.listdir("./")
for a in list:
#print a
if os.path.isdir(a):
print "is path---------"
continue
if a.split('.')[1]=='py':
print "is py---------"
continue
path="./get/"
if not os.path.isdir(path):
os.mkdir(path)
if a.split('.')[1]=='jpg':
im =Image.open("./"+a)
x=im.size[0]
y=im.size[1]
x1=int(x*0.9)
y1=int(y*0.95)
logo=getlogo((x-x1),(y-y1))
box=(x1,y1,x,y)
im.paste(logo, box)
im.save(path+a)
print "==================="+a
输出
'=================='后面的就是处理过的图片,代码将shuiyin.jpg作为一个水印logo贴到工作文件夹下的每张.jpg照片
效果如图:
===================================================================================================
图片添加文字水印:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2014-11-29 19:09:59
# @Author : Linsir (vi5i0n@hotmail.com)
# @Link : http://linsir.org
import Image, ImageEnhance, ImageDraw, ImageFont
def text2img(text, font_color="Blue", font_size=25):
"""生成内容为 TEXT 的水印"""
font = ImageFont.truetype('Anonymous pro.ttf', font_size)
#多行文字处理
text = text.split('\n')#分成两行了Linsir.水印. vi5i0n@hotmail.com
mark_width = 0
for i in range(len(text)):#循环两次
(width, height) = font.getsize(text[i])
if mark_width < width:
mark_width = width#选两行中最长的一行当水印宽度
mark_height = height * len(text)#两层文字水印的高度
#生成水印图片
mark = Image.new('RGBA', (mark_width,mark_height))
draw = ImageDraw.ImageDraw(mark, "RGBA")
draw.setfont(font)
for i in range(len(text)):
(width, height) = font.getsize(text[i])
draw.text((0, i*height), text[i], fill=font_color)#################
return mark
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]
y = im.size[1] - mark.size[1]
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 main():
text = u'Liyabin.\nlybuestc@hotmail.com'
# text = open('README.md').read().decode('utf-8')
# print text
im = Image.open('pic.jpg')
mark = text2img(text)
image = watermark(im, mark, 'right_bottom', 0.9)
if image:
image.save('pic_shuiyin.png')
image.show()
print 'DONE!'
else:
print "Sorry, Failed."
if __name__ == '__main__':
main()
==========================================================================
第 0000 题:将你的
QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。 类似于图中效果
其中自己的代码因为PIL文件夹添加到搜索库的路径中了,所以直接import Image, ImageDraw, ImageFont就行了