Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Add format detection and code readability improvements in plots.py #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 15 additions & 12 deletions utils/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,31 @@
import numpy as np
import matplotlib.pyplot as plt

def load_img(filename, debug=False, norm=True, resize=None):
def _imread(filename):
# Attempt to read image with cv2, if it fails, raise an IOError
img = cv2.imread(filename)
if img is None:
raise IOError(f"Cannot open image file {filename}")
return img

def load_img (filename, debug=False, norm=True, resize=None):
img = cv2.imread(filename)
img = _imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
if norm:
img = img / 255.
img = img.astype(np.float32)
if debug:
print (img.shape, img.dtype, img.min(), img.max())

print(img.shape, img.dtype, img.min(), img.max())
if resize:
img = cv2.resize(img, (resize[0], resize[1]))

return img


def plot_all (images, axis='off', figsize=(16, 8)):

def plot_all(images, axis='off', figsize=(16, 8)):
fig = plt.figure(figsize=figsize, dpi=80)
nplots = len(images)
for i in range(nplots):
plt.subplot(1,nplots,i+1)
plt.axis(axis)
plt.imshow(images[i])
for i, img in enumerate(images):
ax = fig.add_subplot(1, nplots, i+1)
ax.axis(axis)
ax.imshow(img)
plt.show()