Skip to content

Commit

Permalink
Fix 'video.tools.drawing.color_gradient' (Zulko#1467)
Browse files Browse the repository at this point in the history
* Fix inconsistency of arguments in 'video.tools.drawing.color_gradient'

* Add CHANGELOG entry

* Fix error in implementation

* Fix formatting error

* Remove uneeded lines

* Remove uneeded if

* Raise ValueError if 'shape' is invalid

* Raise 'ValueError' with inconsistent p2/vector in 'bilinear' shape
  • Loading branch information
mondeja authored Jan 19, 2021
1 parent 3ebede0 commit 7183eaa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed `freeze` FX was freezing at time minus 1 second as the end [\#1461](https://github.com/Zulko/moviepy/pull/1461)
- `AudioClip.max_volume(stereo=True)` now can return more than 2 channels [\#1464](https://github.com/Zulko/moviepy/pull/1464)
- Fixed `Clip.cutout` transformation not being applied to audio [\#1468](https://github.com/Zulko/moviepy/pull/1468)
- Fixed arguments inconsistency in `video.tools.drawing.color_gradient` [\#1467](https://github.com/Zulko/moviepy/pull/1467)


## [v2.0.0.dev2](https://github.com/zulko/moviepy/tree/v2.0.0.dev2) (2020-10-05)
Expand Down
26 changes: 12 additions & 14 deletions moviepy/video/tools/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ def color_gradient(

if shape == "bilinear":
if vector is None:
if p2 is None:
raise ValueError("You must provide either 'p2' or 'vector'")
vector = np.array(p2) - np.array(p1)

m1, m2 = [
Expand All @@ -124,20 +126,18 @@ def color_gradient(

p1 = np.array(p1[::-1]).astype(float)

if vector is None and p2:
p2 = np.array(p2[::-1])
vector = p2 - p1
else:
vector = np.array(vector[::-1])
p2 = p1 + vector

if vector is not None:
norm = np.linalg.norm(vector)

M = np.dstack(np.meshgrid(range(w), range(h))[::-1]).astype(float)

if shape == "linear":
if vector is None:
if p2 is not None:
vector = np.array(p2[::-1]) - p1
else:
raise ValueError("You must provide either 'p2' or 'vector'")
else:
vector = np.array(vector[::-1])

norm = np.linalg.norm(vector)
n_vec = vector / norm ** 2 # norm 1/norm(vector)

p1 = p1 + offset * vector
Expand All @@ -148,10 +148,7 @@ def color_gradient(
return arr * color_1 + (1 - arr) * color_2

elif shape == "radial":
if radius is None:
radius = norm

if radius == 0:
if (radius or 0) == 0:
arr = np.ones((h, w))
else:
arr = (np.sqrt(((M - p1) ** 2).sum(axis=2))) - offset * radius
Expand All @@ -161,6 +158,7 @@ def color_gradient(
if color_1.size > 1:
arr = np.dstack(3 * [arr])
return (1 - arr) * color_1 + arr * color_2
raise ValueError("Invalid shape, should be either 'radial', 'linear' or 'bilinear'")


def color_split(
Expand Down

0 comments on commit 7183eaa

Please sign in to comment.