Cara menggunakan invert image python

OpenCV or Open Source Computer Vision Library is a real-time computer vision library used for image processing and machine learning. It is written in C/C++ and is available for many programming languages such as C++, Python, and Java. In Python, generally, OpenCV is used along with NumPy, a Python-based library for matrices, multi-dimensional arrays, and mathematical computation.

Images are represented using NumPy multi-dimensional arrays and are processed using OpenCV. Using the OpenCV module, one can perform many operations over images such as flipping, scaling, rotating, mirroring, changing colors, inverting colors, etc. In this article, we will learn how to invert images using the OpenCV module.

Inverting Images

Images are represented using RGB or Red Green Blue values. Each can take up an integer value between 0 and 255 (both included). For example, a red color is represent using (255, 0, 0), white with (255, 255, 255), black with (0, 0, 0), etc.

Inverting an image means reversing the colors on the image. For example, the inverted color for red color will be (0, 255, 255). Note that 0 became 255 and 255 became 0. This means that inverting an image is essentially subtracting the old RGB values from 255.

Original image:

Cara menggunakan invert image python

Inverted image:

invert image in python opencv - original

Invert Images Using import cv2 import numpy as np image = cv2.imread("image.png", 0) inverted_image = np.invert(image) cv2.imwrite("inverted.jpg", inverted) cv2.imshow("Original Image",image) cv2.imshow("Inverted Image",inverted_image) 3 Method in Python

OpenCV has a

import cv2
import numpy as np

image = cv2.imread("image.png", 0)
inverted_image = np.invert(image)
cv2.imwrite("inverted.jpg", inverted)
cv2.imshow("Original Image",image)
cv2.imshow("Inverted Image",inverted_image)
3 method which performs bit-wise NOT operation. We can use this function to invert an image. Refer to the following code. It considers that you have an image by the name of
import cv2
import numpy as np

image = cv2.imread("image.png", 0)
inverted_image = np.invert(image)
cv2.imwrite("inverted.jpg", inverted)
cv2.imshow("Original Image",image)
cv2.imshow("Inverted Image",inverted_image)
5 in your working directory.

import cv2

image = cv2.imread("image.png", 0)
inverted_image = cv2.bitwise_not(image)
cv2.imwrite("inverted.jpg", inverted)
cv2.imshow("Original Image",image)
cv2.imshow("Inverted Image",inverted_image)

This program will first load an image, invert it and save it in the working directory. After that, it will show both the original and the inverted images.

Invert Images Using import cv2 import numpy as np image = cv2.imread("image.png", 0) inverted_image = np.invert(image) cv2.imwrite("inverted.jpg", inverted) cv2.imshow("Original Image",image) cv2.imshow("Inverted Image",inverted_image) 6 Method in Python

NumPy has an

import cv2
import numpy as np

image = cv2.imread("image.png", 0)
inverted_image = np.invert(image)
cv2.imwrite("inverted.jpg", inverted)
cv2.imshow("Original Image",image)
cv2.imshow("Inverted Image",inverted_image)
7 method, which performs bit-wise inversion or bit-wise NOT operation. The following code shows how we can use this method to perform the inversion.

There are plenty of libraries in python which offer a wide range of features and functionalities. In such a way, python has many libraries to play with images and do various alterations with few lines of code.

We have many libraries of python which give an inverted image directly using some inbuilt functions. This article will look into a method to understand the functionality behind the screens.

How images are stored

We can see that when we zoom into the images tends to get blurry, and we can see individual squares, which are known as pixels. Pixels are the basic blocks of the images that extend horizontally and vertically according to the image's dimensions, storing the required data about each block. Pixel Values are numbers that represent the numerical value of each of these pixels. These pixel values represent the intensity of the pixels. Pixel values for grayscale or black-and-white images range from 0 to 255. The smaller numbers closer to zero represent the darker shade, while the larger numbers closer to 255 represent the lighter or the white shade.

Similarly, if we look at a color image, each pixel comprises three different colors: Green, Red, and Blue. So each pixel stores the pixel values of these three different colors. And also, each of these matrice values too varies from 0 to 255.

The elaboration of a single pixel of a color image.How to explore an image using python

Many libraries can be used in python to open images and convert them to arrays. But in this article, we will use mainly three libraries to do our task. “Pillow , IPython.display and Numpy”.

Let's start by importing these libraries and functions,

We will use the Image module from the Pillow / PIL library, we will use the Image module, and from the IPython Display library, we will use the display module to see the outputs in the notebook itself.

so now we can import the image and have a look at it as well

Preview of the image in Jupyter Notebook

So after importing the libraries, we can use the Image. Open to open the image specified and store it to a variable. And using the display module, we can look at the imported image. Here I have imported a grayscale image to ease of explanation. But these steps work for a color image as well.

Let's look at the pixels of this image…

To play with this picture, we will convert this into an array, and let's inspect the values stored and shape of it.

By the above piece of code, we will covert the image to an array and print the array and the shape of the array.

By the output, we can see that it's a two-dimensional array of 200 columns and 200 rows, which is because the image I have used is of resolution “200px X 200px”. And also, the values of each position are a single value which varies from 0–255 since this is a grayscale. These two values may change according to the resolution and color scheme of the image you use.

Now let's invert the values…

As we said before, the pixel values range from 0 – 225, and We can invert each value by subtracting the value from 255. We shall initially create an array with the same shape as the image and initialize all the values with 255. And we can do a simple arithmetic operation by subtracting the image array from the new array we created. This can be easily done with the help of Numpy as follows,

“np. full” will create an array of shapes equal to the im. shape(this will give the shape of the “array im” in which we stored the values of the original image)

Pixel inverting

So now, we can subtract the image array from the new array we created to get the desired output. And “ astype ” function helps us define the data type, which should be of “uint8-unsigned integer of size 8 bit.” and save the array to the variable “mod_image.”

Final step.

Now we can check the output using the“ Image.fromarray ” function, which turns the given array of values into an image to be viewed in the notebook. And tadaaaaaaaa….. we have the output as follows.

The final output of the code.

Here I provide you with the full code, which may help you recheck and verify your understanding.

Final words

I hope this article gave you a clear and proper understanding of inverting an image with python, with all the necessary steps and the reason behind the steps. The same code can also convert color images as well since we don't hardcode the mask's shape, but we do get it from the image directly.