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

Grayscale function #47

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
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
24 changes: 22 additions & 2 deletions src/imagenie/grayscale.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import numpy as np

def grayscale(image):
"""
Converts an image to grayscale.
Expand All @@ -14,12 +16,30 @@ def grayscale(image):

Raises:
------
TypeError
If the input is not a NumPy array.
ValueError
If the input is not a 2D or 3D NumPy array.
If the input is not a 2D or 3D NumPy array, or if the 3D array does not have 3 channels.

Examples:
---------
Convert an RGB image to grayscale:
>>> gray_image = grayscale(image)
"""
pass
# Ensure the input is a valid NumPy array
if not isinstance(image, np.ndarray):
raise TypeError("The input image must be a NumPy array.")

# Handle already grayscale (2D) images
if image.ndim == 2:
return image

# Handle RGB images (3D)
elif image.ndim == 3:
if image.shape[-1] != 3:
raise ValueError("The input image must have 3 channels in the last dimension for RGB.")
# Use weighted average to convert to grayscale
return np.dot(image[..., :3], [0.2989, 0.5870, 0.1140])

else:
raise ValueError("The input image must be a 2D or 3D NumPy array.")
72 changes: 72 additions & 0 deletions tests/test_grayscale.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 10,
"id": "f6be37b7-8203-43ea-b4f6-8c9a7823e0f9",
"metadata": {},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'imagenie'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[10], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01munittest\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mimagenie\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m grayscale \n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtest_grayscale\u001b[39m():\n\u001b[1;32m 6\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Test all cases for the grayscale function.\"\"\"\u001b[39;00m\n",
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'imagenie'"
]
}
],
"source": [
"import unittest\n",
"import numpy as np\n",
"from imagenie import grayscale \n",
"\n",
"def test_grayscale():\n",
" \"\"\"Test all cases for the grayscale function.\"\"\"\n",
"\n",
" # Test that an already grayscale image (2D array) is returned unchanged\n",
" gray_image = np.array([[10, 20, 30], [40, 50, 60]], dtype=np.uint8)\n",
" result = grayscale(gray_image)\n",
" np.testing.assert_array_equal(result, gray_image)\n",
"\n",
" # Test that an empty array is handled correctly\n",
" empty_image = np.array([], dtype=np.uint8).reshape(0, 0)\n",
" result = grayscale(empty_image)\n",
" np.testing.assert_array_equal(result, empty_image)\n",
"\n",
" # Test that a TypeError is raised if the input is not a NumPy array\n",
" try:\n",
" grayscale([[255, 0, 0], [0, 255, 0]]) # Input is a Python list, not a NumPy array\n",
" except TypeError as e:\n",
" assert str(e) == \"The input image must be a NumPy array.\"\n",
"\n",
"if __name__ == \"__main__\":\n",
" test_grayscale()\n",
" print(\"All tests passed.\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading