Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- from operator import itemgetter, attrgetter
- from optparse import OptionParser
- import sys
- parser = OptionParser()
- parser.add_option("-n", "--new", action="store_false", dest="stylevintage", default=False, help="New(Modern) Card Style")
- parser.add_option("-o", "--old", action="store_true", dest="stylevintage", default=False, help="Old(Vintage) Card Style")
- parser.add_option("-f", "--file", action="store", type="string", dest="filename", help="Input Filename")
- parser.add_option("--histo", action="store_true", dest="histo", default=False, help="Display Histogram Color Table")
- parser.add_option("-q", "--quiet", action="store_false", dest="verbose", default=True, help="Quiet Mode(No Debug Info)")
- parser.add_option("-O", "--output", action="store_true", dest="output", default=False, help="Output image files from recognition process")
- parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=True, help="Verbose")
- (options, args) = parser.parse_args()
- im = Image.open(options.filename)
- im = im.convert("P")
- def findCropBorders():
- if options.verbose == True:
- print "#####"
- print "Image Format, Mode"
- print im.format,",",im.mode
- print "Image Size(pixels)"
- print "W:",im.size[0]," H:",im.size[1]
- print "#####"
- ##//Attempt to find border thickness
- wborder = (im.size[0] * 0.048)
- hborder = (im.size[1] * 0.035)
- avgborder = ((wborder + hborder) / 2)
- borderthickness = round(avgborder, 0)
- if options.verbose == True:
- print "#####"
- print "Card border should be about:"
- print "Width(rounded)(actual): ",round(wborder, 0),wborder
- print "Height(rounded)(actual): ",round(hborder, 0),hborder
- print "Avg: ",avgborder
- print "Projected Border Thickness: ",borderthickness
- print "#####"
- ##//Crop Border
- borderboxleft = int(0 + borderthickness)
- borderboxupper = int(0 + borderthickness)
- borderboxright = int(im.size[0] - borderthickness)
- borderboxlower = int(im.size[1] - borderthickness)
- noborderbox = (borderboxleft, borderboxupper, borderboxright, borderboxlower)
- global noborder
- noborder = im.crop(noborderbox)
- borderlessoutfile = str("borderless_"+options.filename+".gif")
- if options.output == True:
- noborder.save(borderlessoutfile, 'gif')
- if options.verbose == True:
- print "#####"
- print "Border box cropping dimensions:"
- print "Left: ",borderboxleft,"Upper: ",borderboxupper,"Right: ",borderboxright,"Lower: ",borderboxlower
- if options.output == True:
- print "Saved border cropped image as ",borderlessoutfile
- print "#####"
- def histogram():
- ##//Histogram Color Table
- his = noborder.histogram()
- values = {}
- if options.histo == True:
- if options.verbose == True:
- print "#####"
- print "Top 5 Color Table"
- print "(ColorID Frequency):"
- for i in range(256):
- values[i] = his[i]
- for j,k in sorted(values.items(), key=itemgetter(1), reverse=True)[:5]:
- if options.verbose == True:
- print j,k
- if options.verbose == True:
- print "#####"
- ##//Card Style
- if options.verbose == True:
- print "#####"
- print "Card Style Vintage(True//False):"
- print options.stylevintage
- print "#####"
- def findCropNameCost():
- ##//Attempt to find name box dimensions
- if options.stylevintage == True:
- nameboxhpercent = 0.061
- #if options.stylevintage == False:
- # nameboxhpercent = int()
- hnamebox = (noborder.size[1] * nameboxhpercent)
- nameboxthickness = round(hnamebox, 0)
- if options.verbose == True:
- print "#####"
- print "Name Box Dimensions:"
- print "Heighth(rounded)(actual): ",nameboxthickness,hnamebox
- print "#####"
- ##//Crop name box
- nameboxleft = int(0)
- nameboxupper = int(0)
- nameboxright = int(noborder.size[0])
- nameboxlower = int(0 + nameboxthickness)
- cardnamebox = (nameboxleft, nameboxupper, nameboxright, nameboxlower)
- cardname = noborder.crop(cardnamebox)
- nameboxoutfile = str("namebox_"+options.filename+".gif")
- if options.output == True:
- cardname.save(nameboxoutfile, 'gif')
- if options.verbose == True:
- print "#####"
- print "Saved namebox output to ",nameboxoutfile
- print "#####"
- findCropBorders()
- findCropNameCost()
- histogram()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement