图片藏字方案介绍

部署运行你感兴趣的模型镜像

在Ubuntu安装库

sudo apt-get install libopencv-dev

生成字符图片,可以通过PS生成,身为程序员,尝试通过python生成,更加灵活方便

from PIL import Image, ImageDraw, ImageFont

def create_text_image(text, font_path, font_size, image_size, output_path):
    # 创建一个白色背景的图像
    image = Image.new('RGB', image_size, 'white')
    draw = ImageDraw.Draw(image)

    # 加载自定义字体
    font = ImageFont.truetype(font_path, font_size)

    # 使用 ImageDraw 的 textsize 方法获取文本尺寸
    text_width, text_height = draw.textsize(text, font=font)

    # 计算文本居中位置
    text_x = (image_size[0] - text_width) // 2
    text_y = (image_size[1] - text_height) // 2

    # 绘制黑色文字
    draw.text((text_x, text_y), text, font=font, fill='black')

    # 保存图像
    image.save(output_path)
# 示例用法
create_text_image("我钟意你", "STXINGKA.TTF", 200, (800, 200), "output.png")

最终的c++代码

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv) {
    if (argc != 7) {
        cout << "Usage: " << argv[0] << " <source_image_path> <watermark_image_path> <output_image_path> <watermark_scale> <watermark_spacing> <scale_factor>" << endl;
        return 1;
    }

    // 加载原始图片和水印图片
    Mat src = imread(argv[1], IMREAD_COLOR);
    Mat watermark = imread(argv[2], IMREAD_COLOR);

    if (src.empty() || watermark.empty()) {
        cerr << "Error loading images." << endl;
        return 1;
    }

    float watermarkScale = stof(argv[4]);
    int watermarkSpacing = stoi(argv[5]);
    float scaleFactor = stof(argv[6]);

    // 放大原始图片
    Mat scaledSrc;
    resize(src, scaledSrc, Size(), scaleFactor, scaleFactor, INTER_LANCZOS4);

    // 缩放水印图片
    Mat scaledWatermark;
    resize(watermark, scaledWatermark, Size(), watermarkScale, watermarkScale, INTER_LINEAR);

    // 重复放置水印
    for (int y = 0; y < scaledSrc.rows; y += scaledWatermark.rows + watermarkSpacing) {
        for (int x = 0; x < scaledSrc.cols; x += scaledWatermark.cols + watermarkSpacing) {
            for (int wy = 0; wy < scaledWatermark.rows && (y + wy) < scaledSrc.rows; ++wy) {
                for (int wx = 0; wx < scaledWatermark.cols && (x + wx) < scaledSrc.cols; ++wx) {
                    Vec3b& srcPixel = scaledSrc.at<Vec3b>(y + wy, x + wx);
                    Vec3b& watermarkPixel = scaledWatermark.at<Vec3b>(wy, wx);
                    
                    // 混合水印和原始图片
                    for (int c = 0; c < scaledSrc.channels(); ++c) {
                        srcPixel[c] = saturate_cast<unsigned char>((1.0 - 0.1) * srcPixel[c] + 0.1 * watermarkPixel[c]);  // 10%透明度的水印
                    }
                }
            }
        }
    }

    // 保存输出图像
    if (!imwrite(argv[3], scaledSrc)) {
        cerr << "Failed to save the output image." << endl;
        return 1;
    }

    cout << "Watermark added successfully with scale " << watermarkScale << " and spacing " << watermarkSpacing << " on an enlarged image." << endl;

    return 0;
}
./main test.jpg test_img.png out.png 1 1000 20
CC = g++
# CFLAGS = -Wall -g

all: main

main: main.cpp
	g++ -o main main.cpp -I/usr/include/opencv4 -L/usr/lib/x86_64-linux-gnu -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs
	
clean:
	rm -f main

您可能感兴趣的与本文相关的镜像

Python3.11

Python3.11

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值