comparison planemo/lib/python3.7/site-packages/galaxy/util/image_util.py @ 1:56ad4e20f292 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:32:28 -0400
parents
children
comparison
equal deleted inserted replaced
0:d30785e31577 1:56ad4e20f292
1 """Provides utilities for working with image files."""
2 from __future__ import absolute_import
3
4 import imghdr
5 import logging
6
7 try:
8 import Image as PIL
9 except ImportError:
10 try:
11 from PIL import Image as PIL
12 except ImportError:
13 PIL = None
14
15 log = logging.getLogger(__name__)
16
17
18 def image_type(filename):
19 fmt = None
20 if PIL is not None:
21 try:
22 im = PIL.open(filename)
23 fmt = im.format
24 im.close()
25 except Exception:
26 # We continue to try with imghdr, so this is a rare case of an
27 # exception we expect to happen frequently, so we're not logging
28 pass
29 if not fmt:
30 fmt = imghdr.what(filename)
31 if fmt:
32 return fmt.upper()
33 else:
34 return False
35
36
37 def check_image_type(filename, types):
38 fmt = image_type(filename)
39 if fmt in types:
40 return True
41 return False