Python Library Media
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

convert.py 1.3KB

1234567891011121314151617181920212223242526272829303132333435
  1. import io
  2. from media import common, logger
  3. from PIL import Image
  4. import subprocess
  5. import platform
  6. def get_pil_image(media_instance):
  7. try:
  8. media_instance = media_instance._im
  9. except AttributeError:
  10. pass
  11. #
  12. if type(media_instance) == str:
  13. ft = common.get_filetype(media_instance)
  14. if ft == common.FILETYPE_IMAGE:
  15. return Image.open(media_instance)
  16. elif ft == common.FILETYPE_VIDEO:
  17. if platform.system() == 'Linux':
  18. cmd = 'ffmpeg -ss 0.5 -i "' + media_instance + '" -vframes 1 -f image2pipe pipe:1 2> /dev/null'
  19. else:
  20. cmd = 'ffmpeg -ss 0.5 -i "' + media_instance + '" -vframes 1 -f image2pipe pipe:1 2> NULL'
  21. try:
  22. data = subprocess.check_output(cmd, shell=True)
  23. except subprocess.CalledProcessError:
  24. logger.warning('ffmpeg seems to be not installed')
  25. return None
  26. ffmpeg_handle = io.BytesIO(data)
  27. im = Image.open(ffmpeg_handle)
  28. return im.copy()
  29. logger.warning('Filetype is not supported (%s)', media_instance)
  30. elif type(media_instance) == Image.Image:
  31. return media_instance.copy()
  32. else:
  33. logger.warning('Instance type is not supported: %s' % type(media_instance))