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)
效果图:
----->
有两种用法:
1. 使用.cube文件
滤镜(.cube)文件格式如下: 里面的值是0~1之间的
# Created by Adobe Lightroom plugin Export LUT (1.17.0) LUT_3D_SIZE 32 DOMAIN_MIN 0.0 0.0 0.0 DOMAIN_MAX 1.0 1.0 1.0 0.000000 0.000000 0.000000 0.047791 0.000000 0.000000 0.080140 0.000000 0.000000 0.118013 0.000000 0.000000 0.169955 0.000000 0.000000 ...
输入的图片也要归一化到0~1之间, 最后输出的时候要重新放大到0~255
2. 使用一个numpy的数组格试的滤镜文件
格式是3,32,32,32这样的数组, .cube转换到npy的代码如下:
def load_lut_file_to_input_cube(cube_path,dim=None): with open(cube_path,'r') as f: lines = f.readlines() for i in range(len(lines)): lines[i] = lines[i].strip() if dim is None: if 'LUT_3D_SIZE' in lines[i]: dim = int(lines[i].split(' ')[-1]) lines = lines[-dim*dim*dim:] cube = np.zeros((3,dim,dim,dim),dtype=np.float32) for i in range(0,dim): for j in range(0,dim): for k in range(0,dim): n = i * dim*dim + j * dim + k line = lines[n].split(' ') x = line try: cube[0,i,j,k] = float(x[0]) # r cube[1,i,j,k] = float(x[1]) # g cube[2,i,j,k] = float(x[2]) # b except Exception: print(lines[n]) cube = np.array(cube,dtype=np.float32) return cube