Skip to content
Snippets Groups Projects

New feature: change background

Open Imported Administrator requested to merge splash-bg into master
1 unresolved thread
1 file
+ 48
15
Compare changes
  • Side-by-side
  • Inline
+ 48
15
@@ -15,21 +15,42 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
from PIL import Image, ImageFont, ImageDraw
from PIL import Image, ImageFont, ImageDraw, ImageOps
import configparser
import os
import sys
import itertools
def make_splash(width, height, filename, landscape=False, text="", config_file=None, center=False, raw=False):
print("Creating ({}x{}) splashscreen {}".format(width, height, os.path.basename(filename)))
def make_splash(width, height, filename, landscape=False, text="",
config_file=None, center=False, raw=False):
print(f"Creating ({width}x{height})"
f" splashscreen {os.path.basename(filename)}")
config = configparser.ConfigParser()
if config_file is not None:
config.read(config_file)
im = Image.new('RGB', (width, height), color=config.get('background', 'color', fallback=0))
bg_color = config.get('background', 'color', fallback=0)
im = Image.new('RGB', (width, height), color=bg_color)
bg_file = config.get('background', 'image', fallback="")
if os.path.isfile(bg_file):
bg = Image.open(bg_file)
viewport = (0, 0, width, height)
bg_x, bg_y = bg.size
scale_ratio = min(bg_x / width, bg_y / height)
bg = ImageOps.scale(bg, 1 / scale_ratio)
x_gap = (bg.size[0] - width) // 2
y_gap = (bg.size[1] - height) // 2
for gap in [x_gap, y_gap]:
if gap < 0:
gap = 0
bg = bg.crop((x_gap, y_gap, bg.size[0] - x_gap, bg.size[1] - y_gap))
bg = bg.resize((width, height))
im.paste(bg, viewport)
draw = ImageDraw.Draw(im)
spacing = width / 100 * float(config.get('logo', 'spacing', fallback='0'))
@@ -52,10 +73,12 @@ def make_splash(width, height, filename, landscape=False, text="", config_file=N
name_width, name_height = draw.textsize(name, font=name_font)
logo_width, logo_height = draw.textsize(logo, font=logo_font)
draw.text(((width - (name_width + logo_width)) / 2 + logo_width + spacing, (height - name_height) / 2), name,
draw.text(((width - (name_width + logo_width)) / 2 + logo_width + spacing,
(height - name_height) / 2), name,
config.get('name', 'color', fallback='#ffffff'), font=name_font)
draw.text(((width - (name_width + logo_width + spacing)) / 2, (height - name_height) / 2), logo,
draw.text(((width - (name_width + logo_width + spacing)) / 2,
(height - name_height) / 2), logo,
config.get('logo', 'color', fallback='#009900'),
font=logo_font)
@@ -72,7 +95,9 @@ def make_splash(width, height, filename, landscape=False, text="", config_file=N
line_width, _ = draw.textsize(line, font=text_font)
text_left = (width / 2) - (line_width / 2)
draw.text((text_left, text_top), line, fill=config.get('text', 'color', fallback='#cccccc'),
text_color = config.get('text', 'color', fallback='#cccccc')
draw.text((text_left, text_top), line,
fill=text_color,
font=text_font)
text_top += line_height + (text_size * 0.4)
@@ -94,7 +119,8 @@ def check_font(config, section):
fallback_font = '/usr/share/fonts/ttf-dejavu/DejaVuSans.ttf'
font_file = config.get(section, 'font', fallback=fallback_font)
if not os.path.isfile(font_file):
print("Font file '{}' does not exist ({}).".format(font_file, section), file=sys.stderr)
print(f"Font file '{font_file}' does not exist ({section}).",
file=sys.stderr)
exit(1)
return font_file
@@ -123,21 +149,28 @@ def visual_split(text, font, width, response_type='list'):
elif response_type == 'str':
return '\n'.join(line.strip() for line in lines)
else:
raise ValueError('Invalid response type. Valid values are "list" and "str".')
raise ValueError('Invalid response type. Valid values are "list"'
' and "str".')
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description="Splash generator for postmarketOS")
parser = argparse.ArgumentParser(description="Splash generator"
" for postmarketOS")
parser.add_argument('width', type=int)
parser.add_argument('height', type=int)
parser.add_argument('filename')
parser.add_argument('--landscape', action='store_true', help='Rotate splash screen for landscape device')
parser.add_argument('--text', type=str, help='Additional text for the splash screen')
parser.add_argument('--landscape', action='store_true',
help='Rotate splash screen for landscape device')
parser.add_argument('--text', type=str,
help='Additional text for the splash screen')
parser.add_argument('--center', action='store_true', help='Center text')
parser.add_argument('--config', type=str, help='Config file for splash screen style')
parser.add_argument('--raw', action='store_true', help='Output raw rgb instead of ppm')
parser.add_argument('--config', type=str,
help='Config file for splash screen style')
parser.add_argument('--raw', action='store_true',
help='Output raw rgb instead of ppm')
args = parser.parse_args()
make_splash(args.width, args.height, args.filename, args.landscape, args.text, args.config, args.center, args.raw)
make_splash(args.width, args.height, args.filename, args.landscape,
args.text, args.config, args.center, args.raw)
Loading