Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for ColorClip.rotate() #1139

Merged
merged 5 commits into from
Apr 14, 2020
Merged

Conversation

tburrows13
Copy link
Collaborator

@tburrows13 tburrows13 commented Apr 8, 2020

Fixes #1140.

Previously, attempting to use clip.rotate(...) when clip is a ColorClip would crash:

TypeError: Cannot handle this data type: (1, 1, 3), <i8

Traceback (most recent call last):
  File "/Users/tomburrows/miniconda3/envs/moviepy/lib/python3.7/site-packages/PIL/Image.py", line 2680, in fromarray
    mode, rawmode = _fromarray_typemap[typekey]
KeyError: ((1, 1, 3), '<i8')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/tomburrows/Python/moviepy-dev/moviepy/tom_t.py", line 64, in <module>
    c = c.rotate(lambda t: 45 + 3*t)
  File "/Users/tomburrows/Python/moviepy-dev/moviepy/moviepy/video/fx/rotate.py", line 84, in rotate
    return clip.fl(fl, apply_to=["mask"])
  File "/Users/tomburrows/Python/moviepy-dev/moviepy/moviepy/video/VideoClip.py", line 1000, in fl
    newclip = VideoClip.fl(self, fl, apply_to=apply_to, keep_duration=keep_duration)
  File "/Users/tomburrows/Python/moviepy-dev/moviepy/moviepy/Clip.py", line 140, in fl
    newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
  File "</Users/tomburrows/miniconda3/envs/moviepy/lib/python3.7/site-packages/decorator.py:decorator-gen-61>", line 2, in set_make_frame
  File "/Users/tomburrows/Python/moviepy-dev/moviepy/moviepy/decorators.py", line 14, in outplace
    f(newclip, *a, **k)
  File "/Users/tomburrows/Python/moviepy-dev/moviepy/moviepy/video/VideoClip.py", line 721, in set_make_frame
    self.size = self.get_frame(0).shape[:2][::-1]
  File "</Users/tomburrows/miniconda3/envs/moviepy/lib/python3.7/site-packages/decorator.py:decorator-gen-11>", line 2, in get_frame
  File "/Users/tomburrows/Python/moviepy-dev/moviepy/moviepy/decorators.py", line 87, in wrapper
    return f(*new_a, **new_kw)
  File "/Users/tomburrows/Python/moviepy-dev/moviepy/moviepy/Clip.py", line 97, in get_frame
    return self.make_frame(t)
  File "/Users/tomburrows/Python/moviepy-dev/moviepy/moviepy/Clip.py", line 140, in <lambda>
    newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
  File "/Users/tomburrows/Python/moviepy-dev/moviepy/moviepy/video/fx/rotate.py", line 82, in fl
    return pil_rotater(im, a, resample=resample, expand=expand)
  File "/Users/tomburrows/Python/moviepy-dev/moviepy/moviepy/video/fx/rotate.py", line 12, in pil_rotater
    Image.fromarray(pic).rotate(angle, expand=expand, resample=resample)
  File "/Users/tomburrows/miniconda3/envs/moviepy/lib/python3.7/site-packages/PIL/Image.py", line 2682, in fromarray
    raise TypeError("Cannot handle this data type: %s, %s" % typekey)
TypeError: Cannot handle this data type: (1, 1, 3), <i8

Discovered on Stack Overflow: https://stackoverflow.com/questions/60940266

Explanation: when a ColorClip is created (for example c = ColorClip([600, 400], color=[150, 200, 100])), each pixel is stored as whatever is passed through as the color parameter. However, Image.fromarray() (from pillow) requires each pixel to be in the "uint8" format. When writing to a file, this is performed in Clip.iter_frames(), so this PR simply calls .astype("uint8") inside of rotate().

  • I have provided code that clearly demonstrates the bug and that only works correctly when applying this fix
  • I have added suitable tests demonstrating a fixed bug or new/changed feature to the test suite in tests/
  • I have formatted my code using black -t py36

@tburrows13 tburrows13 added the bug-fix For PRs and issues solving bugs. label Apr 8, 2020
@coveralls
Copy link

coveralls commented Apr 8, 2020

Coverage Status

Coverage decreased (-0.4%) to 64.352% when pulling 6cebb97 on tburrows13:fix-colorclip-rotate into 4106b4c on Zulko:master.

@tburrows13
Copy link
Collaborator Author

I think that this is ready to merge.

@tburrows13 tburrows13 requested a review from mgaitan April 9, 2020 15:47
@tburrows13 tburrows13 merged commit b606067 into Zulko:master Apr 14, 2020
@tburrows13 tburrows13 deleted the fix-colorclip-rotate branch April 14, 2020 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug-fix For PRs and issues solving bugs.
Projects
None yet
2 participants