python水印 resized_如何改进Python中的水印图像?
我正在使用python为来自this的水印图像源代码import Image
import ImageEnhance
import random
def _percent(var):
"""
Just a simple interface to the _val function with a more meaningful name.
"""
return _val(var, True)
def _int(var):
"""
Just a simple interface to the _val function with a more meaningful name.
"""
return _val(var)
def _val(var, is_percent=False):
"""
Tries to determine the appropriate value of a particular variable that is
passed in. If the value is supposed to be a percentage, a whole integer
will be sought after and then turned into a floating point number between
0 and 1. If the value is supposed to be an integer, the variable is cast
into an integer.
"""
try:
if is_percent:
var = float(int(var.strip('%')) / 100.0)
else:
var = int(var)
except ValueError:
raise ValueError('invalid watermark parameter: ' + var)
return var
def reduce_opacity(img, opacity):
"""
Returns an image with reduced opacity.
"""
assert opacity >= 0 and opacity <= 1
if img.mode != 'RGBA':
img = img.convert('RGBA')
else:
img = img.copy()
alpha = img.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
img.putalpha(alpha)
return img
def determine_scale(scale, img, mark):
"""
Scales an image using a specified ratio or 'F'. If `scale` is 'F', the
image is scaled to be as big as possible to fit in `img` without falling off
the edges. Returns the scaled `mark`.
"""
if scale:
try:
scale = float(scale)
except (ValueError, TypeError):
pass
if type(scale) in (str, unicode) and scale.lower() == 'f':
# scale, but preserve the aspect ratio
scale = min(
float(img.size[0]) / mark.size[0],
float(img.size[1]) / mark.size[1]
)
elif type(scale) not in (float, int):
raise ValueError(
'Invalid scale value "%s"! Valid values are 1) "F" for ratio-preserving scaling and 2) floating-point numbers and integers greater than 0.' % (
scale,))
# determine the new width and height
w = int(mark.size[0] * float(scale)) / 2
h = int(mark.size[1] * float(scale)) / 2
print w, h
# apply the new width and height, and return the new `mark`
return (w, h)
else:
print 'Mark Size', mark.size
return mark.size
def determine_rotation(rotation, mark):
"""
Determines the number of degrees to rotate the watermark image.
"""
if (isinstance(rotation, str) or isinstance(rotation, unicode)) \
and rotation.lower() == 'r':
rotation = random.randint(0, 359)
else:
rotation = _int(rotation)
return rotation
def determine_position(position, img, mark):
"""
Options:
TL: top-left
TR: top-right
BR: bottom-right
BL: bottom-left
C: centered
R: random
X%xY%: relative positioning on both the X and Y axes
X%xY: relative positioning on the X axis and absolute positioning on the
Y axis
XxY%: absolute positioning on the X axis and relative positioning on the
Y axis
XxY: absolute positioning on both the X and Y axes
"""
max_left = max(img.size[0] - mark.size[0], 0)
max_top = max(img.size[1] - mark.size[1], 0)
if not position:
position = 'r'
if isinstance(position, tuple):
left, top = position
elif isinstance(position, str) or isinstance(position, unicode):
position = position.lower()
# corner positioning
if position in ['tl', 'tr', 'br', 'bl']:
if 't' in position:
top = 0
elif 'b' in position:
top = max_top
if 'l' in position:
left = 0
elif 'r' in position:
left = max_left
# center positioning
elif position == 'c':
left = int(max_left / 2)
top = int(max_top / 2)
# random positioning
elif position == 'r':
left = random.randint(0, max_left)
top = random.randint(0, max_top)
# relative or absolute positioning
elif 'x' in position:
left, top = position.split('x')
if '%' in left:
left = max_left * _percent(left)
else:
left = _int(left)
if '%' in top:
top = max_top * _percent(top)
else:
top = _int(top)
print 'top left', left, top
return (left, top)
def watermark(img, mark, position=(0, 0), opacity=1, scale=1.0, tile=False,
greyscale=False, rotation=0, return_name=False, **kwargs):
"""
Adds a watermark to an image.
"""
if opacity < 1:
mark = reduce_opacity(mark, opacity)
if type(scale) != tuple:
scale = determine_scale(scale, img, mark)
print 'mark mode', mark.mode
mark = mark.resize(scale)
if greyscale and mark.mode != 'LA':
mark = mark.convert('LA')
rotation = determine_rotation(rotation, mark)
if rotation != 0:
# give some leeway for rotation overlapping
new_w = mark.size[0]
new_h = mark.size[1]
new_mark = Image.new('RGBA', (new_w, new_h), (0, 0, 0, 0))
# new_mark.putalpha()
# center the watermark in the newly resized image
new_l = (new_w - mark.size[0]) / 2
new_t = (new_h - mark.size[1]) / 2
new_mark.paste(mark, (new_l, new_t))
mark = new_mark.rotate(rotation, Image.BICUBIC, expand=True)
position = determine_position(position, img, mark)
print 'image mode', img.mode
print 'mark mode', mark.mode
if img.mode != 'RGBA':
img = img.convert('RGBA')
print 'image ---', img.mode
alpha = img.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
img.putalpha(alpha)
# make sure we have a tuple for a position now
assert isinstance(position, tuple), 'Invalid position "%s"!' % position
# create a transparent layer the size of the image and draw the
# watermark in that layer.
layer = Image.new('RGBA', img.size, (0, 0, 0, 0))
if tile:
first_y = position[1] % mark.size[1] - mark.size[1]
first_x = position[0] % mark.size[0] - mark.size[0]
for y in range(first_y, img.size[1], mark.size[1]):
for x in range(first_x, img.size[0], mark.size[0]):
layer.paste(mark, (x, y))
else:
layer.paste(mark, position)
# composite the watermark with the layer
return Image.composite(layer, img, layer)
def test():
im = Image.open('/home/chanhle/0450034_a (4th copy).jpg')
mark = Image.open('/home/chanhle/UMA LOGO.png')
# im.save('/home/chanhle/0120047_a.png')
watermark(im, mark,
position='C',
opacity=0.8,
scale='f',
rotation=45).save('/home/chanhle/test3.jpg', 'JPEG')
if __name__ == '__main__':
test()
这是我要加水印的图像
这是logo
运行以上代码时的结果
结果当我使用在线工具时,它很漂亮。
正如你在结果中看到的标志水印不够尖锐,它不是美丽的,因为我的预期。
如何提高这种质量?
感谢支持。在
python水印 resized_如何改进Python中的水印图像?相关推荐
- python双重直方图_Python 2.x中两幅图像的直方图匹配?
我以前写过一个答案here解释如何在图像直方图上进行分段线性插值,以实现高光/中音/阴影的特定比率. 两幅图像之间histogram matching的基本原理相同.基本上,计算源图像和模板图像的累积 ...
- html中水印影响文字复制,HTML中文字水印的清除方法_HTML教程
HTML中文字水印的清除方法 我们在浏览某些网站的时候,看到不错的网页可能就想把上面的内容给复制下来,但是有的网站就是为了防止别人复制,而在每段文字的结尾处增加了干扰码--与背景颜色相同的文字.清除文 ...
- python实现文字游戏_改进Python文字小游戏(4)
前面,我们讲了变量和字符串的一些种类.朋友们,还记得我们之前的那个智障的文字小游戏吗?今天,我们要做的第一件事就是来改进我们的文字小游戏,至于第二件事嘛,先让我卖个关子,众位客官,注意咯! 1)首先, ...
- python canvas画移动物体_canvas中绘制的图像怎么让它移动?
一定要用canvas吗? 如果一定要用canvas,可以和大多交互性的原始画布一样,使用一个渲染循环(rendering loop),例如使用requestAnimationFrame,里面根据条件对 ...
- js添加网页水印和three.js场景中加水印
我们在日常网页开发的时候,可能想给自己的网页或者canvas里面添加水印,增添个人标记,我这里分为普通静态html页面和threejs中3d场景里面添加水印功能. 一 静态html页面添加水印 你只需 ...
- 怎么去除PDF中的水印
很多的PDF文件都有添加水印,但是我们使用文件的时候水印会影响我们对文件的阅读与编辑,那么我们需要把水印去除,水印要如何去除呢?方法是什么呢?就跟小编一起来看看下面的文章了解一下吧! 方法一:迅捷PD ...
- Word格式处理控件Aspose.Words for .NET水印处理教程——如何添加和删除水印
Aspose.Words For .NET是一种高级Word文档处理API,用于执行各种文档管理和操作任务.API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsof ...
- python如何最适合web开发中的人工智能?
无论是初创公司还是跨国公司,Python都为每个人提供了一个很好的好处列表.它是最受欢迎和功能强大的高级编程语言,在2018年获得了极大的普及. 它的日益普及使得它能够进入一些最流行和最复杂的过程,如 ...
- python好还是c+-嵌入式系统中,Python与C/C++哪方更为适用?
[51CTO.com快译]长久以来,C/C++一直编译着嵌入式系统编程领域,但二者亦拥有自己的缺陷.相比之下,Python则成为嵌入式系统中的另一大***语言选项.在今天的文章中,我们将共同探讨双方的 ...
最新文章
- Spring整合ActiveMQ接收消息
- CF-1023F.Mobile Phone Network(并查集缩点)
- Android 音频录制和播放问题
- php-fpm linux 权限,nginx/php-fpm及网站目录的权限设置
- flask基础之jinja2模板-语法定义
- 干货丨总结5类面试官特点和应对方法
- dos进入mysql不记得密码_windos mysql 忘记密码,无密码登录,重新登录
- Visio主题与样式
- 全球排名前50网站使用的开发语言及系统平台
- 【交通流预测】基于matlab小波神经网络短时交通流预测【含Matlab源码 400期】
- Linux环境下如何使用 ffmpeg 处理音视频的合成问题
- 绝对经典英语学习资料
- AdventureWorks DW事例数据库下载
- 火山图——直观的特征差异可视化
- 陶瓷PCB电路板生产工艺
- php 硬盘序号_关于取硬盘序列号
- 51单片机的音乐盒设计
- php laravel 开发工具,Laravel 文档工具
- 定义一个xml文件,保存班级信息
- Mysql 获取成绩排序后的名次
热门文章
- [渝粤教育] 盐城工学院 无机及分析化学C 参考 资料
- 【渝粤教育】国家开放大学2018年秋季 0314-21T兽医基础 参考试题
- 【渝粤教育】国家开放大学2018年春季 0034-22T现代管理原理 参考试题
- [渝粤教育] 西南科技大学 财务管理与分析 在线考试复习资料
- 【渝粤题库】国家开放大学2021春2508学前儿童语言教育题目
- 物联网设备的互操作性问题探讨
- excel数据命令导入mysql_如何将EXCEL数据导入MYSQL
- ie浏览器升级_IE 被微软专家从浏览器里除名了,专家说它只是个兼容性解决方案...
- java中对象字节数_JAVA中求解对象所占字节大小
- python变量持久化_Python 数据持久化:JSON