Projects STRLCPY dummy Commits 31b5ece6
🤬
  • Larger text size to be drawn on the image

  • Loading...
  • tkmru committed 7 months ago
    31b5ece6
    1 parent 7fd68cd0
  • ■ ■ ■ ■ ■ ■
    dummy/cli.py
    skipped 14 lines
    15 15  def make_jpeg(file_path, text):
    16 16   image = Image.new('RGB', (729, 516), (255, 255, 255)) # B5, White
    17 17   draw = ImageDraw.Draw(image)
    18  - draw.text((10, 10), text, fill=(0, 0, 0)) # Black
     18 + # Pillow's builtin font has a static size, so I used Arial
     19 + # https://github.com/python-pillow/Pillow/issues/2695
     20 + arial = ImageFont.truetype('Arial.ttf', 30)
     21 + draw.text((10, 10), text, fill=(0, 0, 0), font=arial) # Black
    19 22   image.save(file_path, format='jpeg')
    20 23   return True
    21 24   
    22 25  def make_png(file_path, text, byte_size):
    23 26   image = Image.new('RGB', (729, 516), (255, 255, 255)) # B5, White
    24 27   draw = ImageDraw.Draw(image)
    25  - draw.text((10, 10), text, fill=(0, 0, 0)) # Black
     28 + # Pillow's builtin font has a static size, so I used Arial
     29 + # https://github.com/python-pillow/Pillow/issues/2695
     30 + arial = ImageFont.truetype('Arial.ttf', 30)
     31 + draw.text((10, 10), text, fill=(0, 0, 0), font=arial) # Black
    26 32   output = io.BytesIO()
    27 33   image.save(output, format='png')
    28 34   png_data = output.getvalue()
    skipped 1 lines
    30 36   if (byte_size == None) or (len(png_data) > byte_size):
    31 37   with open(file_path, 'wb') as f:
    32 38   f.write(png_data)
    33  - return False
     39 + return True
    34 40   
    35 41   # IEND chunk is the last chunk of a PNG file
    36 42   iend_type_index = png_data.find(b'IEND')
    skipped 32 lines
    69 75   if byte_str == None:
    70 76   return None
    71 77   
    72  - if byte_str.endswith(('B', 'b')):
    73  - return int(byte_str[:-1])
    74  - elif byte_str.endswith(('KB', 'Kb', 'kB', 'kb')):
     78 + if byte_str.endswith(('KB', 'Kb', 'kB', 'kb')):
    75 79   return int(byte_str[:-2]) * 1024
    76 80   elif byte_str.endswith(('MB', 'Mb', 'mB', 'mb')):
    77 81   return int(byte_str[:-2]) * 1024 * 1024
    78 82   elif byte_str.endswith(('GB', 'Gb', 'gB', 'gb')):
    79 83   return int(byte_str[:-2]) * 1024 * 1024 * 1024
     84 + elif byte_str.endswith(('B', 'b')):
     85 + return int(byte_str[:-1])
    80 86   else:
    81 87   try:
    82 88   return int(byte_str)
    skipped 9 lines
    92 98   parser.add_argument('-b', '--bytes', help='Bytes of file(.png only)')
    93 99   
    94 100   args = parser.parse_args()
     101 + if args.bytes is not None and not args.file_path.endswith('.png'):
     102 + print(Fore.RED + 'Error: -b option is only available for .png.')
     103 + return
     104 + 
    95 105   if args.file_path.endswith('.jpeg') or args.file_path.endswith('.jpg'):
    96 106   make_jpeg(args.file_path, args.text)
    97  - print(Fore.GREEN + 'Successfully generated' + args.file_path)
     107 + print(Fore.GREEN + 'Successfully generated: ' + args.file_path)
    98 108   
    99 109   elif args.file_path.endswith('.png'):
    100 110   if make_png(args.file_path, args.text, parse_bytes(args.bytes)):
    101  - print(Fore.GREEN + 'Successfully generated' + args.file_path)
     111 + print(Fore.GREEN + 'Successfully generated: ' + args.file_path)
    102 112   else:
    103  - print(Fore.RED + 'Failed to generate file.')
     113 + print(Fore.RED + 'Failed to generate file...')
    104 114   
    105 115   elif args.file_path.endswith('.pdf'):
    106 116   make_pdf(args.file_path, args.text)
    107  - print(Fore.GREEN + 'Successfully generated' + args.file_path)
     117 + print(Fore.GREEN + 'Successfully generated: ' + args.file_path)
    108 118   
    109 119   else:
    110 120   print(Fore.RED + 'Error: Invalid file extension.')
    111 121   
    112  - 
Please wait...
Page is in error, reload to recover