Skip to content

Commit

Permalink
V2.2.7 (#7)
Browse files Browse the repository at this point in the history
* chore: added ImagePlot util

* chore: update version

* release: v2.2.5

* release: v2.2.7
  • Loading branch information
jlsneto authored Nov 22, 2023
1 parent 6f45ddb commit 590f126
Show file tree
Hide file tree
Showing 5 changed files with 771 additions and 718 deletions.
2 changes: 1 addition & 1 deletion calango/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
from .devices import Mouse
from .media import Image, VideoWriter, Video

VERSION = "2.2.6.final.0"
VERSION = "2.2.7.final.0"
__version__ = get_version_pep440_compliant(VERSION)
28 changes: 28 additions & 0 deletions calango/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ def gray_scale(self) -> Image:
return Image(cv2.cvtColor(self, code_))
return self

def gray_to_bgr(self) -> Image:
self[:, ] = cv2.cvtColor(self, cv2.COLOR_GRAY2BGR)
self._color_mode = "BGR"
return self

def gray_to_rgb(self) -> Image:
self[:, ] = cv2.cvtColor(self, cv2.COLOR_GRAY2RGB)
self._color_mode = "RGB"
return self

def rgb_to_bgr(self) -> Image:
if self._color_mode == 'RGB':
self._color_mode = 'BGR'
Expand Down Expand Up @@ -307,6 +317,24 @@ def crop_by_center(self, size=None, keep_scale=False) -> Image:

return Image(cv2.resize(im_crop[start[1]:end[1], start[0]:end[0]], (new_w, new_h)))

@staticmethod
def remove_shadow(image):
if image.shape[-1] > 1:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 2. Apply bilateral filter to smooth the image
bilateral = cv2.bilateralFilter(image, 15, 75, 75)

# 3. Compute the background using morphological closing
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 21))
background = cv2.morphologyEx(bilateral, cv2.MORPH_CLOSE, kernel)

# 4. Compute the ratio of the grayscale and background image to remove shadows
ratio = (image.astype('float') / background.astype('float')) * 255.0
shadow_removed = np.clip(ratio, 0, 255).astype('uint8')

return shadow_removed

def prune(self) -> Image:
min_len = self.min_len
return Image(self.crop_by_center((min_len, min_len)))
Expand Down
29 changes: 29 additions & 0 deletions calango/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import base64
import io
from .settings import ON_COLAB_JUPYTER
from matplotlib import pyplot as plt
import math

__all__ = ['show_local_mp4']

Expand All @@ -17,3 +19,30 @@ def show_local_mp4(file_name, width=640, height=480):
width, height, video_encoded.decode("ascii")
)
)


class ImagePlot:
def __init__(self, fig_size=(13, 13), max_cols=4):
self.fig_size = fig_size
self.figure = plt.figure(figsize=fig_size) # TODO: does it need to be global?
self._max_cols = max_cols
self._total_images = 0

@property
def current_row(self):
return math.ceil(self._total_images / self._max_cols)

@property
def current_col(self):
return ((self._total_images - 1) % self._max_cols) + 1

def plot(self, image, title=None, color='gray'):
try:
self._total_images += 1
plt.subplot(self._max_cols, self._max_cols, self._total_images)
plt.title(self.current_col if title is None else title)
plt.xticks([]), plt.yticks([])
plt.imshow(image.copy(), color)
except Exception as err:
self._total_images -= 1 # garante consistencia da quantidade de imagens
raise err
1,426 changes: 712 additions & 714 deletions docs/example_usage.ipynb

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
cereja
pyautogui
opencv-python
matplotlib
numpy
mss
numpy

0 comments on commit 590f126

Please sign in to comment.