Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
__pycache__/
.coverage
/build
/dist
poetry.lock

14 changes: 11 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
repos:

- repo: https://github.com/pycqa/isort
rev: 5.9.2
rev: 5.13.2
hooks:
- id: isort
additional_dependencies: [toml]
- repo: https://github.com/psf/black
rev: 21.6b0
rev: 22.3.0
hooks:
- id: black

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.942
hooks:
- id: mypy
- repo: https://github.com/pycqa/pylint
rev: v2.17.7
hooks:
- id: pylint
additional_dependencies: [toml]
16 changes: 10 additions & 6 deletions ass_renderer/libass.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,28 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from __future__ import annotations

# pylint: disable=attribute-defined-outside-init
# pylint: disable=invalid-name
# pylint: disable=protected-access

import ctypes
import ctypes.util
import logging
import os
from collections.abc import Callable, Iterator
from enum import IntEnum
from typing import Any, Optional
from typing import Any, Optional, cast

import ass_parser
from ass_parser import write_ass

logger = logging.getLogger(__name__)

_libass_path = ctypes.util.find_library("ass") or ctypes.util.find_library(
"libass"
_libass_path = (
ctypes.util.find_library("ass")
or ctypes.util.find_library("libass")
or os.environ.get("ASS_RENDERER_LIBASS_PATH")
)
assert _libass_path, "libass was not found"
_libass = ctypes.cdll.LoadLibrary(_libass_path)
Expand Down Expand Up @@ -173,12 +177,12 @@ def __del__(self) -> None:
def make_renderer(self) -> "AssRenderer":
renderer = _libass.ass_renderer_init(ctypes.byref(self)).contents
renderer._after_init(self)
return renderer
return cast(AssRenderer, renderer)

def make_track(self) -> "AssTrack":
track = _libass.ass_new_track(ctypes.byref(self)).contents
track._after_init(self)
return track
return cast(AssTrack, track)


class AssRenderer(ctypes.Structure):
Expand Down
10 changes: 9 additions & 1 deletion ass_renderer/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

"""Module for drawing ASS structures to numpy bitmaps."""

from __future__ import annotations

import ctypes
from fractions import Fraction
from typing import Optional, Union
Expand All @@ -27,6 +29,12 @@

from ass_renderer import libass

# For compatability with Pillow changes
try:
_RESAMPLER = PIL.Image.LANCOS
except AttributeError:
_RESAMPLER = PIL.Image.ANTIALIAS


class AssRenderer:
"""Public renderer facade."""
Expand Down Expand Up @@ -120,7 +128,7 @@ def render(

ret = PIL.Image.fromarray(image_data)
ret = ret.resize(
(int(ret.width * aspect_ratio), ret.height), PIL.Image.LANCZOS
(int(ret.width * aspect_ratio), ret.height), _RESAMPLER
)
final = PIL.Image.new("RGBA", self._renderer.frame_size)
final.paste(ret, ((self._renderer.frame_size[0] - ret.width) // 2, 0))
Expand Down
Loading