Python 通过 .cube LUT 文件对图像加滤镜
一个好用的python给图片加滤镜的代码:
https://github.com/CKboss/PyApplyLUT
这个是对C++代码的封装, 并用上了openmp来并行处理, 速度很快, 4k图片加滤镜在本地测试也只要不到0.2秒.
需要编译一下. 依赖pybind11和eigen. 好在这两个库都是只包含头文件就能用的那种. 到官网下好源码(pybind11 2.7.1, eigen 3.4), 在CMakeLists中指明pybind11和eigen的路径, 编译一下即可.
得到.so文件后, 需要把它放到python能找到的地方. 这里就直接把路径写死了.
用法如下:
1 import cv2 2 import numpy as np 3 from pathlib2 import Path 4 5 import sys 6 # the path of .so where python can find it 7 sys.path.append("Q:/WorkSpace/bfood/lut-master/build/Debug") 8 from python.PyApplyLUT import PyApplyLUT 9 from python.lut_tools import cube_to_npy 10 11 INPUT_IMG = Path(r".\test\1.jpg") 12 LUT_FILE = Path(r".\test\1.cube") 13 14 # normlizer the input picture to 0~1 15 img = cv2.imread(INPUT_IMG.as_posix()) 16 img = img / 255 17 18 # apply lut 19 20 # method 1 load lut from a .cube file 21 alut = PyApplyLUT(lut_file=LUT_FILE) 22 new_img = alut.apply_lut(img) 23 # recover to 0~255 24 new_img = new_img * 255 25 cv2.imwrite("./test/new_img_1.jpg",new_img) 26 27 # method 2 load lut from the np array 28 cubenpy = cube_to_npy(LUT_FILE) 29 alut = PyApplyLUT(lut_dim=32, lut_cube=cubenpy) 30 new_img = alut.apply_lut(img) 31 # recover to 0~255 32 new_img = new_img * 255 33 cv2.imwrite("./test/new_img_2.jpg",new_img)