Skip to content

Commit

Permalink
refactor: Linter comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsmaennchen221 committed Dec 8, 2023
1 parent ced39af commit 4b2819c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 66 deletions.
63 changes: 34 additions & 29 deletions src/safeds/data/image/containers/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
import io
import warnings
from pathlib import Path
from typing import Any
from typing import Any, TYPE_CHECKING

import torch
import torch.nn.functional as F
import torch.nn.functional as func
from PIL.Image import open as pil_image_open
from torch import Tensor
from torch.types import Device

if TYPE_CHECKING:
from torch.types import Device
import torchvision

# Disable torchvision V2 beta warnings
torchvision.disable_beta_transforms_warning()
from torchvision.transforms.v2 import PILToTensor, functional as F2
# Disables torchvision V2 beta warnings
# Disabled because of RUFF Linter E402 (Module level import not at top of file)
# torchvision.disable_beta_transforms_warning()
from torchvision.transforms.v2 import PILToTensor, functional as func2
from torchvision.utils import save_image

from safeds.exceptions import OutOfBoundsError, ClosedBound, IllegalFormatError
Expand All @@ -34,7 +37,7 @@ class Image:
_default_device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")

@staticmethod
def from_file(path: str | Path, device: Device = _default_device):
def from_file(path: str | Path, device: Device = _default_device) -> Image:
"""
Create an image from a file.
Expand All @@ -53,7 +56,7 @@ def from_file(path: str | Path, device: Device = _default_device):
return Image(image_tensor=Image._pil_to_tensor(pil_image_open(path)), device=device)

@staticmethod
def from_bytes(data: bytes, device: Device = _default_device):
def from_bytes(data: bytes, device: Device = _default_device) -> Image:
"""
Create an image from bytes.
Expand Down Expand Up @@ -227,7 +230,7 @@ def resize(self, new_width: int, new_height: int) -> Image:
result : Image
The image with the given width and height.
"""
return Image(F.interpolate(self._image_tensor.unsqueeze(dim=1), size=(new_height, new_width)).squeeze(dim=1),
return Image(func.interpolate(self._image_tensor.unsqueeze(dim=1), size=(new_height, new_width)).squeeze(dim=1),
device=self._image_tensor.device)

def convert_to_grayscale(self) -> Image:
Expand All @@ -243,11 +246,11 @@ def convert_to_grayscale(self) -> Image:
"""
if self.channel == 4:
return Image(torch.cat(
[F2.rgb_to_grayscale(self._image_tensor[0:3], num_output_channels=3),
[func2.rgb_to_grayscale(self._image_tensor[0:3], num_output_channels=3),
self._image_tensor[3].unsqueeze(dim=0)]),
device=self.device)
else:
return Image(F2.rgb_to_grayscale(self._image_tensor[0:3], num_output_channels=3), device=self.device)
return Image(func2.rgb_to_grayscale(self._image_tensor[0:3], num_output_channels=3), device=self.device)

def crop(self, x: int, y: int, width: int, height: int) -> Image:
"""
Expand All @@ -267,7 +270,7 @@ def crop(self, x: int, y: int, width: int, height: int) -> Image:
result : Image
The cropped image.
"""
return Image(F2.crop(self._image_tensor, x, y, height, width), device=self.device)
return Image(func2.crop(self._image_tensor, x, y, height, width), device=self.device)

def flip_vertically(self) -> Image:
"""
Expand All @@ -280,7 +283,7 @@ def flip_vertically(self) -> Image:
result : Image
The flipped image.
"""
return Image(F2.vertical_flip(self._image_tensor), device=self.device)
return Image(func2.vertical_flip(self._image_tensor), device=self.device)

def flip_horizontally(self) -> Image:
"""
Expand All @@ -293,7 +296,7 @@ def flip_horizontally(self) -> Image:
result : Image
The flipped image.
"""
return Image(F2.horizontal_flip(self._image_tensor), device=self.device)
return Image(func2.horizontal_flip(self._image_tensor), device=self.device)

def adjust_brightness(self, factor: float) -> Image:
"""
Expand Down Expand Up @@ -330,10 +333,11 @@ def adjust_brightness(self, factor: float) -> Image:
)
if self.channel == 4:
return Image(torch.cat(
[F2.adjust_brightness(self._image_tensor[0:3], factor * 1.0), self._image_tensor[3].unsqueeze(dim=0)]),
[func2.adjust_brightness(self._image_tensor[0:3], factor * 1.0),
self._image_tensor[3].unsqueeze(dim=0)]),
device=self.device)
else:
return Image(F2.adjust_brightness(self._image_tensor, factor * 1.0), device=self.device)
return Image(func2.adjust_brightness(self._image_tensor, factor * 1.0), device=self.device)

def add_noise(self, standard_deviation: float) -> Image:
"""
Expand Down Expand Up @@ -396,13 +400,13 @@ def adjust_contrast(self, factor: float) -> Image:
)
if self.channel == 4:
return Image(torch.cat(
[F2.adjust_contrast(self._image_tensor[0:3], factor * 1.0), self._image_tensor[3].unsqueeze(dim=0)]),
[func2.adjust_contrast(self._image_tensor[0:3], factor * 1.0), self._image_tensor[3].unsqueeze(dim=0)]),
device=self.device)
else:
return Image(F2.adjust_contrast(self._image_tensor, factor * 1.0), device=self.device)
return Image(func2.adjust_contrast(self._image_tensor, factor * 1.0), device=self.device)

def adjust_color_balance(self, factor: float) -> Image:
pass
# def adjust_color_balance(self, factor: float) -> Image:
# pass

def blur(self, radius: int) -> Image:
"""
Expand All @@ -421,7 +425,7 @@ def blur(self, radius: int) -> Image:
result : Image
The blurred image.
"""
return Image(F2.gaussian_blur(self._image_tensor, [radius * 2 + 1, radius * 2 + 1]), device=self.device)
return Image(func2.gaussian_blur(self._image_tensor, [radius * 2 + 1, radius * 2 + 1]), device=self.device)

def sharpen(self, factor: float) -> Image:
"""
Expand Down Expand Up @@ -457,10 +461,11 @@ def sharpen(self, factor: float) -> Image:
)
if self.channel == 4:
return Image(torch.cat(
[F2.adjust_sharpness(self._image_tensor[0:3], factor * 1.0), self._image_tensor[3].unsqueeze(dim=0)]),
[func2.adjust_sharpness(self._image_tensor[0:3], factor * 1.0),
self._image_tensor[3].unsqueeze(dim=0)]),
device=self.device)
else:
return Image(F2.adjust_sharpness(self._image_tensor, factor * 1.0), device=self.device)
return Image(func2.adjust_sharpness(self._image_tensor, factor * 1.0), device=self.device)

def invert_colors(self) -> Image:
"""
Expand All @@ -475,10 +480,10 @@ def invert_colors(self) -> Image:
"""
if self.channel == 4:
return Image(torch.cat(
[F2.invert(self._image_tensor[0:3]), self._image_tensor[3].unsqueeze(dim=0)]),
[func2.invert(self._image_tensor[0:3]), self._image_tensor[3].unsqueeze(dim=0)]),
device=self.device)
else:
return Image(F2.invert(self._image_tensor), device=self.device)
return Image(func2.invert(self._image_tensor), device=self.device)

def rotate_right(self) -> Image:
"""
Expand All @@ -491,7 +496,7 @@ def rotate_right(self) -> Image:
result : Image
The image rotated 90 degrees clockwise.
"""
return Image(F2.rotate(self._image_tensor, -90, expand=True), device=self.device)
return Image(func2.rotate(self._image_tensor, -90, expand=True), device=self.device)

def rotate_left(self) -> Image:
"""
Expand All @@ -504,7 +509,7 @@ def rotate_left(self) -> Image:
result : Image
The image rotated 90 degrees counter-clockwise.
"""
return Image(F2.rotate(self._image_tensor, 90, expand=True), device=self.device)
return Image(func2.rotate(self._image_tensor, 90, expand=True), device=self.device)

def find_edges(self) -> Image:
pass
# def find_edges(self) -> Image:
# pass
2 changes: 1 addition & 1 deletion src/safeds/exceptions/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def __init__(self, column_name: str) -> None:


class IllegalFormatError(Exception):
"""Exception raised when a format is not legal"""
"""Exception raised when a format is not legal."""

def __init__(self, formats: list[str] | str, reason: str = "") -> None:
super().__init__(f"This format is illegal. {reason}Use one of the following formats: {formats}")
Loading

0 comments on commit 4b2819c

Please sign in to comment.