当前位置: 首页 > news >正文

Laravel + Python 图片水印系统:实现与调试指南

前言

本系统通过 Laravel 作为前端框架接收用户上传的图片,调用 Python 脚本处理水印添加,最终返回处理后的图片。这种架构充分利用了 Laravel 的便捷性和 Python 图像处理库的强大功能。

一、Python 水印处理脚本

from PIL import Image, ImageEnhance
import fitz
import io
import sys
import os
import logging
import traceback# 配置日志记录
logging.basicConfig(level=logging.INFO,format='%(asctime)s - %(levelname)s - %(message)s',filename='watermark_process.log'
)def compress_image(image, quality=85, max_size=(1920, 1920)):"""压缩图片尺寸和质量"""try:if image.size[0] > max_size[0] or image.size[1] > max_size[1]:image.thumbnail(max_size, Image.Resampling.LANCZOS)logging.info(f"图片已压缩至尺寸: {image.size}")return imageexcept Exception as e:logging.error(f"图片压缩失败: {str(e)}")raisedef add_watermark_on_top(image_path, pdf_watermark_path, output_path):"""将水印清晰地覆盖整个图片"""try:logging.info(f"开始处理图片: {image_path}")# 检查文件是否存在if not os.path.exists(image_path):error_msg = f"找不到图片文件: {image_path}"logging.error(error_msg)raise FileNotFoundError(error_msg)if not os.path.exists(pdf_watermark_path):error_msg = f"找不到水印文件: {pdf_watermark_path}"logging.error(error_msg)raise FileNotFoundError(error_msg)# 打开并压缩原始图片base_image = Image.open(image_path)logging.info(f"原始图片格式: {base_image.format}, 尺寸: {base_image.size}")base_image = compress_image(base_image)# 打开PDF水印pdf_doc = fitz.open(pdf_watermark_path)page = pdf_doc[0]logging.info(f"PDF水印已加载,页数: {len(pdf_doc)}")# 使用更高分辨率转换PDF水印mat = fitz.Matrix(8, 8)watermark_pix = page.get_pixmap(matrix=mat, alpha=True)watermark_bytes = watermark_pix.tobytes("png")watermark_image = Image.open(io.BytesIO(watermark_bytes))logging.info(f"水印图片已转换,尺寸: {watermark_image.size}")# 确保图片模式正确if base_image.mode != 'RGBA':base_image = base_image.convert('RGBA')logging.info("原始图片已转换为RGBA模式")if watermark_image.mode != 'RGBA':watermark_image = watermark_image.convert('RGBA')logging.info("水印图片已转换为RGBA模式")# 将水印调整为与原图完全相同的尺寸watermark_image = watermark_image.resize(base_image.size, Image.Resampling.LANCZOS)logging.info(f"水印已调整为图片尺寸: {base_image.size}")# 增强水印的不透明度watermark_data = list(watermark_image.getdata())enhanced_data = []for item in watermark_data:if item[3] > 0:  # 如果像素不是完全透明的enhanced_data.append((item[0], item[1], item[2], min(255, int(item[3] * 2))))else:enhanced_data.append(item)watermark_image.putdata(enhanced_data)logging.info("水印不透明度已增强")# 创建新的图层并合成result = Image.new('RGBA', base_image.size, (0,0,0,0))result.paste(base_image, (0,0))result.paste(watermark_image, (0,0), watermark_image)logging.info("图片与水印已合成")# 确保输出目录存在output_dir = os.path.dirname(output_path)if not os.path.exists(output_dir):os.makedirs(output_dir, exist_ok=True)logging.info(f"创建输出目录: {output_dir}")# 保存结果result.save(output_path, 'PNG', quality=85, optimize=True)logging.info(f"处理后的图片已保存至: {output_path}")# 关闭PDF文档pdf_doc.close()return output_pathexcept Exception as e:error_msg = f"处理过程中出现错误:{str(e)}"logging.error(error_msg)logging.error(traceback.format_exc())  # 记录完整的堆栈信息return Nonedef main():try:logging.info("===== 开始新的水印处理任务 =====")if len(sys.argv) != 4:error_msg = "参数数量错误,使用方法: python add_watermark.py 输入图片路径 水印PDF路径 输出图片路径"logging.error(error_msg)print(error_msg)return 1input_image = sys.argv[1]watermark_pdf = sys.argv[2]output_image = sys.argv[3]logging.info(f"接收到命令行参数: 输入={input_image}, 水印={watermark_pdf}, 输出={output_image}")result = add_watermark_on_top(input_image, watermark_pdf, output_image)if result:success_msg = f"处理成功,输出文件:{result}"logging.info(success_msg)print(success_msg)return 0else:error_msg = "处理失败,详情请查看日志文件"logging.error(error_msg)print(error_msg)return 1except Exception as e:logging.critical(f"程序崩溃: {str(e)}")logging.critical(traceback.format_exc())print(f"程序异常终止: {str(e)}")return 1if __name__ == "__main__":sys.exit(main())

二、Laravel 后端集成

<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;class WatermarkController extends Controller
{/*** 添加水印到图片*/public function addWatermark(Request $request){// 验证请求$request->validate(['image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',]);try {// 存储上传的图片$imagePath = $request->file('image')->store('temp', 'public');$imageFullPath = storage_path('app/public/' . $imagePath);Log::info("图片已上传: {$imageFullPath}");// 水印 PDF 路径$watermarkPdfPath = public_path('watermark.pdf');if (!file_exists($watermarkPdfPath)) {throw new \Exception("水印文件不存在: {$watermarkPdfPath}");}// 准备输出路径$outputFileName = 'watermarked_' . time() . '.' . $request->file('image')->extension();$outputPath = 'temp/' . $outputFileName;$outputFullPath = storage_path('app/public/' . $outputPath);// 执行 Python 脚本$pythonPath = env('PYTHON_PATH', 'python3'); // 配置 Python 路径$scriptPath = base_path('scripts/add_watermark.py');$process = new Process([$pythonPath,$scriptPath,$imageFullPath,$watermarkPdfPath,$outputFullPath]);$process->run();// 检查执行结果if (!$process->isSuccessful()) {Log::error("Python 脚本执行失败: " . $process->getErrorOutput());throw new ProcessFailedException($process);}$output = $process->getOutput();Log::info("Python 脚本输出: {$output}");// 检查输出文件是否存在if (!file_exists($outputFullPath)) {throw new \Exception("处理后的图片不存在,可能 Python 脚本执行失败");}// 返回处理后的图片 URL$imageUrl = Storage::url($outputPath);return response()->json(['success' => true,'message' => '水印添加成功','image_url' => $imageUrl]);} catch (\Exception $e) {Log::error("添加水印失败: " . $e->getMessage());return response()->json(['success' => false,'message' => '处理过程中出错: ' . $e->getMessage()], 500);}}
}

三、环境配置

在 .env 文件中添加 Python 路径配置:

PYTHON_PATH=python3  # 根据实际环境修改

四、错误调试指南

1. Python 脚本调试

日志文件: 查看 watermark_process.log 获取详细处理过程
命令行测试: 直接通过命令行执行 Python 脚本测试

python3 scripts/add_watermark.py /path/to/input.jpg /path/to/watermark.pdf /path/to/output.png

2. Laravel 调试

日志检查: 查看 storage/logs/laravel.log
异常信息: 捕获并记录完整的异常堆栈
权限问题: 确保 storage 目录可写
Python 路径: 确认 .env 中的 PYTHON_PATH 配置正确

3. 常见错误及解决方案

错误信息可能原因解决方案
找不到图片文件文件路径错误或权限不足检查文件路径和权限
Python 脚本执行失败Python 环境问题或依赖缺失确认 Python 路径和依赖(如 PyMuPDF、Pillow)
处理后的图片不存在Python 脚本内部错误查看 Python 日志文件获取详细信息

五、部署注意事项

1、Python 依赖安装:

pip install pillow pymupdf

2、文件权限设置:

chmod -R 755 storage/
chmod -R 755 bootstrap/cache/

3、Nginx 配置:确保上传文件大小限制足够大:

client_max_body_size 20M;

通过以上方案,你可以实现一个完整的 Laravel + Python 图片水印系统,并具备完善的错误调试能力。这种架构既发挥了 Laravel 的 Web 开发优势,又利用了 Python 在图像处理领域的强大生态。

http://www.dtcms.com/a/274391.html

相关文章:

  • 将七大核心理念融入AI Agent设计——构建“数字生命体”的宏伟蓝图
  • 大数据在UI前端的应用创新:基于用户反馈的产品迭代优化系统
  • UI前端大数据可视化实战技巧:如何利用数据故事化提升用户参与度?
  • 前端面试专栏-算法篇:22.树结构(二叉树、B树、红黑树)
  • Web 应用防火墙:守护应用安全的核心屏障
  • webpack高级配置
  • OpenAvatarChat数字人项目结构分析
  • 星云穿越与超光速飞行特效的前端实现原理与实践
  • 第三章 隧道与轨道交通工程 3.8 安全质量控制
  • 排序算法(一):冒泡排序
  • UniApp 生命周期详解:从启动到销毁的完整指南
  • 如何快速掌握WeNet:从零到一的端到端语音识别学习指南
  • SSRF11 各种限制绕过之DNS rebinding 绕过内网 ip 限制
  • FREERTOS根本不能使用连续接收串口思想
  • C语言的程序控制语句
  • 多态 使用场景
  • 【构建Tomcat版本检查工具:自动检测并提醒版本更新】
  • 云、实时、时序数据库混合应用:医疗数据管理的革新与展望(中)
  • 解决Linux绑定失败地址已使用(端口被占用)的问题
  • day050-ansible剧本与变量
  • 云暴露面分析完整指南
  • 2025年7月11日—基础算法—高精度
  • 删除mysql文件夹时显示在另一程序中打开
  • 期权交易完整版教程简介
  • 工具分享--IP与域名提取工具
  • YOLOv13来了!基于超图增强的自适应视觉感知实时目标检测
  • 数据结构第一章复杂度的认识
  • WebSocket 重连与心跳机制:打造坚如磐石的实时连接
  • 005---Xilinx Viivado FIFO (二)---fifo IP核使用总结
  • python之set详谈