Last active
February 28, 2024 03:13
-
-
Save kirkmawa/9bb2f19e13451cd826f34b7d1fd0f887 to your computer and use it in GitHub Desktop.
Python script to resize and encode images for use with the Station Logo service on HD Radio broadcasts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Requires Python 3.6+ with PIL | |
# hdradio_logo_correction.py: Utility to resize and encode images | |
# for use with the Station Logo service on HD Radio broadcasts | |
# Copyright (C) 2023-2024 W. Andrew Kirkman <drew@broadcastblueprint.com> | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <https://www.gnu.org/licenses/>. | |
usage_message = '''\ | |
Usage: | |
hdradio_logo_correction.py --callsign XXXX --channel 1 --logo logo_filename.jpg [--output_dir /path/to/output] | |
Args: | |
--callsign XXXX | |
[Required] Station call sign, without -FM or -AM suffix if there is one | |
--channel 1 | |
[Required] HD Radio channel ID, number only (e.g. 3 for HD3) | |
--logo logo_filename.jpg | |
[Required] Path to logo filename to process | |
--output_dir /path/to/output | |
[Optional] Destination folder of finished logo image, defaults to current working directory''' | |
import PIL.Image | |
import sys, os | |
# Important variables | |
script_path = sys.path[0] | |
cwd = os.getcwd() | |
# Load arguments list | |
args = sys.argv[1:] | |
# Arguments list should have an even number of elements | |
if len(args) % 2 != 0: | |
sys.exit(f'Error: Check arguments - odd number of elements given\n\n{usage_message}') | |
# Make sure all required flags are present | |
if not set(['--callsign', '--channel', '--logo']).issubset(set(args)): | |
sys.exit(f'Error: Not all required arguments present\n\n{usage_message}') | |
# Find call sign and load it | |
if '--callsign' in args: | |
station_call = args[args.index('--callsign') + 1] | |
print(f'Station call sign is {station_call}') | |
else: | |
# Call sign was not given | |
sys.exit(f'Error: Missing required argument --callsign\n\n{usage_message}') | |
# Find channel number and load it | |
if '--channel' in args: | |
channel_number = args[args.index('--channel') + 1] | |
# Make sure number fits HD1 through HD8 | |
if not 1 <= int(channel_number) <= 8: | |
sys.exit(f'Channel ID {channel_number} out of range (expected 1 through 8)\n\n{usage_message}') | |
else: | |
print(f'Channel ID is HD{channel_number}') | |
else: | |
# Channel ID was not given | |
sys.exit(f'Missing required argument --channel\n\n{usage_message}') | |
# Find logo argument | |
if '--logo' in args: | |
logo_filename = os.path.realpath(args[args.index('--logo') + 1]) | |
# Check if file exists | |
if not os.path.exists(logo_filename): | |
sys.exit(f'Error: file {logo_filename} does not exist\n\n{usage_message}') | |
else: | |
print(f'Found file {logo_filename}') | |
f = open(logo_filename, 'rb') | |
else: | |
#Logo filename was not given | |
sys.exit(f'Missing required argument --logo\n\n{usage_message}') | |
# Handle optional --output-dir argument | |
if '--output_dir' in args: | |
output_dir = os.path.realpath(args[args.index('--output_dir') + 1]) | |
# Check if path exists and is a directory | |
if os.path.exists(output_dir): | |
if not os.path.isdir(output_dir): | |
# Default to current working directory | |
print(f'Warning: output destination {output_dir} is not a directory, defaulting to CWD') | |
output_dir = cwd | |
else: | |
# Destination directory doesn't exist | |
print(f'Warning: output destination {output_dir} does not exist, defaulting to CWD') | |
output_dir = cwd | |
else: | |
# Use default | |
print(f'Saving finished image to current directory') | |
output_dir = cwd | |
# Process image | |
ip = PIL.Image.open(f) | |
ip = ip.convert('RGB') | |
# Resize image to 200x200 | |
ip = ip.resize((200,200)) | |
final_filename = f'{output_dir}/SL{station_call}$$0{channel_number}0001.jpg' | |
out = open(final_filename, 'wb') | |
ip.save(out, 'jpeg', progressive=False, subsampling=2, quality=75) | |
print(f'Finished! Processed image available at {final_filename}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment