Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. from django.shortcuts import render
  2. from image.models import fva
  3. from PIL import Image
  4. from PIL import ImageStat
  5. from PIL import ImageFilter
  6. from math import log
  7. import mysql.connector
  8.  
  9. def homepage(request):
  10. return render(request,'index.html',{})
  11.  
  12. def rootmeansquare( im_file ):
  13. im = Image.open(im_file).convert('L')
  14. stat = ImageStat.Stat(im)
  15. return stat.rms[0]
  16.  
  17. def average( im_file ):
  18. im = Image.open(im_file)
  19. stat = ImageStat.Stat(im)
  20. return stat.mean[0]
  21.  
  22. def varaince( im_file ):
  23. im = Image.open(im_file)
  24. stat = ImageStat.Stat(im)
  25. return stat.var[0]
  26.  
  27. def standarddeviation( im_file ):
  28. im = Image.open(im_file)
  29. stat = ImageStat.Stat(im)
  30. return stat.stddev[0]
  31.  
  32. def edgedetection( im_file ):
  33. im = Image.open(im_file)
  34. im = im.filter(ImageFilter.FIND_EDGES)
  35. stat = ImageStat.Stat(im)
  36. return stat.mean[0]
  37.  
  38. def histogramvalue( im_file):
  39. im = Image.open(im_file)
  40. hist=im.histogram()
  41. sumhist=sum(hist)
  42. lenhist=len(hist)
  43. avghist=float(sumhist/lenhist)
  44. return avghist
  45.  
  46. def entropy( im_file ):
  47. img = Image.open(im_file)
  48. histogram=img.histogram()
  49. log2 = lambda x:log(x)/log(2)
  50.  
  51. total = len(histogram)
  52. counts = {}
  53. for item in histogram:
  54. counts.setdefault(item,0)
  55. counts[item]+=1
  56.  
  57. ent = 0
  58. for i in counts:
  59. p = float(counts[i])/total
  60. ent-=p*log2(p)
  61. return -ent*log2(1/ent)
  62.  
  63. def upload(request):
  64. filename = request.FILES['search_field']
  65.  
  66. #return render(request,'temp.html',{'A': filename})
  67. #conn=mysql.connector.connect(user="root",password="28091994",host="localhost",database="images")
  68. #mycursor=conn.cursor()
  69.  
  70.  
  71. rms=rootmeansquare(filename)
  72. var=varaince(filename)
  73. #sd=standarddeviation(filename)
  74. #edge=edgedetection(filename)
  75. #histv=histogramvalue(filename)
  76. #ev=entropy(filename)
  77. #fvsum=rms+sd+edge+histv+ev
  78. fvavg=float(rms/7)
  79. #mycursor.execute("INSERT INTO fva(img,fvavg) VALUES (%s, %s)", (filename, fvavg))
  80. #conn.commit()
  81. #a=fva(img=filename,fvavg=fvavg)
  82. #a.save()
  83. return render(request,'index.html',{})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement