CHKim

pixel2text.py

Dec 14th, 2020
1,525
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. # PIL 은 Pillow 라이브러리를 설치해야한다. (using PIL need to pillow library) pip install pillow
  2. # python3 pixel2text.py <image filename> [output filename]
  3. # output filename은 생략 하면 output.txt 로 지정됨
  4.  
  5. import sys
  6.  
  7. from PIL import Image
  8. from PIL import ImageOps
  9.  
  10. def main(argv):
  11.     srcfilename = argv[0]
  12.     outfilename = 'output.txt'
  13.     if len(argv) > 1:
  14.         outfilename = argv[1]
  15.  
  16.     # 이미지를 열고 -
  17.     with Image.open(argv[0]) as im:
  18.         width, height = im.size
  19.         px = im.load()
  20.  
  21.         print(f'image size {width} x {height}')
  22.  
  23.         outfile = open(outfilename,'w')
  24.  
  25.         for y in range(height):
  26.             for x in range(width):
  27.                 hexcolor = '#%02x%02x%02x' % px[x, y]
  28.                 outfile.write(f'{x},{y},{hexcolor}\n')
  29.        
  30.         outfile.close()
  31.  
  32. if __name__ == "__main__":
  33.    main(sys.argv[1:])
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment