Пример #1
0
def calculate_hist_and_id(path, t, flag):
    """
    This is a modified trusty hist function
    """
    try:
        image = Image.open(path)
    except IOError:
        print "Error Opening Image file (PIL)"

    try:
        hist = image.histogram()
    except:
        print "Unexpected error:", sys.exc_info()
        exit(0)
        
    hist16bin = []

    start = 0
    end = 16

    bin_count = 0
    hist16bin.append(0)

    for i in range(len(hist)):
        if i % 16 == 0:
            
            start = i
            end = start + 16
            if bin_count == 15:
                break
            bin_count += 1
            hist16bin.append(0)
            
        else:
            hist16bin[bin_count] += hist[i]
    
    # Now we put the histogram in the database
    if flag == True:
        normal = Histograms()
        normal.bin0 = hist16bin[0]
        normal.bin1 = hist16bin[1]
        normal.bin2 = hist16bin[2]
        normal.bin3 = hist16bin[3]
        normal.bin4 = hist16bin[4]
        normal.bin5 = hist16bin[5]
        normal.bin6 = hist16bin[6]
        normal.bin7 = hist16bin[7]
        normal.bin8 = hist16bin[8]
        normal.bin9 = hist16bin[9]
        normal.bin10 = hist16bin[10]
        normal.bin11 = hist16bin[11]
        normal.bin12 = hist16bin[12]
        normal.bin13 = hist16bin[13]
        normal.bin14 = hist16bin[14]
        normal.bin15 = hist16bin[15]
        normal.hist_type = t
        normal.is_video = 'y'
        normal.save()

    im = Histograms.objects.all()
    id = 0
    l = len(im)

    if len(im) > 0:
        id = im[l - 1].id

    return [id, hist16bin]
Пример #2
0
def calculate_hist(path, t, flag):
    """ Returns a length 16 list
        The third parameter is a flag.  It says that if it's set
        to false, we should not put the calcuated histograms in the
        database.  And we should if it's true.  This came about because
        of the fact that search image histograms do not need to be put in the database
        while uploaded images do.  And since I use the same function to calculate the
        histograms there is no need to rewrite code, just set a flag."""
    try:
        image = Image.open(path)
    except IOError:
        print "Error Opening Image file (PIL)"

    #print "Format: ", image.format, " Size: ", image.size, " Mode: ", image.mode

    # This is so neat!  A histogram method!  Hopefully the professor doesn't disapprove.
    
    #print "Histogram: ", image.histogram()

    # However, we must make it a 16 bin historgram.
    try:
        hist = image.histogram()
    except:
        print "Unexpected error:", sys.exc_info()
        exit(0)
        
    hist16bin = []

    start = 0
    end = 16

    bin_count = 0
    hist16bin.append(0)

    for i in range(len(hist)):
        if i % 16 == 0:
            
            start = i
            end = start + 16
            if bin_count == 15:
                break
            bin_count += 1
            hist16bin.append(0)
            
        else:
            hist16bin[bin_count] += hist[i]
            
    print "\n\n"
    print "16 Bin histogram: ", hist16bin
    print 
    print "Size: ", len(hist16bin)
    print "Original Size: ", len(hist)
    
    # Now we put the histogram in the database
    if flag == True:
        normal = Histograms()
        normal.bin0 = hist16bin[0]
        normal.bin1 = hist16bin[1]
        normal.bin2 = hist16bin[2]
        normal.bin3 = hist16bin[3]
        normal.bin4 = hist16bin[4]
        normal.bin5 = hist16bin[5]
        normal.bin6 = hist16bin[6]
        normal.bin7 = hist16bin[7]
        normal.bin8 = hist16bin[8]
        normal.bin9 = hist16bin[9]
        normal.bin10 = hist16bin[10]
        normal.bin11 = hist16bin[11]
        normal.bin12 = hist16bin[12]
        normal.bin13 = hist16bin[13]
        normal.bin14 = hist16bin[14]
        normal.bin15 = hist16bin[15]
        normal.hist_type = t
        normal.save()

    return hist16bin