Search code examples
pythonwebimage-processingweb-crawlerimage-manipulation

python: manipulate images online insted of downloading them


I am running this code to compare the similarity of two profile images belonging to two different social networks, i am first downloading the images from the two websites,and then for each user,i give the local images paths to two variables, i wonder if there is any alternative that allow me to manipulate those images online instead of downloading them, ie: giving the code image's URL instead of local path (i don't want to download the images on my local machine, cause it will take too much space when dealing with millions of them)

from PIL import Image
import imagehash
hash0 = imagehash.average_hash(Image.open('quora_photo.jpg')) 
hash1 = imagehash.average_hash(Image.open('twitter_photo.jpeg')) 
cutoff = 5

if hash0 - hash1 < cutoff:
    print('images are similar')
else:
    print('images are not similar')

Solution

  • Every time you see an image on your browser your machine has downloaded it before. It is not possible to manipulate an image without downloading it.

    Take a look at tempfile module, you may create temporary files to be sure that they will be deleted in the future. Or delete the files after you manipulate them as @ArnavBorborah said

    EDIT :

    Take a look at this method urllib.request.urlretrieve

    You can adapt their example :

    import urllib.request
    local_filename, headers = urllib.request.urlretrieve(<image_url>)
    with open(local_filename) as image:
        #do stuff
    

    The second argument, if present, specifies the file location to copy to (if absent, the location will be a tempfile with a generated name).

    If you do not specify the filename argument, urllib will create a temporary file