Load gif to numpy array. Args: filename: filename of the gif crop_ratio: ratio of part to be cropped crop_dir: direction to be cropped, default left Returns: ndarray (n,h,w), n: number of frames
(
filename: str,
crop_ratio: float = 0,
crop_dir: str = 'left',
)
| 521 | |
| 522 | |
| 523 | def gif_to_nparray( |
| 524 | filename: str, |
| 525 | crop_ratio: float = 0, |
| 526 | crop_dir: str = 'left', |
| 527 | ) -> np.ndarray: |
| 528 | """ |
| 529 | Load gif to numpy array. |
| 530 | |
| 531 | Args: |
| 532 | filename: filename of the gif |
| 533 | crop_ratio: ratio of part to be cropped |
| 534 | crop_dir: direction to be cropped, default left |
| 535 | Returns: |
| 536 | ndarray (n,h,w), n: number of frames |
| 537 | """ |
| 538 | imglist = [] |
| 539 | if not os.path.exists(filename): |
| 540 | return None |
| 541 | |
| 542 | imageObject = Image.open(filename) |
| 543 | for frame in range(0, imageObject.n_frames): |
| 544 | imageObject.seek(frame) |
| 545 | tmp = imageObject.convert() # Make without palette |
| 546 | tmp = np.asarray(tmp) / 255. |
| 547 | # print(tmp.shape) |
| 548 | if crop_ratio != 0: |
| 549 | if crop_dir == 'left': |
| 550 | tmp = tmp[:, int(tmp.shape[1] * crop_ratio):] |
| 551 | elif crop_dir == 'right': |
| 552 | tmp = tmp[:, :int(tmp.shape[1] * crop_ratio)] |
| 553 | else: |
| 554 | raise NotImplementedError |
| 555 | |
| 556 | imglist.append(np.expand_dims(tmp, axis=0)) |
| 557 | gifarray = np.concatenate(imglist, axis=0) # (n,h,w) |
| 558 | return gifarray |
| 559 | |
| 560 | |
| 561 | def add_title_to_image( |
nothing calls this directly
no outgoing calls
no test coverage detected