Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. from PIL import Image
  2. from operator import itemgetter, attrgetter
  3. from optparse import OptionParser
  4. import sys
  5.  
  6.  
  7. parser = OptionParser()
  8. parser.add_option("-n", "--new", action="store_false", dest="stylevintage", default=False, help="New(Modern) Card Style")
  9. parser.add_option("-o", "--old", action="store_true", dest="stylevintage", default=False, help="Old(Vintage) Card Style")
  10. parser.add_option("-f", "--file", action="store", type="string", dest="filename", help="Input Filename")
  11. parser.add_option("--histo", action="store_true", dest="histo", default=False, help="Display Histogram Color Table")
  12. parser.add_option("-q", "--quiet", action="store_false", dest="verbose", default=True, help="Quiet Mode(No Debug Info)")
  13. parser.add_option("-O", "--output", action="store_true", dest="output", default=False, help="Output image files from recognition process")
  14. parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=True, help="Verbose")
  15.  
  16. (options, args) = parser.parse_args()
  17.  
  18. im = Image.open(options.filename)
  19. im = im.convert("P")
  20.  
  21. def findCropBorders():
  22. if options.verbose == True:
  23. print "#####"
  24. print "Image Format, Mode"
  25. print im.format,",",im.mode
  26. print "Image Size(pixels)"
  27. print "W:",im.size[0]," H:",im.size[1]
  28. print "#####"
  29. ##//Attempt to find border thickness
  30. wborder = (im.size[0] * 0.048)
  31. hborder = (im.size[1] * 0.035)
  32. avgborder = ((wborder + hborder) / 2)
  33. borderthickness = round(avgborder, 0)
  34. if options.verbose == True:
  35. print "#####"
  36. print "Card border should be about:"
  37. print "Width(rounded)(actual): ",round(wborder, 0),wborder
  38. print "Height(rounded)(actual): ",round(hborder, 0),hborder
  39. print "Avg: ",avgborder
  40. print "Projected Border Thickness: ",borderthickness
  41. print "#####"
  42.  
  43. ##//Crop Border
  44. borderboxleft = int(0 + borderthickness)
  45. borderboxupper = int(0 + borderthickness)
  46. borderboxright = int(im.size[0] - borderthickness)
  47. borderboxlower = int(im.size[1] - borderthickness)
  48. noborderbox = (borderboxleft, borderboxupper, borderboxright, borderboxlower)
  49. global noborder
  50. noborder = im.crop(noborderbox)
  51. borderlessoutfile = str("borderless_"+options.filename+".gif")
  52. if options.output == True:
  53. noborder.save(borderlessoutfile, 'gif')
  54. if options.verbose == True:
  55. print "#####"
  56. print "Border box cropping dimensions:"
  57. print "Left: ",borderboxleft,"Upper: ",borderboxupper,"Right: ",borderboxright,"Lower: ",borderboxlower
  58. if options.output == True:
  59. print "Saved border cropped image as ",borderlessoutfile
  60. print "#####"
  61.  
  62. def histogram():
  63. ##//Histogram Color Table
  64. his = noborder.histogram()
  65. values = {}
  66. if options.histo == True:
  67. if options.verbose == True:
  68. print "#####"
  69. print "Top 5 Color Table"
  70. print "(ColorID Frequency):"
  71. for i in range(256):
  72. values[i] = his[i]
  73. for j,k in sorted(values.items(), key=itemgetter(1), reverse=True)[:5]:
  74. if options.verbose == True:
  75. print j,k
  76. if options.verbose == True:
  77. print "#####"
  78.  
  79. ##//Card Style
  80. if options.verbose == True:
  81. print "#####"
  82. print "Card Style Vintage(True//False):"
  83. print options.stylevintage
  84. print "#####"
  85. def findCropNameCost():
  86. ##//Attempt to find name box dimensions
  87. if options.stylevintage == True:
  88. nameboxhpercent = 0.061
  89. #if options.stylevintage == False:
  90. # nameboxhpercent = int()
  91. hnamebox = (noborder.size[1] * nameboxhpercent)
  92. nameboxthickness = round(hnamebox, 0)
  93. if options.verbose == True:
  94. print "#####"
  95. print "Name Box Dimensions:"
  96. print "Heighth(rounded)(actual): ",nameboxthickness,hnamebox
  97. print "#####"
  98.  
  99. ##//Crop name box
  100. nameboxleft = int(0)
  101. nameboxupper = int(0)
  102. nameboxright = int(noborder.size[0])
  103. nameboxlower = int(0 + nameboxthickness)
  104. cardnamebox = (nameboxleft, nameboxupper, nameboxright, nameboxlower)
  105. cardname = noborder.crop(cardnamebox)
  106. nameboxoutfile = str("namebox_"+options.filename+".gif")
  107. if options.output == True:
  108. cardname.save(nameboxoutfile, 'gif')
  109. if options.verbose == True:
  110. print "#####"
  111. print "Saved namebox output to ",nameboxoutfile
  112. print "#####"
  113.  
  114. findCropBorders()
  115. findCropNameCost()
  116. histogram()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement