A small package wich enables the modification of OpenCV or NumPy images using OpenGL pixel shaders written in GLSL.
Project description
Unknown6656.CVGLPixelShader
This is a small package wich enables the modification of OpenCV or NumPy images using OpenGL pixel shaders written in GLSL.
Download / Installation
- Install the package using
pip install Unknown6656.CVGLPixelShader
- Insert the following import statements into your Python code:
from Unknown6656.CVGLPixelShader import * import cv2
Basic Example
Create a new shader by invoking the constructor PixelShader(...)
as follows:
my_shader = PixelShader('''
vec4 color = texture(image, coords);
out_color = vec4(1 - color.xyz, 1);
''')
The code segment passed to the constructor is GLSL code and will be inserted into the fragment shader of the program. Have a look at the official GLSL documentation pages from Khronos for more information on how to use the shader language.
The shader can then be used as follows:
image_in = cv2.imread('.../path/to/image.png') # read the input image from file
image_out = my_shader(image_in) # process the image using the pixel shader
cv2.imwrite('.../path/to/output.png', image_out) # save the processed image
my_shader.close() # close the shader and free all
# underlying resources
You may want to replace a call to PixelShader.close(self)
with the usage of with
-statement blocks:
with PixelShader('''
vec4 color = texture(image, coords);
out_color = vec4(1 - color.xyz, 1);
''') as my_shader:
image_in = cv2.imread('.../path/to/image.png')
image_out = my_shader(image_in)
cv2.imwrite('.../path/to/output.png', image_out)
Using Variables
You may sometimes want to pass variables to the shader. This can be performed as follows:
-
Create a new shader variable:
my_variable = ShaderVariable('time', ShaderVariableType.FLOAT)
This code creates a new variable with the name
'time'
and the typefloat
(32-bit IEE754 floating-point value). Take a look at the enumShaderVariableType
for all supported shader variable types. -
Create a new shader and pass all variables to the shader:
my_shader = PixelShader(''' out_color = texture(image, coords); out_color.x += sin(time); ''', variables = [my_variable])
Note that the GLSL code makes usage of the variable
time
in the lineout_color.x += sin(time);
. -
Assign a value to the variable:
my_shader[my_variable] = time.time()
-
Use the shader:
image_out = my_shader(image_in)
Complete example:
image = cv2.imread('...../image.png')
v_time = ShaderVariable('time', ShaderVariableType.FLOAT)
time = 0
with PixelShader('''
out_color = texture(image, coords);
out_color.x += sin(time * 2) * .5;
''', '', [v_time]) as shader:
while True:
time += .01
shader[v_time] = time
image = shader(image)
cv2.imshow('image', image)
if cv2.waitKey(1) & 0xff == ord('q'):
break
Notes:
Please keep the following in mind:
- This library is still in beta. Some bugs may occur.
- You may declare multiple shaders and use them independently from each other. Please keep in mind that the image processing is still performed synchroniously in the background, meaning that two shaders cannot process an image at exactly the same time (at least on a per-process basis).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for Unknown6656.CVGLPixelShader-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67f138d8df6dd0789de6d802dfa0650e5a30ceff5a00ff213fd638074c0714be |
|
MD5 | b5690170529e38984fcd85406ff5bd79 |
|
BLAKE2b-256 | 67c4ae38e673be0fbdc0393ab22ef6d7d1411fa599684c90c640529c35da968e |
Hashes for Unknown6656.CVGLPixelShader-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96839a951ce505397e8f843a2402d02f24769746b382cc4b7e6e6ddb791af645 |
|
MD5 | b066cffe574d0bb3271663bef9ee6c88 |
|
BLAKE2b-256 | ac2cb982b035c2f5f082d58da1429e3909af1b3fb1501afda2f0608c4d2e5d99 |