图像批量添加数字水印

图像批量添加数字水印

首先准备图像文件,然后把该文件中的图像内容作为数字水印批量添加到 某个文件夹中所有图像文件中,要求水印在目标图像文件中的位置随机选择为左上角、右下角或图像中间,并且水印图像的背景在目标图像中设置为透明。
1.首先下载要添加的水印图像,放入指定路径。
2其次下载添加水印的图像,放入指定路径。
3.然后下载好对应的python库。
4.最后运行下面的代码。
最终形成的图片会放入某个路径,应提前定义好。
下载PIL库,在命令行输入 pip install pillow

from PIL import Image
import random
import os
"""
def addwm(imagepath,k):
    path1="D:/image/watermark/1.png"
    path2="D:/image/img/"+imagepath
    path3="D:/image/finish/"
    wm=Image.open(path1).convert("RGBA")
    img=Image.open(path2).convert("RGBA")
    newwm=wm.resize((300,300))
    newimg=img.resize((450,500))
    width1,height1=newwm.size
    width2,height2=newimg.size
    position=[(0,0),(width2//2-width1//2,height2//2-height1//2),(width2-width1,height2-height1)]
    tuce=Image.new("RGBA",(width2,height2),(0,0,0,0))
    tuce.paste(newwm,random.choice(position))
    out=Image.composite(tuce,newimg,tuce)
    out.save(path3+str(k+1)+".png")
def start():
    path="D:/image/img/"
    images=os.listdir(path)
    withs=[".jpg",".png",".bmp"]
    for i in range(len(images)):
        hname=os.path.splitext(images[i])[1]
        if hname in withs:
            addwm(images[i],i)
            print("图片"+images[i]+"添加水印成功")

if __name__=="__main__":
    start()
"""
def addwm(imagepath,k):
    path1="D:/image/watermark/3.jpg"
    path2="D:/image/img/"
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
0; i < inputSize; ++i) { int weightIndex = i + j * inputSize; inputGradient[i] += gradient * weights[weightIndex]; weightGradient[weightIndex] += gradient * input[i]; } } } private你可以使用Python的Pillow库来批量添加数字水印。以下是一个简单的示例代码: ```python : int inputSize; int outputSize; vector<double> weights; vector<double> biases; }; ``` 4. 定from PIL import Image, ImageDraw, ImageFont import os # 要添加水印的文件夹路径 folder_path = "/path义神经网络的类 ```c++ class NeuralNetwork { public: NeuralNetwork() { // 构建卷积层和/to/folder" # 水印文字 watermark_text = "123" # 水印字体 font = ImageFont.truetype("池化层 convolution1 = new ConvolutionLayer(kImageSize, kImageSize, 1, 5, 16); arial.ttf", 36) # 循环处理文件夹中的每张图片 for filename in os.listdir(folder_path): if filename pooling1 = new PoolingLayer(convolution1->outputWidth, convolution1->outputHeight, convolution1->outputDepth, 2.endswith(".jpg") or filename.endswith(".png"): # 打开图片 image_path = os.path.join(folder_path, filename) ); convolution2 = new ConvolutionLayer(pooling1->outputWidth, pooling1->outputHeight, pooling1->outputDepth, 5, 32); pooling2 = new PoolingLayer(convolution2->outputWidth, convolution2->outputHeight, convolution2-> image = Image.open(image_path) # 添加水印 draw = ImageDraw.Draw(image) draw.text((10, 10outputDepth, 2); // 计算全连接层的输入大小 int fullyConnectedInputSize = pooling2->output), watermark_text, fill=(255, 255, 255), font=font) # 保存图片 new_file_path =Width * pooling2->outputHeight * convolution2->outputDepth; // 构建全连接层 fullyConnected1 = new os.path.join(folder_path, "watermarked_" + filename) image.save(new_file_path) ``` 这个示例代码会将 FullyConnectedLayer(fullyConnectedInputSize, 128); fullyConnected2 = new FullyConnectedLayer(128, kNumClasses); 指定文件夹中的所有jpg和png图片添加数字水印,并保存为新的文件名。你可以根据需要修改水印内容、字体、位置等参数。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值