python图片添加水印(转载)
转载来自:http://blog.csdn.net/orangleliu/
# -*- encoding=utf-8 -*-
'''''
author: orangleliu
pil处理图片,验证,处理
大小,格式 过滤
压缩,截图,转换 图片库最好用Pillow
还有一个测试图片test.jpg, 一个log图片,一个字体文件
''' #图片的基本参数获取
try:
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
except ImportError:
import Image, ImageDraw, ImageFont, ImageEnhance def compress_image(img, w=128, h=128):
'''''
缩略图
'''
img.thumbnail((w,h))
im.save('test1.png', 'PNG')
print u'成功保存为png格式, 压缩为128*128格式图片' def cut_image(img):
'''''
截图, 旋转,再粘贴
'''
#eft, upper, right, lower
#x y z w x,y 是起点, z,w是偏移值
width, height = img.size
box = (width-200, height-100, width, height)
region = img.crop(box)
#旋转角度
region = region.transpose(Image.ROTATE_180)
img.paste(region, box)
img.save('test2.jpg', 'JPEG')
print u'重新拼图成功' def logo_watermark(img, logo_path):
'''''
添加一个图片水印,原理就是合并图层,用png比较好
'''
baseim = img
logoim = Image.open(logo_path)
bw, bh = baseim.size
lw, lh = logoim.size
baseim.paste(logoim, (bw-lw, bh-lh))
baseim.save('test3.jpg', 'JPEG')
print u'logo水印组合成功' def text_watermark(img, text, out_file="test4.jpg", angle=23, opacity=0.50):
'''''
添加一个文字水印,做成透明水印的模样,应该是png图层合并
http://www.pythoncentral.io/watermark-images-python-2x/
这里会产生著名的 ImportError("The _imagingft C module is not installed") 错误
Pillow通过安装来解决 pip install Pillow
'''
watermark = Image.new('RGBA', img.size, (255,255,255)) #我这里有一层白色的膜,去掉(255,255,255) 这个参数就好了 FONT = "msyh.ttf"
size = 2 n_font = ImageFont.truetype(FONT, size) #得到字体
n_width, n_height = n_font.getsize(text)
text_box = min(watermark.size[0], watermark.size[1])
while (n_width+n_height < text_box):
size += 2
n_font = ImageFont.truetype(FONT, size=size)
n_width, n_height = n_font.getsize(text) #文字逐渐放大,但是要小于图片的宽高最小值 text_width = (watermark.size[0] - n_width) / 2
text_height = (watermark.size[1] - n_height) / 2
#watermark = watermark.resize((text_width,text_height), Image.ANTIALIAS)
draw = ImageDraw.Draw(watermark, 'RGBA') #在水印层加画笔
draw.text((text_width,text_height),
text, font=n_font, fill="#21ACDA")
watermark = watermark.rotate(angle, Image.BICUBIC)
alpha = watermark.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
watermark.putalpha(alpha)
Image.composite(watermark, img, watermark).save(out_file, 'JPEG')
print u"文字水印成功" #等比例压缩图片
def resizeImg(img, dst_w=0, dst_h=0, qua=85):
'''''
只给了宽或者高,或者两个都给了,然后取比例合适的
如果图片比给要压缩的尺寸都要小,就不压缩了
'''
ori_w, ori_h = im.size
widthRatio = heightRatio = None
ratio = 1 if (ori_w and ori_w > dst_w) or (ori_h and ori_h > dst_h):
if dst_w and ori_w > dst_w:
widthRatio = float(dst_w) / ori_w #正确获取小数的方式
if dst_h and ori_h > dst_h:
heightRatio = float(dst_h) / ori_h if widthRatio and heightRatio:
if widthRatio < heightRatio:
ratio = widthRatio
else:
ratio = heightRatio if widthRatio and not heightRatio:
ratio = widthRatio if heightRatio and not widthRatio:
ratio = heightRatio newWidth = int(ori_w * ratio)
newHeight = int(ori_h * ratio)
else:
newWidth = ori_w
newHeight = ori_h im.resize((newWidth,newHeight),Image.ANTIALIAS).save("test5.jpg", "JPEG", quality=qua)
print u'等比压缩完成' '''''
Image.ANTIALIAS还有如下值:
NEAREST: use nearest neighbour
BILINEAR: linear interpolation in a 2x2 environment
BICUBIC:cubic spline interpolation in a 4x4 environment
ANTIALIAS:best down-sizing filter
''' #裁剪压缩图片
def clipResizeImg(im, dst_w, dst_h, qua=95):
'''''
先按照一个比例对图片剪裁,然后在压缩到指定尺寸
一个图片 16:5 ,压缩为 2:1 并且宽为200,就要先把图片裁剪成 10:5,然后在等比压缩
'''
ori_w,ori_h = im.size dst_scale = float(dst_w) / dst_h #目标高宽比
ori_scale = float(ori_w) / ori_h #原高宽比 if ori_scale <= dst_scale:
#过高
width = ori_w
height = int(width/dst_scale) x = 0
y = (ori_h - height) / 2 else:
#过宽
height = ori_h
width = int(height*dst_scale) x = (ori_w - width) / 2
y = 0 #裁剪
box = (x,y,width+x,height+y)
#这里的参数可以这么认为:从某图的(x,y)坐标开始截,截到(width+x,height+y)坐标
#所包围的图像,crop方法与php中的imagecopy方法大为不一样
newIm = im.crop(box)
im = None #压缩
ratio = float(dst_w) / width
newWidth = int(width * ratio)
newHeight = int(height * ratio)
newIm.resize((newWidth,newHeight),Image.ANTIALIAS).save("test6.jpg", "JPEG",quality=95)
print "old size %s %s"%(ori_w, ori_h)
print "new size %s %s"%(newWidth, newHeight)
print u"剪裁后等比压缩完成" if __name__ == "__main__":
'''''
主要是实现功能, 代码没怎么整理
'''
im = Image.open('test.jpg') #image 对象
compress_image(im) im = Image.open('test.jpg') #image 对象
cut_image(im) im = Image.open('test.jpg') #image 对象
logo_watermark(im, 'logo.png') im = Image.open('test.jpg') #image 对象
text_watermark(im, 'Orangleliu') im = Image.open('test.jpg') #image 对象
resizeImg(im, dst_w=100, qua=85) im = Image.open('test.jpg') #image 对象
clipResizeImg(im, 100, 200) # -*- encoding=utf-8 -*-
'''''
author: orangleliu
pil处理图片,验证,处理
大小,格式 过滤
压缩,截图,转换 图片库最好用Pillow
还有一个测试图片test.jpg, 一个log图片,一个字体文件
''' #图片的基本参数获取
try:
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
except ImportError:
import Image, ImageDraw, ImageFont, ImageEnhance def compress_image(img, w=128, h=128):
'''''
缩略图
'''
img.thumbnail((w,h))
im.save('test1.png', 'PNG')
print u'成功保存为png格式, 压缩为128*128格式图片' def cut_image(img):
'''''
截图, 旋转,再粘贴
'''
#eft, upper, right, lower
#x y z w x,y 是起点, z,w是偏移值
width, height = img.size
box = (width-200, height-100, width, height)
region = img.crop(box)
#旋转角度
region = region.transpose(Image.ROTATE_180)
img.paste(region, box)
img.save('test2.jpg', 'JPEG')
print u'重新拼图成功' def logo_watermark(img, logo_path):
'''''
添加一个图片水印,原理就是合并图层,用png比较好
'''
baseim = img
logoim = Image.open(logo_path)
bw, bh = baseim.size
lw, lh = logoim.size
baseim.paste(logoim, (bw-lw, bh-lh))
baseim.save('test3.jpg', 'JPEG')
print u'logo水印组合成功' def text_watermark(img, text, out_file="test4.jpg", angle=23, opacity=0.50):
'''''
添加一个文字水印,做成透明水印的模样,应该是png图层合并
http://www.pythoncentral.io/watermark-images-python-2x/
这里会产生著名的 ImportError("The _imagingft C module is not installed") 错误
Pillow通过安装来解决 pip install Pillow
'''
watermark = Image.new('RGBA', img.size, (255,255,255)) #我这里有一层白色的膜,去掉(255,255,255) 这个参数就好了 FONT = "msyh.ttf"
size = 2 n_font = ImageFont.truetype(FONT, size) #得到字体
n_width, n_height = n_font.getsize(text)
text_box = min(watermark.size[0], watermark.size[1])
while (n_width+n_height < text_box):
size += 2
n_font = ImageFont.truetype(FONT, size=size)
n_width, n_height = n_font.getsize(text) #文字逐渐放大,但是要小于图片的宽高最小值 text_width = (watermark.size[0] - n_width) / 2
text_height = (watermark.size[1] - n_height) / 2
#watermark = watermark.resize((text_width,text_height), Image.ANTIALIAS)
draw = ImageDraw.Draw(watermark, 'RGBA') #在水印层加画笔
draw.text((text_width,text_height),
text, font=n_font, fill="#21ACDA")
watermark = watermark.rotate(angle, Image.BICUBIC)
alpha = watermark.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
watermark.putalpha(alpha)
Image.composite(watermark, img, watermark).save(out_file, 'JPEG')
print u"文字水印成功" #等比例压缩图片
def resizeImg(img, dst_w=0, dst_h=0, qua=85):
'''''
只给了宽或者高,或者两个都给了,然后取比例合适的
如果图片比给要压缩的尺寸都要小,就不压缩了
'''
ori_w, ori_h = im.size
widthRatio = heightRatio = None
ratio = 1 if (ori_w and ori_w > dst_w) or (ori_h and ori_h > dst_h):
if dst_w and ori_w > dst_w:
widthRatio = float(dst_w) / ori_w #正确获取小数的方式
if dst_h and ori_h > dst_h:
heightRatio = float(dst_h) / ori_h if widthRatio and heightRatio:
if widthRatio < heightRatio:
ratio = widthRatio
else:
ratio = heightRatio if widthRatio and not heightRatio:
ratio = widthRatio if heightRatio and not widthRatio:
ratio = heightRatio newWidth = int(ori_w * ratio)
newHeight = int(ori_h * ratio)
else:
newWidth = ori_w
newHeight = ori_h im.resize((newWidth,newHeight),Image.ANTIALIAS).save("test5.jpg", "JPEG", quality=qua)
print u'等比压缩完成' '''''
Image.ANTIALIAS还有如下值:
NEAREST: use nearest neighbour
BILINEAR: linear interpolation in a 2x2 environment
BICUBIC:cubic spline interpolation in a 4x4 environment
ANTIALIAS:best down-sizing filter
''' #裁剪压缩图片
def clipResizeImg(im, dst_w, dst_h, qua=95):
'''''
先按照一个比例对图片剪裁,然后在压缩到指定尺寸
一个图片 16:5 ,压缩为 2:1 并且宽为200,就要先把图片裁剪成 10:5,然后在等比压缩
'''
ori_w,ori_h = im.size dst_scale = float(dst_w) / dst_h #目标高宽比
ori_scale = float(ori_w) / ori_h #原高宽比 if ori_scale <= dst_scale:
#过高
width = ori_w
height = int(width/dst_scale) x = 0
y = (ori_h - height) / 2 else:
#过宽
height = ori_h
width = int(height*dst_scale) x = (ori_w - width) / 2
y = 0 #裁剪
box = (x,y,width+x,height+y)
#这里的参数可以这么认为:从某图的(x,y)坐标开始截,截到(width+x,height+y)坐标
#所包围的图像,crop方法与php中的imagecopy方法大为不一样
newIm = im.crop(box)
im = None #压缩
ratio = float(dst_w) / width
newWidth = int(width * ratio)
newHeight = int(height * ratio)
newIm.resize((newWidth,newHeight),Image.ANTIALIAS).save("test6.jpg", "JPEG",quality=95)
print "old size %s %s"%(ori_w, ori_h)
print "new size %s %s"%(newWidth, newHeight)
print u"剪裁后等比压缩完成" if __name__ == "__main__":
'''''
主要是实现功能, 代码没怎么整理
'''
im = Image.open('test.jpg') #image 对象
compress_image(im) im = Image.open('test.jpg') #image 对象
cut_image(im) im = Image.open('test.jpg') #image 对象
logo_watermark(im, 'logo.png') im = Image.open('test.jpg') #image 对象
text_watermark(im, 'Orangleliu') im = Image.open('test.jpg') #image 对象
resizeImg(im, dst_w=100, qua=85) im = Image.open('test.jpg') #image 对象
clipResizeImg(im, 100, 200)
python图片添加水印(转载)的更多相关文章
- 【Python】给图片添加水印的Python及Golang实现
前言 不知道大家有没有这样的习惯,一篇比较得意的博客在发表一段时间之后会特别关注,前段时间一篇写到凌晨的博客被 码迷 这个网关爬取之后发表了,因为搜索引擎先爬取码迷的,所以我的博客无法被搜索到,即使直 ...
- Python Windows 快捷键自动给剪贴板(复制)图片添加水印
编写一个能在windows上使用的按下快捷键自动给剪贴板(复制)的图片添加水印的小工具.plyer.PIL.pyinstaller.pynput.win32clipboard库.记录自己踩过的坑,部分 ...
- 神奇的canvas——巧用 canvas 为图片添加水印
代码地址如下:http://www.demodashi.com/demo/11637.html 很久之前写过一篇关于 canvas 的文章,是通过 canvas 来实现一个绚丽的动画效果,不管看过没看 ...
- Android 图片添加水印图片或者文字
给图片添加水印的基本思路都是载入原图,添加文字或者载入水印图片,保存图片这三个部分 添加水印图片: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
- java实现给图片添加水印
package michael.io.image; import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.aw ...
- java.imageIo给图片添加水印
最近项目在做一个商城项目, 项目上的图片要添加水印①,添加图片水印;②:添加文字水印; 一下提供下个方法,希望大家可以用得着: package com.blogs.image; import java ...
- 教程,Python图片转字符堆叠图
Python 图片转字符画 一.实验说明 1. 环境登录 无需密码自动登录, 2. 环境介绍 本实验环境采用带桌面的UbuntuLinux环境,实验中会用到桌面上的程序: LX终端(LXTermina ...
- Python 图片转字符画
Python 图片转字符画 一.课程介绍 1. 课程来源 原创 2. 内容简介 本课程讲述怎样使用 Python 将图片转为字符画 3. 前置课程 Python编程语言 Linux 基础入门(新版) ...
- python 图片在线转字符画预览
文章链接:https://mp.weixin.qq.com/s/yiFOmljhyalE8ssAgwo6Jw 关于python图片转字符画,相信大家都不陌生,经常出现在 n个超有趣的python项目中 ...
随机推荐
- Jetty使用教程(四:24-27)—Jetty开发指南
二十四.处理器(Handler ) 24.1 编写一个常用的Handler Jetty的Handler组件用来处理接收到的请求. 很多使用者不需要编写Jetty的Handler ,而是通过使用Serv ...
- 手动编译安装docker环境,以及偶尔出现的bug
总结安装过程如下: 前提:安装git,go,make, docker(docker中编译docker) git clone https://git@github.com/docker/docker c ...
- BZOJ2471 : Count
考虑KMP,设$f[i][j][S]$表示还剩最低$i$位没有确定,目前KMP匹配到了$j$这个位置,前缀匹配情况是$S$,最终会匹配到哪里,中途匹配成功几次. 其中$S[i]$是一个pair< ...
- java比较两个对象是否相等的方法
java比较两个对象是否相等直接使用equals方法进行判断肯定是不会相同的. 例如: Person person1 =new Person("张三"); Person pe ...
- jQuery验证控件jquery.validate.js使用说明+中文API
官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 学习 ...
- 关于div的滚动条滚动到底部,内容显示不全的问题。(已解决)
今天我做了一个带有滚动条,底部有两个按钮的div. 当我拖动滚动条到底部, 按钮没有显示出来. 我看了看我的样式设置,是这样的: /* 内容样式 */ #contentPartDiv{ posit ...
- 利用其它带文件防护功能的软件防止*.asp;*.jpg写入文件。
此木马是一个.NET程序制作,如果你的服务器支持.NET那就要注意了,,进入木马有个功能叫:IIS Spy,点击以后可以看到所有站点所在的物理路径.以前有很多人提出过,但一直没有人给解决的答案.. 防 ...
- java内置数据类型
常量在程序运行时,不会被修改的量. 在 Java 中使用 final 关键字来修饰常量,声明方式和变量类似: finaldouble PI =3.1415927; 虽然常量名也可以用小写,但为了便于识 ...
- Codeforces Round #384 (Div. 2) E
给出n个数字 1-8之间 要求选出来一个子序列 使里面1-8的数字个数 极差<=1 并且相同数字必须相邻(112 可以但是121不行)求这个子序列的最长长度 一道状压dp 看不懂别人的dp思想. ...
- sql入门基础
好用的mysql客户端 https://www.quora.com/What-is-the-best-free-DB-schema-design-tool https://www.quora.com/ ...