123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- from PIL import Image
- from pprint import pprint
- import argparse, sys, time, pickle
- ON = 'X'
- OFF = '.'
- EMPTY = False
- hexbug = True
- INTENSITY = 64
- parser = argparse.ArgumentParser( description='Display Graphical Image.')
- parser.add_argument( 'FILENAME', help='Image file to process' )
- parser.add_argument( '--hex', help='Display output in hexadecimal', action="store_true" )
- parser.add_argument( '--all', help='Display all of the image.', action="store_true" )
- parser.add_argument( '-d', help='Display image.', action="store_true" )
- args = parser.parse_args()
- filename = args.FILENAME
- print("Loading", filename)
- im = Image.open(filename)
- pix = im.load()
- size = im.size
- print("size:",size[0],"x",size[1])
- if im.mode == 'P':
- pal = im.getpalette()
-
- def img_point(x,y):
- global pix
- return pix[x,y]
-
- def img_average(x,y):
- global im, pal
- rgb = img_point(x,y)
-
-
- if im.mode == 'P':
-
- rgb = pal[rgb*3:(rgb+1)*3]
-
- if im.mode == 'I':
- return rgb >> 8
- return int( ( rgb[0] + rgb[1] + rgb[2] ) / 3 )
- def is_set(x,y):
- avg = img_average(x,y)
- return (avg < INTENSITY )
- sx = 0
- ex = size[0]
- sy = 0
- ey = size[1]
- total = 0
- def scan_image():
- global sx, ex, sy, ey, total
- total = 0
-
-
- sx = size[0]; ex = 0
- sy = size[1]; ey = 0
- for y in range( 0,size[1] ):
- for x in range( 0,size[0] ):
-
- pnt_is = is_set(x,y)
- if (pnt_is):
- total += 1
-
- if x < sx:
- sx = x
- if x > ex:
- ex = x
- if y < sy:
- sy = y
- if y > ey:
- ey = y
-
-
- if sx > 0:
- sx -= 1
- if ex < size[0]:
- ex += 1
- if sy > 0:
- sy -= 1
- if ey < size[1]:
- ey += 1
-
- return(sx,sy,ex,ey)
- scan_image()
- print("Character within ({0},{1}) - ({2},{3}) total {4} pixels...".format( sx, sy, ex, ey, total ))
- def display_image():
- global args
- if args.all:
- for y in range(0,size[1]):
- s = ''
- for x in range(0,size[0]):
- if args.hex:
- s += '{0:02X}'.format( img_average(x,y))
- else:
- if is_set(x,y):
- s += ON
- else:
- s += OFF
- print(s)
-
- else:
- for y in range(sy,ey):
- s = ''
- for x in range(sx,ex):
- if args.hex:
- s += '{0:02X}'.format( img_average(x,y) )
- else:
- if is_set(x,y):
- s += ON
- else:
- s += OFF
- print(s)
- print(" --")
- def save_image():
- global args
- result = []
- if args.all:
- for y in range(0,size[1]):
- s = ''
- for x in range(0,size[0]):
- if args.hex:
- s += '{0:02X}'.format( img_average(x,y))
- else:
- if is_set(x,y):
- s += ON
- else:
- s += OFF
- result.append(s)
-
- else:
- for y in range(sy,ey):
- s = ''
- for x in range(sx,ex):
- if args.hex:
- s += '{0:02X}'.format( img_average(x,y) )
- else:
- if is_set(x,y):
- s += ON
- else:
- s += OFF
- result.append(s)
-
- return result
- display_image()
- img_s = save_image()
- img_s.append('net')
- with open('out.txt', 'w') as f:
- for e in img_s
- f.write(e)
|