36 行
1.3 KiB
Python
36 行
1.3 KiB
Python
import io
|
|
from media import common, logger
|
|
from PIL import Image
|
|
import subprocess
|
|
import platform
|
|
|
|
|
|
def get_pil_image(media_instance):
|
|
try:
|
|
media_instance = media_instance._im
|
|
except AttributeError:
|
|
pass
|
|
#
|
|
if type(media_instance) == str:
|
|
ft = common.get_filetype(media_instance)
|
|
if ft == common.FILETYPE_IMAGE:
|
|
return Image.open(media_instance)
|
|
elif ft == common.FILETYPE_VIDEO:
|
|
if platform.system() == 'Linux':
|
|
cmd = 'ffmpeg -ss 0.5 -i "' + media_instance + '" -vframes 1 -f image2pipe pipe:1 2> /dev/null'
|
|
else:
|
|
cmd = 'ffmpeg -ss 0.5 -i "' + media_instance + '" -vframes 1 -f image2pipe pipe:1 2> NULL'
|
|
try:
|
|
data = subprocess.check_output(cmd, shell=True)
|
|
except subprocess.CalledProcessError:
|
|
logger.warning('ffmpeg seems to be not installed')
|
|
return None
|
|
ffmpeg_handle = io.BytesIO(data)
|
|
im = Image.open(ffmpeg_handle)
|
|
return im.copy()
|
|
logger.warning('Filetype is not supported (%s)', media_instance)
|
|
elif type(media_instance) == Image.Image:
|
|
return media_instance.copy()
|
|
else:
|
|
logger.warning('Instance type is not supported: %s' % type(media_instance))
|