You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求GIMP script-fu镜头畸变处理的ImageMagick等效实现方案

镜头畸变校正:替代GIMP脚本的Python内实现方案

我已编写了一个GIMP Script-Fu函数,通过lens-distortion插件去除图像镜头畸变,目前在主Python脚本中通过命令行调用该脚本。现寻求是否存在等效的ImageMagick函数(或其他Python库),无需脱离Python脚本即可实现相同功能。

GIMP脚本代码(lens-distortion.scm)

(define (lens-distortion filename destination)
  (let* (
    (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))  ; 加载图像
    (drawable (car (gimp-image-flatten image)))

    (offset-x -3.51)
    (offset-y -9.36)
    (main-adjust 28.07)
    (edge-adjust 0)
    (rescale -100)
    (brighten 0.58)
  )

  (gimp-message (string-append "processing-" filename))
  (plug-in-lens-distortion RUN-NONINTERACTIVE image drawable offset-x offset-y main-adjust edge-adjust rescale brighten)
  (gimp-file-save RUN-NONINTERACTIVE image drawable destination destination)
  (gimp-image-delete image)
 )
)

示例输入图像

该图像的畸变中心并非图像中心,提升了校正难度:
示例图像


替代实现方案

1. 使用ImageMagick的Python绑定(Wand库)

Wand是ImageMagick的Python原生绑定,可直接在Python脚本内完成畸变校正,无需调用外部命令行。先安装依赖:pip install wand

映射GIMP参数的示例代码:

from wand.image import Image

def lens_distortion_with_wand(input_path, output_path):
    offset_x = -3.51
    offset_y = -9.36
    main_adjust = 28.07
    edge_adjust = 0
    rescale = -100
    brighten = 0.58
    
    with Image(filename=input_path) as img:
        # 计算畸变中心:基于相对图像中心的偏移百分比
        img_center_x = img.width * 0.5
        img_center_y = img.height * 0.5
        distort_center_x = img_center_x + img.width * (offset_x / 100)
        distort_center_y = img_center_y + img.height * (offset_y / 100)
        
        # 适配ImageMagick的畸变系数范围
        k1 = main_adjust / 1000
        k2 = edge_adjust / 1000
        
        # 应用镜头畸变校正
        img.distort('lenscorrection', [distort_center_x, distort_center_y, k1, k2])
        
        # 处理缩放:对应GIMP的rescale参数
        scale_factor = 1 + (rescale / 1000)
        img.scale(int(img.width * scale_factor), int(img.height * scale_factor))
        
        # 调整亮度:将brighten转换为百分比增量
        img.modulate(brightness=int(100 + brighten*100))
        
        img.save(filename=output_path)

2. 使用OpenCV库

OpenCV提供成熟的镜头畸变校正API,适合Python环境直接处理。安装依赖:pip install opencv-python

示例代码:

import cv2
import numpy as np

def lens_distortion_with_opencv(input_path, output_path):
    img = cv2.imread(input_path)
    h, w = img.shape[:2]
    
    # 构造相机矩阵:基于畸变中心偏移
    img_center_x = w * 0.5
    img_center_y = h * 0.5
    distort_center_x = img_center_x + w * (-3.51 / 100)
    distort_center_y = img_center_y + h * (-9.36 / 100)
    
    camera_matrix = np.array([
        [w, 0, distort_center_x],
        [0, h, distort_center_y],
        [0, 0, 1]
    ], dtype=np.float32)
    
    # 畸变系数:映射GIMP的main-adjust和edge-adjust
    distortion_coeffs = np.array([28.07/1000, 0, 0, 0, 0], dtype=np.float32)
    
    # 执行畸变校正
    new_camera_matrix, _ = cv2.getOptimalNewCameraMatrix(camera_matrix, distortion_coeffs, (w, h), 0)
    undistorted_img = cv2.undistort(img, camera_matrix, distortion_coeffs, None, new_camera_matrix)
    
    # 处理缩放
    scale_factor = 1 + (-100 / 1000)
    undistorted_img = cv2.resize(undistorted_img, None, fx=scale_factor, fy=scale_factor)
    
    # 调整亮度
    undistorted_img = cv2.convertScaleAbs(undistorted_img, beta=0.58*255)
    
    cv2.imwrite(output_path, undistorted_img)

注意事项

  • 参数映射需根据实际效果微调:不同工具对畸变参数的定义(单位、系数范围)存在差异,建议先用小图测试,调整系数至匹配GIMP脚本的效果。
  • 若需完全复刻GIMP插件逻辑,可查阅该插件的源码,确保参数转换的准确性。

内容的提问来源于stack exchange,提问作者DhiwaTdG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 23:44:57