前言
图片加水印有什么好处?在现今的数字化时代,网络上的图片泛滥,盗图现象也越来越严重。因此,在发布文章时,为了保护自己的原创作品版权,很多人选择使用水印来保护他们的图片。这样就能更好地做到:
1.版权保护:在商业用途的照片中添加水印可以帮助保护作者的版权,防止他人未经授权使用照片。
2.品牌推广:将商业品牌、商标或公司标志添加到照片中,可以帮助提高品牌知名度和曝光率。
3.防止盗版:添加水印可以防止盗版和未经授权的使用,因为水印会明显表明该照片的版权归原作者所有。
4.标识来源:在社交媒体平台上分享照片时,添加水印可以帮助其他用户识别出照片的来源和作者。
5.保护隐私:在一些情况下,添加水印可以帮助保护照片中的个人隐私,特别是在共享照片时,可能会有人试图使用照片来滥用或伤害个人隐私。
当然防止滥用是最主要的,我是这么觉得、。
PIL的Image模块的基本介绍
Pillow 是 Python 中较为基础的图像处理库,主要用于图像的基本处理,比如裁剪图像、调整图像大小和图像颜色处理等。与 Pillow 相比,OpenCV 和 Scikit-image 的功能更为丰富,所以使用起来也更为复杂,主要应用于机器视觉、图像分析等领域,比如众所周知的“人脸识别”应用 。
Image模块:
Image模块是PIL最基本的模块,其中导出了Image类,一个Image类实例对象就对应了一副图像。同时,Image模块还提供了很多有用的函数。
这里只是初步学习了一些用法与实际操作。
| |
---|
| |
基础函数 简介 | |
open() 打开图片 | |
save(“test.gif”,“GIF”) 保存(新图片路径和名称,保存格式) | |
show() 显示图片 | |
size 是img图片对象的成员数据,是一个元组,包含了像素宽度和像素高度 | |
Image.new() 生成新的图片 | |
getpixel((1,1)) 获取像素点(1,1)三通道信息 | |
putpixel() 设置像素点三通道信息 | |
Python代码实现文字水印
下面咱们用Python代码讲解下怎么批量给图片添加水印,并用代码实现。
这里使用.png图片格式,
'''
如何批量为图片添加文字水印
'''
from PIL import Image, ImageDraw, ImageFont,ImageEnhance
path=input('请输入要添加水印的图片所在路径:')
text=input('请输入水印文字:')
positionflag=int(input('请输入水印位置(0:左上角,1:左下角,2:右上角,3:右下角,4:居中):'))
alphavalue=float(input('请输入水印透明度(范围在0——1之间的1位小数):'))
font = ImageFont.truetype(r'simkai.ttf', 24)
def textMark(img):
try:
im = Image.open(img).convert('RGBA')
newImg = Image.new('RGBA', im.size, (255, 255, 255, 0))
imagedraw = ImageDraw.Draw(newImg)
imgwidth, imgheight = im.size
txtwidth=font.getsize(text)[0]
txtheight = font.getsize(text)[1]
if positionflag == 0:
position=(0,0)
elif positionflag == 1:
position=(0,imgheight - txtheight)
elif positionflag == 2:
position=(imgwidth - txtwidth,0)
elif positionflag == 3:
position=(imgwidth - txtwidth, imgheight - txtheight)
elif positionflag == 4:
position=(imgwidth/2,imgheight/2)
imagedraw.text(position, text, font=font, fill="red")
alpha = newImg.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(alphavalue)
newImg.putalpha(alpha)
Image.alpha_composite(im, newImg).save(img,'png')
except Exception as e:
print(e)
import os
try:
list = os.listdir(path)
for i in range(0, len(list)):
filepath = os.path.join(path, list[i])
if os.path.isfile(filepath):
filetype = os.path.splitext(filepath)[1]
if filetype == '.png':
textMark(filepath)
print('批量添加水印完成')
except:
print('请输入一个有效路径……')
左上角添加水印,图例展示:
Python代码实现图片水印
from PIL import Image
path=input('请输入要添加水印的图片所在路径:')
imgpath=input('请输入要作为水印图片的路径:')
positionflag=int(input('请输入水印位置(0:左上角,1:左下角,2:右上角,3:右下角,4:居中):'))
alphavalue=int(input('请输入水印透明度(范围在1——10之间):'))
def imgMark(img):
im = Image.open(img)
mark = Image.open(imgpath)
rgbaim = im.convert('RGBA')
rgbamark = mark.convert('RGBA')
imgwidth, imgheight = rgbaim.size
nimgwidth, nimgheight = rgbamark.size
scale = 10
markscale = max(imgwidth / (scale * nimgwidth), imgheight / (scale * nimgheight))
newsize = (int(nimgwidth * markscale), int(nimgheight * markscale))
rgbamark = rgbamark.resize(newsize, resample=Image.ANTIALIAS)
nimgwidth, nimgheight = rgbamark.size
if positionflag == 0:
position = (0, 0)
elif positionflag == 1:
position = (0, imgheight - nimgheight)
elif positionflag == 2:
position = (imgwidth - nimgwidth, 0)
elif positionflag == 3:
position = (imgwidth - nimgwidth, imgheight - nimgheight)
elif positionflag == 4:
position = (int(imgwidth / 2), int(imgheight / 2))
rgbamarkpha = rgbamark.convert("L").point(lambda x: x/alphavalue)
rgbamark.putalpha(rgbamarkpha)
rgbaim.paste(rgbamark, position, rgbamarkpha)
rgbaim.save(img)
import os
try:
list = os.listdir(path)
for i in range(0, len(list)):
filepath = os.path.join(path, list[i])
if os.path.isfile(filepath):
filetype = os.path.splitext(filepath)[1]
if filetype == '.png':
imgMark(filepath)
print('批量添加水印完成')
except Exception as e:
print(e)
左下角添加图片水印,图例展示:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)