Skip to content

Commit

Permalink
Add syntax highlighting to README
Browse files Browse the repository at this point in the history
  • Loading branch information
cahidenes committed Oct 18, 2021
1 parent ba48687 commit 83b0688
Showing 1 changed file with 85 additions and 81 deletions.
166 changes: 85 additions & 81 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ Change variables during runtime using simple GUI.
## Usage
Here is a minimalist example

from dynamic_variables import VariableTweaker
import time

vt = VariableTweaker()
vt.add_slider('var_name', 0, 0, 10, 0.1)
vt.init_gui()
```python
from dynamic_variables import VariableTweaker
import time

while True:
print(vt.var_name)
time.sleep(0.1)
vt = VariableTweaker()
vt.add_slider('var_name', 0, 0, 10, 0.1)
vt.init_gui()

while True:
print(vt.var_name)
time.sleep(0.1)
```

When this program run, a slider appears and `vt.var_name` changes according to this slider.

Expand All @@ -38,94 +40,96 @@ There are 5 widgets that you can add to GUI. They allow manipulating different t
#### Slider
Slider widget allows you to change `int` or `float` variables easily.

add_slider(variable_name, initial_value, min_value, max_value, step_value)

```python
add_slider(variable_name, initial_value, min_value, max_value, step_value)
```
Example:

vt.add_slider('slider', 5, 0, 10, 0.1)

```python
vt.add_slider('slider', 5, 0, 10, 0.1)
```
#### Text
Text widget allows you to change `str` variables.

add_text(variable_name, initial_value)

```python
add_text(variable_name, initial_value)
```
Example:
vt.add_text('text', 'this is a text')

```python
vt.add_text('text', 'this is a text')
```
#### Dropdown
Dropdown widget allows you to change your variable to any predetermined value.

add_dropdown(variable_name, initial_value, list_or_tuple_of_options)

```python
add_dropdown(variable_name, initial_value, list_or_tuple_of_options)
```
Example:

vt.add_text('dropdown, 'option 1', ['option 1', 'option 2', 'option 3'])

```python
vt.add_text('dropdown', 'option 1', ['option 1', 'option 2', 3, 4.5])
```
#### Boolean
Boolean widget allows you to change your `bool` variable

add_boolean(variable_name, initial_value)

```python
add_boolean(variable_name, initial_value)
```
Example:

vt.add_boolean('boolean', True)

```python
vt.add_boolean('boolean', True)
```
#### Color
Color widget allows you to pick colors easily. When clicked on the color, a color picker
shows up for you to choose a color.

add_color(variable_name, initial_value)

```python
add_color(variable_name, initial_value)
```
initial_value must be a tuple `(r, g, b)` or a colorcode `#xxxxxx`. Example:

vt.add_color('color1', (12, 63, 85))
vt.add_color('color2', '#0c3f55')

```python
vt.add_color('color1', (12, 63, 85))
vt.add_color('color2', '#0c3f55')
```
When accessing the color variable, `r`, `g`, `b` and `color_code` parts are available.

print(vt.color.r, vt.color.color_code)

```python
print(vt.color.r, vt.color.color_code)
```
## Example Application

import cv2 as cv
from dynamic_variables import VariableTweaker

# Set up dynamic variables
vt = VariableTweaker()
vt.add_text('text', 'Threshold')
vt.add_slider('x', 0, 0, 100, 1)
vt.add_slider('y', 0, 0, 100, 1)
vt.add_slider('scale', 1, 1, 5, 0.01)
vt.add_slider('thickness', 1, 1, 10, 1)
vt.add_color('color', (0, 0, 0))
vt.add_dropdown('threshold_type', 'None', ['None', 'Normal', 'Adaptive Gaussian', 'Adaptive Mean'])
vt.add_slider('thresh', 100, 0, 255, 1)
vt.add_slider('block_size', 3, 3, 201, 2)
vt.add_slider('C', 0, -100, 100, 1)
vt.init_gui()

# import image
image = cv.imread('image.png')

while cv.waitKey(20) != ord('q'):

# Apply Threshold
copy = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
if vt.threshold_type == 'Normal':
_, copy = cv.threshold(copy, vt.thresh, 255, cv.THRESH_BINARY)
elif vt.threshold_type == 'Adaptive Gaussian':
copy = cv.adaptiveThreshold(copy, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, vt.block_size, vt.C)
elif vt.threshold_type == 'Adaptive Mean':
copy = cv.adaptiveThreshold(copy, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, vt.block_size, vt.C)
copy = cv.cvtColor(copy, cv.COLOR_GRAY2BGR)

# Put text
copy = cv.putText(copy, vt.text, (vt.x, vt.y), cv.FONT_HERSHEY_SIMPLEX, vt.scale,
(vt.color.b, vt.color.g, vt.color.r), vt.thickness)

# Show image
cv.imshow('Image', copy)

```python
import cv2 as cv
from dynamic_variables import VariableTweaker

# Set up dynamic variables
vt = VariableTweaker()
vt.add_text('text', 'Threshold')
vt.add_slider('x', 0, 0, 100, 1)
vt.add_slider('y', 0, 0, 100, 1)
vt.add_slider('scale', 1, 1, 5, 0.01)
vt.add_slider('thickness', 1, 1, 10, 1)
vt.add_color('color', (0, 0, 0))
vt.add_dropdown('threshold_type', 'None', ['None', 'Normal', 'Adaptive Gaussian', 'Adaptive Mean'])
vt.add_slider('thresh', 100, 0, 255, 1)
vt.add_slider('block_size', 3, 3, 201, 2)
vt.add_slider('C', 0, -100, 100, 1)
vt.init_gui()

# import image
image = cv.imread('image.png')

while cv.waitKey(20) != ord('q'):

# Apply Threshold
copy = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
if vt.threshold_type == 'Normal':
_, copy = cv.threshold(copy, vt.thresh, 255, cv.THRESH_BINARY)
elif vt.threshold_type == 'Adaptive Gaussian':
copy = cv.adaptiveThreshold(copy, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, vt.block_size, vt.C)
elif vt.threshold_type == 'Adaptive Mean':
copy = cv.adaptiveThreshold(copy, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, vt.block_size, vt.C)
copy = cv.cvtColor(copy, cv.COLOR_GRAY2BGR)

# Put text
copy = cv.putText(copy, vt.text, (vt.x, vt.y), cv.FONT_HERSHEY_SIMPLEX, vt.scale,
(vt.color.b, vt.color.g, vt.color.r), vt.thickness)

# Show image
cv.imshow('Image', copy)
```

![Slider Window](https://github.com/cahidenes/visuals/blob/main/demo.gif?raw=true)

0 comments on commit 83b0688

Please sign in to comment.