本文整理汇总了Python中typing.io.BinaryIO方法的典型用法代码示例。如果您正苦于以下问题:Python io.BinaryIO方法的具体用法?Python io.BinaryIO怎么用?Python io.BinaryIO使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类typing.io
的用法示例。
在下文中一共展示了io.BinaryIO方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: open_binary
# 需要导入模块: from typing import io [as 别名]
# 或者: from typing.io import BinaryIO [as 别名]
def open_binary(package: Package, resource: Resource) -> BinaryIO:
"""Return a file-like object opened for binary reading of the resource."""
resource = _normalize_path(resource)
package = _get_package(package)
reader = _get_resource_reader(package)
if reader is not None:
return reader.open_resource(resource)
# Using pathlib doesn't work well here due to the lack of 'strict'
# argument for pathlib.Path.resolve() prior to Python 3.6.
absolute_package_path = os.path.abspath(
package.__spec__.origin or 'non-existent file')
package_path = os.path.dirname(absolute_package_path)
full_path = os.path.join(package_path, resource)
try:
return open(full_path, mode='rb')
except OSError:
# Just assume the loader is a resource loader; all the relevant
# importlib.machinery loaders are and an AttributeError for
# get_data() will make it clear what is needed from the loader.
loader = cast(ResourceLoader, package.__spec__.loader)
data = None
if hasattr(package.__spec__.loader, 'get_data'):
with suppress(OSError):
data = loader.get_data(full_path)
if data is None:
package_name = package.__spec__.name
message = '{!r} resource not found in {!r}'.format(
resource, package_name)
raise FileNotFoundError(message)
return BytesIO(data)
示例2: open_binary
# 需要导入模块: from typing import io [as 别名]
# 或者: from typing.io import BinaryIO [as 别名]
def open_binary(package: Package, resource: Resource) -> BinaryIO:
"""Return a file-like object opened for binary reading of the resource."""
resource = _normalize_path(resource)
package = _get_package(package)
reader = _get_resource_reader(package)
if reader is not None:
return reader.open_resource(resource)
_check_location(package)
absolute_package_path = os.path.abspath(package.__spec__.origin)
package_path = os.path.dirname(absolute_package_path)
full_path = os.path.join(package_path, resource)
try:
return open(full_path, mode='rb')
except OSError:
# Just assume the loader is a resource loader; all the relevant
# importlib.machinery loaders are and an AttributeError for
# get_data() will make it clear what is needed from the loader.
loader = cast(ResourceLoader, package.__spec__.loader)
data = None
if hasattr(package.__spec__.loader, 'get_data'):
with suppress(OSError):
data = loader.get_data(full_path)
if data is None:
package_name = package.__spec__.name
message = '{!r} resource not found in {!r}'.format(
resource, package_name)
raise FileNotFoundError(message)
else:
return BytesIO(data)
示例3: test_binaryio
# 需要导入模块: from typing import io [as 别名]
# 或者: from typing.io import BinaryIO [as 别名]
def test_binaryio(self):
def stuff(a: BinaryIO) -> bytes:
return a.readline()
a = stuff.__annotations__['a']
assert a.__parameters__ == (bytes,)
示例4: test_io_submodule
# 需要导入模块: from typing import io [as 别名]
# 或者: from typing.io import BinaryIO [as 别名]
def test_io_submodule(self):
from typing.io import IO, TextIO, BinaryIO, __all__, __name__
assert IO is typing.IO
assert TextIO is typing.TextIO
assert BinaryIO is typing.BinaryIO
assert set(__all__) == set(['IO', 'TextIO', 'BinaryIO'])
assert __name__ == 'typing.io'
示例5: open_binary
# 需要导入模块: from typing import io [as 别名]
# 或者: from typing.io import BinaryIO [as 别名]
def open_binary(package: Package, resource: Resource) -> BinaryIO:
"""Return a file-like object opened for binary reading of the resource."""
resource = _normalize_path(resource)
package = _get_package(package)
reader = _get_resource_reader(package)
if reader is not None:
return reader.open_resource(resource)
# Using pathlib doesn't work well here due to the lack of 'strict'
# argument for pathlib.Path.resolve() prior to Python 3.6.
absolute_package_path = os.path.abspath(package.__spec__.origin)
package_path = os.path.dirname(absolute_package_path)
full_path = os.path.join(package_path, resource)
try:
return open(full_path, mode='rb')
except OSError:
# Just assume the loader is a resource loader; all the relevant
# importlib.machinery loaders are and an AttributeError for
# get_data() will make it clear what is needed from the loader.
loader = cast(ResourceLoader, package.__spec__.loader)
data = None
if hasattr(package.__spec__.loader, 'get_data'):
with suppress(OSError):
data = loader.get_data(full_path)
if data is None:
package_name = package.__spec__.name
message = '{!r} resource not found in {!r}'.format(
resource, package_name)
raise FileNotFoundError(message)
else:
return BytesIO(data)
示例6: test_binaryio
# 需要导入模块: from typing import io [as 别名]
# 或者: from typing.io import BinaryIO [as 别名]
def test_binaryio(self):
def stuff(a: BinaryIO) -> bytes:
return a.readline()
a = stuff.__annotations__['a']
self.assertEqual(a.__parameters__, ())
示例7: test_io_submodule
# 需要导入模块: from typing import io [as 别名]
# 或者: from typing.io import BinaryIO [as 别名]
def test_io_submodule(self):
from typing.io import IO, TextIO, BinaryIO, __all__, __name__
self.assertIs(IO, typing.IO)
self.assertIs(TextIO, typing.TextIO)
self.assertIs(BinaryIO, typing.BinaryIO)
self.assertEqual(set(__all__), set(['IO', 'TextIO', 'BinaryIO']))
self.assertEqual(__name__, 'typing.io')
示例8: empty_image
# 需要导入模块: from typing import io [as 别名]
# 或者: from typing.io import BinaryIO [as 别名]
def empty_image(size: Tuple[int, int]) -> BinaryIO:
"""Return a fully transparent PNG image of given size"""
settings = get_settings()
compress_level = settings.PNG_COMPRESS_LEVEL
img = Image.new(mode='P', size=size, color=0)
sio = BytesIO()
img.save(sio, 'png', compress_level=compress_level, transparency=0)
sio.seek(0)
return sio
示例9: tiles
# 需要导入模块: from typing import io [as 别名]
# 或者: from typing.io import BinaryIO [as 别名]
def tiles(
scene: str,
z: int,
x: int,
y: int,
scale: int = 1,
ext: str = "png",
bands: str = None,
expr: str = None,
rescale: str = None,
color_formula: str = None,
color_map: str = None,
pan: bool = False,
) -> Tuple[str, str, BinaryIO]:
"""Handle tile requests."""
driver = "jpeg" if ext == "jpg" else ext
if bands and expr:
raise LandsatTilerError("Cannot pass bands and expression")
tilesize = scale * 256
pan = True if pan else False
if expr is not None:
tile, mask = expression(scene, x, y, z, expr=expr, tilesize=tilesize, pan=pan)
elif bands is not None:
tile, mask = landsat8.tile(
scene, x, y, z, bands=tuple(bands.split(",")), tilesize=tilesize, pan=pan
)
else:
raise LandsatTilerError("No bands nor expression given")
rtile, rmask = _postprocess(
tile, mask, rescale=rescale, color_formula=color_formula
)
if color_map:
color_map = get_colormap(color_map, format="gdal")
options = img_profiles.get(driver, {})
return (
"OK",
f"image/{ext}",
array_to_image(rtile, rmask, img_format=driver, color_map=color_map, **options),
)
示例10: singleband
# 需要导入模块: from typing import io [as 别名]
# 或者: from typing.io import BinaryIO [as 别名]
def singleband(keys: Union[Sequence[str], Mapping[str, str]],
tile_xyz: Tuple[int, int, int] = None, *,
colormap: Union[str, Mapping[Number, RGBA], None] = None,
stretch_range: Tuple[Number, Number] = None,
tile_size: Tuple[int, int] = None) -> BinaryIO:
"""Return singleband image as PNG"""
cmap_or_palette: Union[str, Sequence[RGBA], None]
if stretch_range is None:
stretch_min, stretch_max = None, None
else:
stretch_min, stretch_max = stretch_range
preserve_values = isinstance(colormap, collections.Mapping)
settings = get_settings()
if tile_size is None:
tile_size = settings.DEFAULT_TILE_SIZE
driver = get_driver(settings.DRIVER_PATH, provider=settings.DRIVER_PROVIDER)
with driver.connect():
metadata = driver.get_metadata(keys)
tile_data = xyz.get_tile_data(
driver, keys, tile_xyz,
tile_size=tile_size, preserve_values=preserve_values
)
if preserve_values:
# bin output image into supplied labels, starting at 1
colormap = cast(Mapping, colormap)
labels, label_colors = list(colormap.keys()), list(colormap.values())
cmap_or_palette = label_colors
out = image.label(tile_data, labels)
else:
# determine stretch range from metadata and arguments
stretch_range_ = list(metadata['range'])
if stretch_min is not None:
stretch_range_[0] = stretch_min
if stretch_max is not None:
stretch_range_[1] = stretch_max
cmap_or_palette = cast(Optional[str], colormap)
out = image.to_uint8(tile_data, *stretch_range_)
return image.array_to_png(out, colormap=cmap_or_palette)