0

I have some url of images in my database, and i want to compare them with the images on my local without download them.

i read this example and this question and i tried this

adr = "url_of_image"
file = cStringIO.StringIO(urllib.urlopen(adr).read())
img = Image.open(file)
img = str(img)
print type(img)

image_file = open('adresse_of_image_in_local').read()
print type(image_file)


if ( img == image_file):
    print "the pictures of the same"
else :
    print "they are not the same"

i test this code for the same image, but i got this

<type 'str'>
<type 'str'>
they are not the same

My question is how can i compare an image on local with an image on web without saving?

parik
  • 2,313
  • 12
  • 39
  • 67

1 Answers1

1

You cannot avoid downloading the image from the net, else you'll have nothing to compare to. You can download it entirely into RAM, though, if you e.g. can't write to disk. This is what you already do correctly.

== on image objects won't work, though. Try doing Image.open() on your local image, too. Then compare what .getbbox() returns on both of them. If the sizes matched, try comparing what .tobytes() returns.

9000
  • 39,899
  • 9
  • 66
  • 104
  • It is actually possible to compare the images without downloading them. You can [use a hash function](https://stackoverflow.com/questions/1761607/what-is-the-fastest-hash-algorithm-to-check-if-two-files-are-equal) to compare the two files, instead of downloading the entire file. – Anderson Green Oct 06 '21 at 17:01
  • @AndersonGreen: If the server with the image agrees to send you the hash, yes! But usually they don't. You can avoid _storing_ the entire file, but reduce it to a hash in a streaming fashion. This does not prevent you from actually downloading the file, that is, form spending the bandwidth. It can of course help you spend less RAM. – 9000 Oct 06 '21 at 17:45