脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - python实现水印图片功能

python实现水印图片功能

2022-12-19 15:17月逝天 Python

这篇文章主要为大家详细介绍了python实现水印图片功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在做一些工作的时候,有时候会涉及到给图片加上水印,这个如果手动添加的话,效率太低了,通常选择代码完成。下面这个是给图像添加文字水印(图片水印还在研究中)

比如,在下面的图片中添加 “美团外卖” 水印

python实现水印图片功能

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from PIL import Image,ImageDraw,ImageFont
import numpy as np
import random
import cv2
import re
 
###################################################################################
class Make_Font(object):  #### 设置文字水印
    def __init__(self,image_path,out_path,font,font_size,diaphaneity):
        self.image_path = image_path  ### 读入背景图片
        self.out_path = out_path  ### 输出水印图片
        self.font = font  ### 设置水印字内容
        self.font_size = font_size  ## 设置字体大小
        self.diaphaneity = diaphaneity  ### 设置字体透明度
 
        suffix = self.out_path.split('.')[-1]
        match = re.match(r'png',suffix)
        if not match:
            raise ValueError('The out put file name must be PNG file!')
    def _text_xy(self,image_size):
        width, height = image_size
        x = random.randint(min(0, width), max(0, width))  #### 随机取画文字点
        y = random.randint(min(0, height), max(0, height))
        return x,y
 
    def _draw_font_box(self,image_size,font_size):
        img_w,img_h = image_size
        font_w,font_h = font_size
        all_x = []
        x = 0
        all_x.append(x)
        while x < img_w:
            x = font_w + 50 + x  ####  隔50 画一次文字
            all_x.append(x)
 
        all_y = []
        y = 0
        all_y.append(y)
        while y < img_h:
            y = font_h + 50 + y   ####  隔50 画一次文字
            all_y.append(y)
        return all_x,all_y
 
    def run_make_font(self):
        image = Image.open(self.image_path) ## (598,419)
        image_x,image_y = image.size[0:2] ## (598,419)
        text = self.font
        text_diap = self.diaphaneity   ####  设置字体透明度  越小越透明 (0,100)
        font = ImageFont.truetype('1.ttf',self.font_size) ## 设置字体和大小
        layer = image.convert('RGBA')  ## 转换图像格式:A为透明度   尺寸(598, 419)
 
        max_size = max(image_x,image_y)
        text_overlayer = Image.new('RGBA',(2*max_size,2*max_size),(255,255,255,0))  ## 生成同等大小的透明图片
 
        image_draw = ImageDraw.Draw(text_overlayer) ## 画图
        text_size_x,text_size_y = image_draw.textsize(text,font = font)  ## 获取文本大小
        #print(text_size_x,text_size_y)   ### 字体大小 (250,50)
        x_count,y_count = self._draw_font_box(text_overlayer.size,(text_size_x,text_size_y))
        for i in x_count:
            for j in y_count:
                #text_x,text_y = text_xy((image_x,image_y)) ## 设置文本位置
                image_draw.text((int(i),int(j)),text,font=font,fill=(255,255,255,text_diap)) ## 设置文本颜色和透明度
        text_overlayer = text_overlayer.rotate(45)   # 设置逆时针旋转45度
        #######  设置切割点  ##############
        box_x = (text_overlayer.size[0]-image_x)/2
        box_y = (text_overlayer.size[1]-image_y)/2
        box = [box_x,box_y,box_x+image_x,box_y+image_y]
        new_img = text_overlayer.crop(box)
        new_img = new_img.resize(layer.size)
        #text_overlayer.save('text_overlayer_after.png')  ## 生成的水印png图片
        #new_img.save('new_img.png')  ## 生成的水印png图片
        after = Image.alpha_composite(layer,new_img)  ## (im1,im2)将im2复合到im1上,返回一个Image对象
        after.save(self.out_path)  ### .png 可以直接保存RGBA格式
 
#########################################################################################
 
if __name__=="__main__":
    ############### 文字水印  ########################
    MK = Make_Font(image_path='./without_water/test5.jpg',out_path='test5_after.png',font = '美团外卖',font_size = 30,diaphaneity = 90)
    MK.run_make_font()
    ##################################################

这段代码主要完成的是,将特定的文字水印添加图像中,文字内容、文字尺寸、文字透明度都可以调节(image_path为原图像,out_path为输出的水印图像)

效果图如下:

注:上传图像的时候,图像进行了压缩。

python实现水印图片功能

本来准备再写一个生成图像Logo的水印的代码,可惜一直没达到自己预期。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/yueshitian/article/details/100775963

延伸 · 阅读

精彩推荐
  • PythonPython re模块介绍

    Python re模块介绍

    这篇文章主要介绍了Python re模块介绍,需要的朋友可以参考下 ...

    脚本之家3872020-05-16
  • PythonPython装饰器详情

    Python装饰器详情

    这篇文章主要介绍了Python装饰器,装饰器Decorator从字面上理解,就是装饰对象的器件,其的特点是特点是函数是作为其参数出现的,装饰器还拥有闭包的特点...

    一碗周11522022-02-23
  • PythonPython代码实现KNN算法

    Python代码实现KNN算法

    这篇文章主要为大家详细介绍了Python代码实现KNN算法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Troublemaker2014073732020-12-27
  • PythonPython list列表中删除多个重复元素操作示例

    Python list列表中删除多个重复元素操作示例

    这篇文章主要介绍了Python list列表中删除多个重复元素操作,结合实例形式分析了Python删除list列表重复元素的相关操作技巧与注意事项,需要的朋友可以参考...

    tomato_guo7592021-06-03
  • Pythonpycharm2021激活码使用教程(永久激活亲测可用)

    pycharm2021激活码使用教程(永久激活亲测可用)

    pycharm2021激活码是一个可以轻松帮助用户免费激活pycharm2021.1软件的文件,虽然说pycharm现在只是推出了2021.1的EAP版,但是如果你想先率先体验一波,那么就可...

    shangke40232021-09-29
  • PythonPython Opencv实现最强美颜滤镜效果

    Python Opencv实现最强美颜滤镜效果

    这篇文章主要介绍了如何利用Python OpenCV制作一个强大的美颜滤镜效果,文中的示例代码讲解详细,感兴趣的小伙伴可以学习一下...

    木木子学python3722022-11-13
  • PythonPytorch保存模型用于测试和用于继续训练的区别详解

    Pytorch保存模型用于测试和用于继续训练的区别详解

    今天小编就为大家分享一篇Pytorch保存模型用于测试和用于继续训练的区别详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    鱼丸粗面23311222020-04-28
  • Pythonpython中xlrd模块的使用详解

    python中xlrd模块的使用详解

    这篇文章主要介绍了python中xlrd模块的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随...

    宣小飞11392021-08-31