I work as a Software Engineer at Endtest.
In this article, I will show you how easy it is to use Computer Vision.
The term itself sounds intimidating, it might make you think that you need a PhD in Machine Learning.
Why is it useful?
Computer Vision can be used to detect objects in images.
If you have an Arduino, you can build your own self-driving toy car that detects the road signs.
It can also be used to detect differences between images.
This second use case can be extremely useful for developers who want to do automated visual testing.
Let’s focus on that one.
Detecting differences between images
The entire concept is dead simple, we just compare each pixel and we calculate the percentage of different pixels.
We’ll just need to make sure our system has Python, OpenCV, scikit-image, Pillow and imutils.
You can find instructions for installing OpenCV here.
As for the rest, you can just use pip:
pip install scikit-image
pip install imutils
pip install pillow
First, we need to make sure that the images have the same size.
The quickest way to achieve that is just to resize the bigger one.
import argparse
import imutils
import cv2
from PIL import Image
from PIL import ImageFilter
from PIL import ImageDraw
image1 = Image.open("/path/to/image1.png")
image2 = Image.open("/path/to/image2.png")
image1_width, image1_height = image1.size
image2_width, image2_height = image2.size
image1_surface = image1_width * image1_height
image2_surface = image2_width * image2_height
if image1_surface != image2_surface:
if image1_surface > image2_surface:
image1 = image1.resize((image2_width, image2_height), Image.ANTIALIAS)
image1.save("/path/to/image1.png")
if image2_surface > image1_surface:
image2_surface = image2_surface.resize((image1_width, image1_height), Image.ANTIALIAS)
image2.save("/path/to/image2.png")
Now our images have the same size.
#software-testing #programming #python #test-automation #testing #code-quality #computer-vision #arduino