Skip to content

Commit

Permalink
Merge pull request #1 from truethari/beta
Browse files Browse the repository at this point in the history
version = 1.0.5
  • Loading branch information
truethari authored Apr 3, 2021
2 parents 4ef845c + 1d36998 commit da02fb9
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/thumb-gen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install pytest Pillow get-video-properties ffmpy ffmpeg-python
python -m pip install pytest Pillow get-video-properties ffmpy
sudo wget -P /home/ "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4"
sudo apt-get install ffmpeg
- name: Test with pytest
Expand Down
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@ You can use pip:

## Configurations

(These may change during the update)

- The number of screen images that should be included in the final thumbnail image

- Thumbnail image quality

- Font type in the video info panel. You can add a file path of a font file (.ttf) to this

- Font size in the video info panel

- Custom text in the video info panel
- Background color of the thumbnail (Hex codes are also supported)
- Font colour of the thumbnail (Hex codes are also supported)

Download font files : [FontSquirrel](https://www.fontsquirrel.com/)

Expand All @@ -54,6 +50,8 @@ IMAGE_QUALITY = 80
FONT =
FONT_SIZE = 30
CUSTOM_TEXT =
BG_COLOUR = white
FONT_COLOUR = black
```

## Usage
Expand Down Expand Up @@ -95,15 +93,15 @@ CUSTOM_TEXT =

### Python

- If you don't set an output folder, thumbnail images will be saved in the video folder (video_path).
- If you don't need a custom text and custom font file (including font size) and you have already set these for the configuration file (using console or defaults), it will be added automatically. To avoid this set the `custom_text` value to `False` and add a custom font file location.
- If you don't set an output folder, thumbnail images will be saved in the video folder (video_path).
- If you don't need a custom text and custom font file (including font size) and you have already set these for the configuration file (using console or defaults), it will be added automatically. To avoid this set the `custom_text` value to `False` and add a custom font file location.

#### Example 1

``` python
from thumb_gen.worker import Generator

#video_path, output_path='', custom_text=True, font_dir='', font_size=
#video_path, output_path='', custom_text=True, font_dir='', font_size=0, bg_colour='', font_colour=''
app = Generator("C:/input/video.mp4", "C:/output/", "www.example.com", "C:/Windows/Fonts/Arial.ttf", 30)
app.run()
```
Expand All @@ -117,6 +115,6 @@ from thumb_gen.worker import Generator
folder = 'C:/input'
for video in os.listdir(folder):
if video.endswith('.mp4') or video.endswith('.mkv'):
app = Generator(os.path.join(folder, video), custom_text=False, font_dir="C:/Project/font.ttf", font_size=25)
app = Generator(os.path.join(folder, video), custom_text=False, font_dir="C:/Project/font.ttf", font_size=25, bg_colour='blue', font_colour='red')
app.run()
```
17 changes: 7 additions & 10 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,13 @@ You can use pip:
Configurations
--------------

(These may change during the update)

- The number of screen images that should be included in the final thumbnail image.

- Thumbnail image quality

- Font type in the video info panel. You can add a file path of a font file (.ttf) to this.

- Font size in the video info panel

- Custom text in the video info panel
- Background color of the thumbnail (Hex codes are also supported)
- Font colour of the thumbnail (Hex codes are also supported)

Download font files : `FontSquirrel <https://www.fontsquirrel.com/>`_

Expand All @@ -63,6 +59,8 @@ By program default:
FONT =
FONT_SIZE = 30
CUSTOM_TEXT =
BG_COLOUR = white
FONT_COLOUR = black
-----
Usage
Expand Down Expand Up @@ -109,7 +107,6 @@ Python
======

- If you don't set an output folder, thumbnail images will be saved in the video folder (video_path).

- If you don't need a custom text and custom font file (including font size) and you have already set these for the configuration file (using console or defaults), it will be added automatically. To avoid this set the `custom_text` value to `False` and add a custom font file location.

Example 1
Expand All @@ -131,6 +128,6 @@ Example 2
folder = 'C:/input'
for video in os.listdir(folder):
if video.endswith('.mp4') or video.endswith('.mkv'):
app = Generator(os.path.join(folder, video), custom_text=False)
app.run()
if video.endswith('.mp4') or video.endswith('.mkv'):
app = Generator(os.path.join(folder, video), custom_text=False, font_dir="C:/Project/font.ttf", font_size=25, bg_colour='blue', font_colour='red')
app.run()
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ffmpy
get_video_properties
Pillow
44 changes: 35 additions & 9 deletions thumb_gen/application.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import os
import re
import sys
import datetime

from ffmpy import FFmpeg
from videoprops import get_video_properties
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from PIL import ImageColor

from .config import read_config
from .utils import listToString, video_info, get_file_size, convert_unit, packagePath
Expand Down Expand Up @@ -79,7 +81,7 @@ def lining(text, font, font_size, image_width):

return lines

def imageText(video_path, secure_tmp, bg_width, bg_height, custom_text, font_dir, font_size):
def imageText(video_path, secure_tmp, bg_width, bg_height, custom_text, font_dir, font_size, bg_colour, font_colour):
if font_dir == '':
font_name = read_config('font')
if font_name == '':
Expand All @@ -97,6 +99,12 @@ def imageText(video_path, secure_tmp, bg_width, bg_height, custom_text, font_dir
elif custom_text == 'False':
custom_text = ''

if bg_colour == '':
bg_colour = read_config('bg_colour')

if font_colour == '':
font_colour = read_config('font_colour')

filename = re.split(pattern = r"[/\\]", string = video_path)
filename = filename[-1]

Expand Down Expand Up @@ -183,7 +191,25 @@ def imageText(video_path, secure_tmp, bg_width, bg_height, custom_text, font_dir

bg_new_height = text_area_height + bg_height

img = Image.new('RGB', (bg_width, bg_new_height), color = 'white')
try:
bg_colour = ImageColor.getrgb(bg_colour)
except ValueError:
bg_colour = bg_colour
print("ValueError: unknown color specifier: {}".format(bg_colour))
print("This can be fixed by changing the configurations.\n" \
"Ex: Background colour = 'white' / Background colour = '#ffffff'")
sys.exit()

try:
font_colour = ImageColor.getrgb(font_colour)
except ValueError:
font_colour = font_colour
print("ValueError: unknown color specifier: {}".format(font_colour))
print("This can be fixed by changing the configurations.\n" \
"Ex: Font colour = 'black' / Font colour = '#ffffff'")
sys.exit()

img = Image.new('RGB', (bg_width, bg_new_height), bg_colour)
img.save(os.path.join(secure_tmp, 'bg.png'))

background = Image.open(os.path.join(secure_tmp, 'bg.png'))
Expand Down Expand Up @@ -211,21 +237,21 @@ def imageText(video_path, secure_tmp, bg_width, bg_height, custom_text, font_dir
for lines in info_filename_line['line{}'.format(rounds)]:
if lines != []:
font_height = font_info(info_filename, font_name, font_size)[1]
draw.text((x, y), lines, 'black', font=font)
draw.text((x, y), lines, font_colour, font=font)
y = y + font_height

font_height = font_info(info_filesize, font_name, font_size)[1]

#line2
draw.text((x, y), info_line2, 'black', font=font)
draw.text((x, y), info_line2, font_colour, font=font)
y = y + 5 + font_height

#line3
draw.text((x, y), info_line3, 'black', font=font)
draw.text((x, y), info_line3, font_colour, font=font)
y = y + 5 + font_height

#line4
draw.text((x, y), info_line4, 'black', font=font)
draw.text((x, y), info_line4, font_colour, font=font)
y = y + 5 + font_height

rounds = 0
Expand All @@ -236,7 +262,7 @@ def imageText(video_path, secure_tmp, bg_width, bg_height, custom_text, font_dir
for lines in text_lines['line{}'.format(rounds)]:
if lines != []:
font_height = font_info(lines, font_name, font_size)[1]
draw.text((x, y), lines, 'black', font=font)
draw.text((x, y), lines, font_colour, font=font)
y = y + font_height
y = y + 5

Expand Down Expand Up @@ -277,7 +303,7 @@ def resize(screenshot_folder, resize_folder):

return True

def thumb(video_path, output_folder, resize_folder, secure_temp, custom_text, font_dir, font_size):
def thumb(video_path, output_folder, resize_folder, secure_temp, custom_text, font_dir, font_size, bg_colour, font_colour):
for img in os.listdir(resize_folder):
image = Image.open(os.path.join(resize_folder, img))
r_new_width, new_height = image.size
Expand All @@ -292,7 +318,7 @@ def thumb(video_path, output_folder, resize_folder, secure_temp, custom_text, fo
bg_new_width = int((r_new_width * 3) + 20)
bg_new_height = int((new_height * img_rows) + ((5 * img_rows) + 5))

y = imageText(video_path, secure_temp, bg_new_width, bg_new_height, custom_text, font_dir, font_size)
y = imageText(video_path, secure_temp, bg_new_width, bg_new_height, custom_text, font_dir, font_size, bg_colour, font_colour)

backgroud = Image.open(os.path.join(secure_temp, 'bg.png'))

Expand Down
14 changes: 12 additions & 2 deletions thumb_gen/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .utils import get_datadir, CheckIfFileExists
from .version import config_version

def create_config(IMAGES=12, IMAGE_QUALITY=80, FONT='', FONT_SIZE=30, CUSTOM_TEXT=''):
def create_config(IMAGES=12, IMAGE_QUALITY=80, FONT='', FONT_SIZE=30, CUSTOM_TEXT='', BG_COLOUR='white', FONT_COLOUR='black'):
my_datadir = get_datadir() / "thumb-gen"

try:
Expand All @@ -16,7 +16,7 @@ def create_config(IMAGES=12, IMAGE_QUALITY=80, FONT='', FONT_SIZE=30, CUSTOM_TEX
finally:
configfile_path = os.path.join(str(my_datadir), "config.ini")
config_object = configparser.ConfigParser()
config_object["DEFAULT"] = {"images": IMAGES, "image_quality": IMAGE_QUALITY, "font":FONT, "font_size":FONT_SIZE, "custom_text":CUSTOM_TEXT}
config_object["DEFAULT"] = {"images": IMAGES, "image_quality": IMAGE_QUALITY, "font":FONT, "font_size":FONT_SIZE, "custom_text":CUSTOM_TEXT, "bg_colour":BG_COLOUR, "font_colour":FONT_COLOUR}
config_object["VERSION"] = {"config_version": config_version}
with open(configfile_path, 'w') as conf:
config_object.write(conf)
Expand Down Expand Up @@ -49,6 +49,12 @@ def modify_config(options, value):
else:
userinfo["custom_text"] = str(value)

elif options == "bg_colour":
userinfo["bg_colour"] = str(value)

elif options == "font_colour":
userinfo["font_colour"] = str(value)

with open(configfile_path, 'w') as conf:
config_object.write(conf)

Expand Down Expand Up @@ -96,6 +102,10 @@ def read_config(option):
return int(default['font_size'])
elif option == 'custom_text':
return str(default['custom_text'])
elif option == 'bg_colour':
return str(default['bg_colour'])
elif option == 'font_colour':
return str(default['font_colour'])
loop = False

except KeyError:
Expand Down
11 changes: 8 additions & 3 deletions thumb_gen/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def parseOpts(argument_list):
sys.exit()

elif opt in ("-c", "--config"):
conf_images, conf_image_quality, conf_font, conf_font_size, conf_custom_text = configurations()

conf_images, conf_image_quality, conf_font, conf_font_size, conf_custom_text, conf_bg_colour, conf_font_colour = configurations()
if conf_images != 0:
modify_config('images', conf_images)

Expand All @@ -55,9 +54,15 @@ def parseOpts(argument_list):
if conf_font_size != 0:
modify_config('font_size', conf_font_size)

if conf_custom_text != 0:
if conf_custom_text != '':
modify_config('custom_text', conf_custom_text)

if conf_bg_colour != '':
modify_config('bg_colour', conf_bg_colour)

if conf_font_colour != '':
modify_config('font_colour', conf_font_colour)

sys.exit()

def begin(args='', opt=''):
Expand Down
4 changes: 2 additions & 2 deletions thumb_gen/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "1.0.4"
config_version = "2021.03.25"
__version__ = "1.0.5"
config_version = "2021.04.03"
Loading

0 comments on commit da02fb9

Please sign in to comment.