mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-10 19:56:47 +03:00
Merge pull request #8107 from radarhere/type_hint
This commit is contained in:
commit
6dbedc51ae
|
@ -25,6 +25,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from typing import IO
|
||||
|
||||
from . import Image, ImageFile, ImagePalette
|
||||
from ._binary import i16le as i16
|
||||
|
@ -52,7 +53,7 @@ def _accept(prefix: bytes) -> bool:
|
|||
return prefix[:2] == b"BM"
|
||||
|
||||
|
||||
def _dib_accept(prefix):
|
||||
def _dib_accept(prefix: bytes) -> bool:
|
||||
return i32(prefix) in [12, 40, 52, 56, 64, 108, 124]
|
||||
|
||||
|
||||
|
@ -394,11 +395,13 @@ SAVE = {
|
|||
}
|
||||
|
||||
|
||||
def _dib_save(im, fp, filename):
|
||||
def _dib_save(im: Image.Image, fp: IO[bytes], filename: str) -> None:
|
||||
_save(im, fp, filename, False)
|
||||
|
||||
|
||||
def _save(im, fp, filename, bitmap_header=True):
|
||||
def _save(
|
||||
im: Image.Image, fp: IO[bytes], filename: str, bitmap_header: bool = True
|
||||
) -> None:
|
||||
try:
|
||||
rawmode, bits, colors = SAVE[im.mode]
|
||||
except KeyError as e:
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
#
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import IO
|
||||
|
||||
from . import Image, ImageFile
|
||||
|
||||
_handler = None
|
||||
|
@ -58,7 +60,7 @@ class BufrStubImageFile(ImageFile.StubImageFile):
|
|||
return _handler
|
||||
|
||||
|
||||
def _save(im, fp, filename):
|
||||
def _save(im: Image.Image, fp: IO[bytes], filename: str) -> None:
|
||||
if _handler is None or not hasattr(_handler, "save"):
|
||||
msg = "BUFR save handler not installed"
|
||||
raise OSError(msg)
|
||||
|
|
|
@ -16,6 +16,7 @@ import io
|
|||
import struct
|
||||
import sys
|
||||
from enum import IntEnum, IntFlag
|
||||
from typing import IO
|
||||
|
||||
from . import Image, ImageFile, ImagePalette
|
||||
from ._binary import i32le as i32
|
||||
|
@ -510,7 +511,7 @@ class DdsRgbDecoder(ImageFile.PyDecoder):
|
|||
return -1, 0
|
||||
|
||||
|
||||
def _save(im, fp, filename):
|
||||
def _save(im: Image.Image, fp: IO[bytes], filename: str) -> None:
|
||||
if im.mode not in ("RGB", "RGBA", "L", "LA"):
|
||||
msg = f"cannot write mode {im.mode} as DDS"
|
||||
raise OSError(msg)
|
||||
|
|
|
@ -31,6 +31,7 @@ import os
|
|||
import subprocess
|
||||
from enum import IntEnum
|
||||
from functools import cached_property
|
||||
from typing import IO
|
||||
|
||||
from . import (
|
||||
Image,
|
||||
|
@ -336,14 +337,13 @@ class GifImageFile(ImageFile.ImageFile):
|
|||
self._mode = "RGB"
|
||||
self.im = self.im.convert("RGB", Image.Dither.FLOYDSTEINBERG)
|
||||
|
||||
def _rgb(color):
|
||||
def _rgb(color: int) -> tuple[int, int, int]:
|
||||
if self._frame_palette:
|
||||
if color * 3 + 3 > len(self._frame_palette.palette):
|
||||
color = 0
|
||||
color = tuple(self._frame_palette.palette[color * 3 : color * 3 + 3])
|
||||
return tuple(self._frame_palette.palette[color * 3 : color * 3 + 3])
|
||||
else:
|
||||
color = (color, color, color)
|
||||
return color
|
||||
return (color, color, color)
|
||||
|
||||
self.dispose_extent = frame_dispose_extent
|
||||
try:
|
||||
|
@ -709,11 +709,13 @@ def _write_multiple_frames(im, fp, palette):
|
|||
return True
|
||||
|
||||
|
||||
def _save_all(im, fp, filename):
|
||||
def _save_all(im: Image.Image, fp: IO[bytes], filename: str) -> None:
|
||||
_save(im, fp, filename, save_all=True)
|
||||
|
||||
|
||||
def _save(im, fp, filename, save_all=False):
|
||||
def _save(
|
||||
im: Image.Image, fp: IO[bytes], filename: str, save_all: bool = False
|
||||
) -> None:
|
||||
# header
|
||||
if "palette" in im.encoderinfo or "palette" in im.info:
|
||||
palette = im.encoderinfo.get("palette", im.info.get("palette"))
|
||||
|
@ -730,7 +732,7 @@ def _save(im, fp, filename, save_all=False):
|
|||
fp.flush()
|
||||
|
||||
|
||||
def get_interlace(im):
|
||||
def get_interlace(im: Image.Image) -> int:
|
||||
interlace = im.encoderinfo.get("interlace", 1)
|
||||
|
||||
# workaround for @PIL153
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
#
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import IO
|
||||
|
||||
from . import Image, ImageFile
|
||||
|
||||
_handler = None
|
||||
|
@ -58,7 +60,7 @@ class GribStubImageFile(ImageFile.StubImageFile):
|
|||
return _handler
|
||||
|
||||
|
||||
def _save(im, fp, filename):
|
||||
def _save(im: Image.Image, fp: IO[bytes], filename: str) -> None:
|
||||
if _handler is None or not hasattr(_handler, "save"):
|
||||
msg = "GRIB save handler not installed"
|
||||
raise OSError(msg)
|
||||
|
|
|
@ -25,6 +25,7 @@ from __future__ import annotations
|
|||
import warnings
|
||||
from io import BytesIO
|
||||
from math import ceil, log
|
||||
from typing import IO
|
||||
|
||||
from . import BmpImagePlugin, Image, ImageFile, PngImagePlugin
|
||||
from ._binary import i16le as i16
|
||||
|
@ -39,7 +40,7 @@ from ._binary import o32le as o32
|
|||
_MAGIC = b"\0\0\1\0"
|
||||
|
||||
|
||||
def _save(im, fp, filename):
|
||||
def _save(im: Image.Image, fp: IO[bytes], filename: str) -> None:
|
||||
fp.write(_MAGIC) # (2+2)
|
||||
bmp = im.encoderinfo.get("bitmap_format") == "bmp"
|
||||
sizes = im.encoderinfo.get(
|
||||
|
@ -194,7 +195,7 @@ class IcoFile:
|
|||
"""
|
||||
return self.frame(self.getentryindex(size, bpp))
|
||||
|
||||
def frame(self, idx):
|
||||
def frame(self, idx: int) -> Image.Image:
|
||||
"""
|
||||
Get an image from frame idx
|
||||
"""
|
||||
|
@ -205,6 +206,7 @@ class IcoFile:
|
|||
data = self.buf.read(8)
|
||||
self.buf.seek(header["offset"])
|
||||
|
||||
im: Image.Image
|
||||
if data[:8] == PngImagePlugin._MAGIC:
|
||||
# png frame
|
||||
im = PngImagePlugin.PngImageFile(self.buf)
|
||||
|
|
|
@ -28,6 +28,7 @@ from __future__ import annotations
|
|||
|
||||
import os
|
||||
import re
|
||||
from typing import IO, Any
|
||||
|
||||
from . import Image, ImageFile, ImagePalette
|
||||
|
||||
|
@ -103,7 +104,7 @@ for j in range(2, 33):
|
|||
split = re.compile(rb"^([A-Za-z][^:]*):[ \t]*(.*)[ \t]*$")
|
||||
|
||||
|
||||
def number(s):
|
||||
def number(s: Any) -> float:
|
||||
try:
|
||||
return int(s)
|
||||
except ValueError:
|
||||
|
@ -325,7 +326,7 @@ SAVE = {
|
|||
}
|
||||
|
||||
|
||||
def _save(im, fp, filename):
|
||||
def _save(im: Image.Image, fp: IO[bytes], filename: str) -> None:
|
||||
try:
|
||||
image_type, rawmode = SAVE[im.mode]
|
||||
except KeyError as e:
|
||||
|
|
|
@ -2875,7 +2875,7 @@ class Image:
|
|||
self.load()
|
||||
return self._new(self.im.transpose(method))
|
||||
|
||||
def effect_spread(self, distance):
|
||||
def effect_spread(self, distance: int) -> Image:
|
||||
"""
|
||||
Randomly spread pixels in an image.
|
||||
|
||||
|
@ -3012,7 +3012,7 @@ def new(
|
|||
return im._new(core.fill(mode, size, color))
|
||||
|
||||
|
||||
def frombytes(mode, size, data, decoder_name="raw", *args) -> Image:
|
||||
def frombytes(mode, size, data, decoder_name: str = "raw", *args) -> Image:
|
||||
"""
|
||||
Creates a copy of an image memory from pixel data in a buffer.
|
||||
|
||||
|
@ -3051,7 +3051,7 @@ def frombytes(mode, size, data, decoder_name="raw", *args) -> Image:
|
|||
return im
|
||||
|
||||
|
||||
def frombuffer(mode, size, data, decoder_name="raw", *args) -> Image:
|
||||
def frombuffer(mode: str, size, data, decoder_name: str = "raw", *args) -> Image:
|
||||
"""
|
||||
Creates an image memory referencing pixel data in a byte buffer.
|
||||
|
||||
|
@ -3553,7 +3553,7 @@ def register_save(id: str, driver) -> None:
|
|||
SAVE[id.upper()] = driver
|
||||
|
||||
|
||||
def register_save_all(id, driver) -> None:
|
||||
def register_save_all(id: str, driver) -> None:
|
||||
"""
|
||||
Registers an image function to save all the frames
|
||||
of a multiframe format. This function should not be
|
||||
|
@ -3565,7 +3565,7 @@ def register_save_all(id, driver) -> None:
|
|||
SAVE_ALL[id.upper()] = driver
|
||||
|
||||
|
||||
def register_extension(id, extension) -> None:
|
||||
def register_extension(id: str, extension: str) -> None:
|
||||
"""
|
||||
Registers an image extension. This function should not be
|
||||
used in application code.
|
||||
|
@ -3576,7 +3576,7 @@ def register_extension(id, extension) -> None:
|
|||
EXTENSION[extension.lower()] = id.upper()
|
||||
|
||||
|
||||
def register_extensions(id, extensions) -> None:
|
||||
def register_extensions(id: str, extensions: list[str]) -> None:
|
||||
"""
|
||||
Registers image extensions. This function should not be
|
||||
used in application code.
|
||||
|
@ -3588,7 +3588,7 @@ def register_extensions(id, extensions) -> None:
|
|||
register_extension(id, extension)
|
||||
|
||||
|
||||
def registered_extensions():
|
||||
def registered_extensions() -> dict[str, str]:
|
||||
"""
|
||||
Returns a dictionary containing all file extensions belonging
|
||||
to registered plugins
|
||||
|
@ -3650,7 +3650,7 @@ def effect_mandelbrot(size, extent, quality):
|
|||
return Image()._new(core.effect_mandelbrot(size, extent, quality))
|
||||
|
||||
|
||||
def effect_noise(size, sigma):
|
||||
def effect_noise(size: tuple[int, int], sigma: float) -> Image:
|
||||
"""
|
||||
Generate Gaussian noise centered around 128.
|
||||
|
||||
|
@ -3661,7 +3661,7 @@ def effect_noise(size, sigma):
|
|||
return Image()._new(core.effect_noise(size, sigma))
|
||||
|
||||
|
||||
def linear_gradient(mode):
|
||||
def linear_gradient(mode: str) -> Image:
|
||||
"""
|
||||
Generate 256x256 linear gradient from black to white, top to bottom.
|
||||
|
||||
|
@ -3670,7 +3670,7 @@ def linear_gradient(mode):
|
|||
return Image()._new(core.linear_gradient(mode))
|
||||
|
||||
|
||||
def radial_gradient(mode):
|
||||
def radial_gradient(mode: str) -> Image:
|
||||
"""
|
||||
Generate 256x256 radial gradient from black to white, centre to edge.
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@ from __future__ import annotations
|
|||
import itertools
|
||||
import os
|
||||
import struct
|
||||
from typing import IO
|
||||
|
||||
from . import (
|
||||
Image,
|
||||
|
@ -32,7 +33,7 @@ from . import (
|
|||
from ._binary import o32le
|
||||
|
||||
|
||||
def _save(im, fp, filename):
|
||||
def _save(im: Image.Image, fp: IO[bytes], filename: str) -> None:
|
||||
JpegImagePlugin._save(im, fp, filename)
|
||||
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@ import io
|
|||
import math
|
||||
import os
|
||||
import time
|
||||
from typing import IO
|
||||
|
||||
from . import Image, ImageFile, ImageSequence, PdfParser, __version__, features
|
||||
|
||||
|
@ -39,7 +40,7 @@ from . import Image, ImageFile, ImageSequence, PdfParser, __version__, features
|
|||
# 5. page contents
|
||||
|
||||
|
||||
def _save_all(im, fp, filename):
|
||||
def _save_all(im: Image.Image, fp: IO[bytes], filename: str) -> None:
|
||||
_save(im, fp, filename, save_all=True)
|
||||
|
||||
|
||||
|
|
|
@ -37,7 +37,7 @@ from __future__ import annotations
|
|||
import os
|
||||
import struct
|
||||
import sys
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import IO, TYPE_CHECKING
|
||||
|
||||
from . import Image, ImageFile
|
||||
|
||||
|
@ -263,7 +263,7 @@ def makeSpiderHeader(im: Image.Image) -> list[bytes]:
|
|||
return [struct.pack("f", v) for v in hdr]
|
||||
|
||||
|
||||
def _save(im, fp, filename):
|
||||
def _save(im: Image.Image, fp: IO[bytes], filename: str) -> None:
|
||||
if im.mode[0] != "F":
|
||||
im = im.convert("F")
|
||||
|
||||
|
@ -279,7 +279,7 @@ def _save(im, fp, filename):
|
|||
ImageFile._save(im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, 1))])
|
||||
|
||||
|
||||
def _save_spider(im, fp, filename):
|
||||
def _save_spider(im: Image.Image, fp: IO[bytes], filename: str) -> None:
|
||||
# get the filename extension and register it with Image
|
||||
ext = os.path.splitext(filename)[1]
|
||||
Image.register_extension(SpiderImageFile.format, ext)
|
||||
|
|
|
@ -1995,7 +1995,7 @@ class AppendingTiffWriter:
|
|||
self.finalize()
|
||||
self.setup()
|
||||
|
||||
def __enter__(self):
|
||||
def __enter__(self) -> AppendingTiffWriter:
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_value, traceback):
|
||||
|
@ -2023,7 +2023,7 @@ class AppendingTiffWriter:
|
|||
self.f.write(bytes(pad_bytes))
|
||||
self.offsetOfNewPage = self.f.tell()
|
||||
|
||||
def setEndian(self, endian):
|
||||
def setEndian(self, endian: str) -> None:
|
||||
self.endian = endian
|
||||
self.longFmt = f"{self.endian}L"
|
||||
self.shortFmt = f"{self.endian}H"
|
||||
|
|
|
@ -20,6 +20,8 @@
|
|||
# http://wvware.sourceforge.net/caolan/ora-wmf.html
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import IO
|
||||
|
||||
from . import Image, ImageFile
|
||||
from ._binary import i16le as word
|
||||
from ._binary import si16le as short
|
||||
|
@ -161,7 +163,7 @@ class WmfStubImageFile(ImageFile.StubImageFile):
|
|||
return super().load()
|
||||
|
||||
|
||||
def _save(im, fp, filename):
|
||||
def _save(im: Image.Image, fp: IO[bytes], filename: str) -> None:
|
||||
if _handler is None or not hasattr(_handler, "save"):
|
||||
msg = "WMF save handler not installed"
|
||||
raise OSError(msg)
|
||||
|
|
Loading…
Reference in New Issue
Block a user