Click here to Skip to main content
15,904,823 members

Comments by rain-13 (Top 1 by date)

rain-13 30-Jul-23 5:33am View    
Thanks. That helped. How did I not think of it.
Seems like Either PIL or imagehash are not thread safe.
__init__
Traceback (most recent call last):
File "c:\code\threading_test.py", line 17, in computeHash
return hashFunction(image, hashSize)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\imagehash\__init__.py", line 248, in average_hash
image = image.convert('L').resize((hash_size, hash_size), ANTIALIAS)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\Image.py", line 933, in convert
self.load()
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\ImageFile.py", line 266, in load
raise OSError(msg)
OSError: image file is truncated (167 bytes not processed)

to solve this I had to use image_copies = [image.copy() for _ in range(4)] and pass these instead.

So this is the changed part. The rest of code is not changed:
                    image_copies = [image.copy() for _ in range(4)]
                    future_dhash10 = executor.submit(self.computeHash, image, imagehash.dhash, 10)
                    future_averageHash10 = executor.submit(self.computeHash, image_copies[0], imagehash.average_hash, 10)
                    future_phash10 = executor.submit(self.computeHash, image_copies[1], imagehash.phash, 10)
                    future_whash16 = executor.submit(self.computeHash, image_copies[2], imagehash.whash, 16)
                    future_phashSimple8 = executor.submit(self.computeHash, image_copies[3], imagehash.phash_simple, 8)
                    future_md5 = executor.submit(self.compute_md5_hash, imagePath)


Now the next question is why it's not using 100% cpu? It's using like 12 to 16 % instead. I am not sure should I continue this discussion here and post solution once all is done or should I post this solution under "Add your solution here" and start new topic to get 100% cpu utilization?