This is the system in which we search for a match for an image. We select a game for a person by using hash coding. We check the hash code difference between the two images. Image with a minimum contrast gets selected and finally shown.
OS: for using an operating system.
Sys: for providing various functions of the system.
Glob: used to return all file paths that match a specific pattern.
Imagehash: used to convert the Image into hash code.
PIL: Python Imaging Library (expansion of PIL) is an image processing package. It incorporates lightweight image processing tools that aid in editing, creating, and saving images.
Firstly, we run a file named resized1.py (that we used to resize all our images in a folder and change them into one extension like here it is .jpg).
- Import the libraries.
- Give a path of a directory to a program.
- We use listdir to get the list of all files and directories in the specified directory.
- We make a function named resize () in which we run a for loop, and one by one, we select Image from the directory.
- In that, I use if loop, open that Image, and use the method splitext to split a string into text.
- Then, using the resize method, we resize our Image by giving the dimension we require (here, it is 400X400). (Here, one parameter given in resize is Image.ANTIALIAS that a technique used in digital imaging to reduce the visual defects that occur when high-resolution images are present in a lower resolution.)
- Here we are done and get our final resized Image in that directory.
Secondly, we run our main program that is match.py, after resizing all our images.
- Import the libraries.
- To open the Image (that we want to search for a perfect match for), we give a path of our directory to a program.
- We use the average_hash method from the image hash to get the hashcode of our Image.
- To extract the multiple files of the same pattern, we use glob. glob.
- We make a list named girls [0] and a variable named accepted_difference = 1000.
- Then we run a for loop to extract Images from a directory and get their hash code.
- We make a difference between the hashcode of the Image (that we want to search for a perfect match) and the images (in the directory we are finding).
- Then we use an if loop inside for loop and check the condition if (diff< accepted_diff) then that Image has been selected.
- Then we create a new frame to open both images and save them with a specific name (here, it is my_valentine_dat_date.jpg).
- Finally, we use img. show () to show or open that, Image.