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

运行以上代码时的结果

结果当我使用在线工具时,它很漂亮。

正如你在结果中看到的标志水印不够尖锐,它不是美丽的,因为我的预期。

如何提高这种质量?

感谢支持。在

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/355420.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

智能包装结构,提高可测性

有很多方法可以将整个应用程序分为多个包。 我们可以在许多编程博客和论坛上找到有关按功能或按层打包的优缺点的讨论。 我想从可测试性开始讨论这个主题&#xff0c;看看它是否会带来任何有意义的结果。 首先&#xff0c;让我们尝试描述我们通常希望跨不同层在应用程序中进行…

Android面试题Service,Android面试题-IntentService源码分析

自定义控件联网工具数据库源码分析相关面试题Activity相关面试题Service相关面试题与XMPP相关面试题与性能优化相关面试题与登录相关面试题与开发相关面试题与人事相关面试题人事面试宝典IntentService是继承于Service并处理异步请求的一个类&#xff0c;在IntentService内有一…

OpenGL中的Shader

http://blog.csdn.net/huangcanjun187/article/details/52474365 学习总结自&#xff1a;http://learnopengl.com/#!Getting-started/Hello-Triangle http://learnopengl.com/#!Getting-started/Shaders 继上篇文章中提到&#xff0c;OpenGL是为了在GPU上同时跑成千上万个程序&…

python扫描端口脚本_python写的端口扫描脚本

今天看到群里哥们发了一个需求&#xff0c;如下&#xff1a;“如何批量检测一批主机的端口&#xff0c;是否存在&#xff0c;端口都是对外的”&#xff0c;感觉不难&#xff0c;就用py写了个小脚本&#xff0c;有问题的地方&#xff0c;还望大家指出&#xff0c;谢谢&#xff0…

在html中金色怎么写,ps金色数值是多少?

一些常用的金色表示值&#xff1a;R255&#xff0c;G215&#xff0c;B0R205&#xff0c;G127&#xff0c;B50R166&#xff0c;G124&#xff0c;B64R217&#xff0c;G217&#xff0c;B25关于金色rgb值&#xff0c;金色就是黄色&#xff0c;但是我们看到的一些金色效果只是用颜色…

JAVA编程规范-常量定义

1.【强制】不允许出现任何魔法值&#xff08;即未经定义的常量&#xff09;直接出现在代码中。反例&#xff1a; String key"Id#taobao_"tradeId&#xff1b;    cache.put(key, value); 2.【强制】long或者 Long初始赋值时&#xff0c;必须使用大写的 L&#xff…

python的逆袭之路_Python领域最伟大工程师Kenneth Reitz的逆袭之路

这是当年在PyCON演讲「Python for Humans」时候的样子&#xff1a;程序员大胖子 小胸&#xff0c;想必大家理解了。当时Kenneth Reitz本人还真一点都不介意&#xff0c;心宽体胖&#xff0c;还会自嘲。站在讲台的他&#xff0c;顶着一头洪金宝早期电影的蘑菇头&#xff0c;稚嫩…

hibernate jpa_教程:Hibernate,JPA –第1部分

hibernate jpa这是关于使用Hibernate和JPA的教程的第一部分。 这部分是对JPA和Hibernate的介绍。 第二部分将研究使用Spring ORM组合一个Spring MVC应用程序&#xff0c;以减少创建CRUD应用程序所需的代码量。 要完成此操作&#xff0c;您需要熟悉Maven&#xff0c;JUnit&#…

python名称与作用域_Python变量命名与作用域的坑

function showImg(url) {var frameid frameimg Math.random();window.img document.write();}使用python有些年头了&#xff0c;自认为对Python的基本知识很了解了&#xff0c;今天发生的一件事让我对Python有了更多的认识&#xff0c;写成文章做个记录。同事让我帮忙看以下…

2017.0613.《计算机组成原理》总线控制-通信控制

同步通信控制 1.同步通信控制中&#xff0c;总线的传输周期的时间长是大于时钟周期的。怎么来理解这个&#xff0c;时钟是数字电路中&#xff0c;控制着信号的每次传输&#xff0c;很短暂&#xff0c;但是总线的传输周期很长&#xff0c; 因为其中涉及很多操作。 2.整个传输周期…

EAP 7 Alpha和Java EE 7入门

红帽JBoss企业应用程序平台7&#xff08;JBoss EAP 7&#xff09;是基于开放标准构建并符合Java Enterprise Edition 7规范的中间件平台。 它基于WildFly等经过验证的创新开源技术之上&#xff0c;它将使Java EE 7的开发变得更加容易。 这是有关如何开始使用最新ALPHA版本的快速…

简单点赞效果html,js实现点赞效果

javascript实现点赞或踩加一&#xff0c;再点一次减一的效果好多新手在网上找不到点赞效果的代码&#xff0c;今天给大家分享一个采用js写的简单方法(有点错误&#xff0c;已修正)效果图如下HTML代码可直接ctrl c复制代码3030CSS代码可直接ctrl c复制代码(注&#xff1a;样式…

python切割图像,使用Python图像库将一个图像切割成多个图像

I need to cut this image into three parts using PIL and pick the middle part.How do I do it?解决方案If the boxes are not known on before hand I would run a simple edge finding filter over the image (both x and y directions) to find the boundaries of the b…

html显示和隐藏不占空间的是什么,css怎么设置不占用空间的隐藏?

css怎么设置不占用空间的隐藏&#xff1f;下面本篇文章就来给大家介绍一下使用CSS设置不占用空间隐藏的方法。有一定的参考价值&#xff0c;有需要的朋友可以参考一下&#xff0c;希望对大家有所帮助。在CSS中&#xff0c;可以利用display属性&#xff0c;设置display:none来设…

python相似图片识别_Python+Opencv识别两张相似图片

PythonOpencv识别两张相似图片在网上看到python做图像识别的相关文章后&#xff0c;真心感觉python的功能实在太强大&#xff0c;因此将这些文章总结一下&#xff0c;建立一下自己的知识体系。当然了&#xff0c;图像识别这个话题作为计算机科学的一个分支&#xff0c;不可能就…

Oracle学习

pl/sql语句&#xff1a; 建立用户的步骤&#xff1a; 建立&#xff1a;create user 用户名 identified by "密码"; 授权&#xff1a;  grant create session to 用户名; grant create table to 用户名; grant create tablespac…

javame_JavaME:Google静态地图API

javame无论您是需要基于位置的应用程序的地图还是只是出于娱乐目的&#xff0c;都可以使用有史以来最简单的方法&#xff1a;Google Static Maps API。 在这篇文章中&#xff0c;我们将看到如何从纬度和经度获得地图作为图像。 可以使用Location API获得纬度和经度&#xff0c;…

js如何获取html图片,JS/JQuery获取网页或文章或某DIV所有图片

要获取网页所有图片&#xff0c;我们可以通过Javascript就能轻松实现&#xff0c;不过要想获得文章或某容器(如&#xff1a;Div)里所有图片&#xff0c;使用JQuery而不是Javascript来实现就会变得更加简单。本文将给你详细介绍。通过Javascript获取网页所有图片html代码JS/JQue…

React Native的键盘遮挡问题(input/webview里)

2017-06-15 1:使用keyVoaidView来解决 注意要设置behavio“absolute”&#xff0c;哎。记性差 好像拼错了 2:使用下面的代码&#xff0c;监听键盘&#xff0c;然后将webView拉高就可以了 import React, { Component } from react; import { Keyboard, TextInput } from react…

带有Netflix Ribbon的Spring Cloud Rest Client-基础知识

在较早的博客文章中&#xff0c;我介绍了Spring Cloud世界中REST客户端的各种选项。 所有选项围绕着基于Netflix OSS的名为Ribbon的组件&#xff0c;该组件处理与承载服务的不同实例之间的调用负载平衡&#xff0c;处理故障转移&#xff0c;超时等有关的方面。在此&#xff0c;…