Django Filefield or imagefield - checking equality with another image or file

1.8k views Asked by At

I have a model with an ImageField and there are around 100 instances of the model in the database. Some of these instances have the same bitmap image (but saved in different path locations, path = obj.first_name + obj.date_of_creation).

I want to run a search for all obj.images and if the content (in bitmap sense), is same as a new Image (that I would specify), I want to replace the obj. image with the new image. How do I check this equality at file level/bitmap level in Django?

Thanks

1

There are 1 answers

0
rafaponieman On

The only real way to do that is generating a hash of each file, and looking for matching hashes. You can add another field on your model to store the image hash. Then run though all the images you already have, and generate the hashes, and then work with those images whose hashes match.

Here you can check how to make this hashes: How to get the filesize and SHA-1 digest of an uploaded file?