Python/digital_image_processing/convert_to_negative.py
pre-commit-ci[bot] bc8df6de31
[pre-commit.ci] pre-commit autoupdate ()
* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.2.2 → v0.3.2](https://github.com/astral-sh/ruff-pre-commit/compare/v0.2.2...v0.3.2)
- [github.com/pre-commit/mirrors-mypy: v1.8.0 → v1.9.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.8.0...v1.9.0)

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-03-13 07:52:41 +01:00

31 lines
734 B
Python

"""
Implemented an algorithm using opencv to convert a colored image into its negative
"""
from cv2 import destroyAllWindows, imread, imshow, waitKey
def convert_to_negative(img):
# getting number of pixels in the image
pixel_h, pixel_v = img.shape[0], img.shape[1]
# converting each pixel's color to its negative
for i in range(pixel_h):
for j in range(pixel_v):
img[i][j] = [255, 255, 255] - img[i][j]
return img
if __name__ == "__main__":
# read original image
img = imread("image_data/lena.jpg", 1)
# convert to its negative
neg = convert_to_negative(img)
# show result image
imshow("negative of original image", img)
waitKey(0)
destroyAllWindows()