Skip to content

Instantly share code, notes, and snippets.

@thypon
Created May 2, 2023 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thypon/9afca331d3f30e895e940421cfe0bef2 to your computer and use it in GitHub Desktop.
Save thypon/9afca331d3f30e895e940421cfe0bef2 to your computer and use it in GitHub Desktop.
Image Uploader for Samsung Art Displays, from HTTP(S) addresses
import sys
import logging
import os
import random
import json
import argparse
from PIL import Image
import requests
from io import BytesIO
import tempfile
sys.path.append('../')
from samsungtvws import SamsungTVWS
# Add command line argument parsing
parser = argparse.ArgumentParser(description='Upload images to Samsung TV.')
parser.add_argument('--tvip', help='TV IP', default='192.168.1.51')
parser.add_argument('--address', help='Remote Address for an artwork to be downloaded')
parser.add_argument('--remove-all', action='store_true', help='Delete all the artworks before uploading anything')
parser.add_argument('--debug', action='store_true', help='Enable debug mode to check if TV is reachable')
args = parser.parse_args()
# Increase debug level
logging.basicConfig(level=logging.INFO)
# Set your TVs local IP address. Highly recommend using a static IP address for your TV.
tv = SamsungTVWS(args.tvip)
# Check if TV is reachable in debug mode
if args.debug:
try:
logging.info('Checking if the TV can be reached.')
info = tv.rest_device_info()
logging.info('If you do not see an error, your TV could be reached.')
sys.exit()
except Exception as e:
logging.error('Could not reach the TV: ' + str(e))
sys.exit()
# Checks if the TV supports art mode
art_mode = tv.art().supported()
if art_mode == True:
if args.remove_all:
for artwork in tv.art().available():
tv.art().delete(artwork['content_id'])
try:
response = requests.get(args.address)
image = Image.open(BytesIO(response.content))
except Exception as error:
print('\n' + str(error))
sys.exit()
required_aspect = int(16) / int(9)
border = 0
width = image.size[0]
height = image.size[1]
aspect = width / height
if aspect > required_aspect:
# Then crop the left and right edges:
new_width = int(required_aspect * height)
offset = int(abs(width - new_width) / 2)
resize = (offset + border, border, width - offset - border, height - border)
else:
# crop the top and bottom:
new_height = int(width / required_aspect)
offset = int(abs(height - new_height))
resize = (border, border, width - border, height - offset - border)
with tempfile.NamedTemporaryFile(suffix=".png") as fp:
cropped_image = image.crop(resize)
image.save(fp)
fp.seek(0)
data = fp.read()
remote_filename = tv.art().upload(data, file_type='PNG', matte='none')
tv.art().select_image(remote_filename, show=False)
else:
logging.warning('Your TV does not support art mode.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment