OLD | NEW |
1 # coding: utf-8 | 1 # coding: utf-8 |
2 | 2 |
3 # This Source Code Form is subject to the terms of the Mozilla Public | 3 # This Source Code Form is subject to the terms of the Mozilla Public |
4 # License, v. 2.0. If a copy of the MPL was not distributed with this | 4 # License, v. 2.0. If a copy of the MPL was not distributed with this |
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. | 5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
6 | 6 |
7 import os | 7 import os |
8 import re | 8 import re |
9 from StringIO import StringIO | 9 from StringIO import StringIO |
10 | 10 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 ) | 56 ) |
57 | 57 |
58 if any(im.mode in ('RGBA', 'LA', 'P') for im in (im1, im2)): | 58 if any(im.mode in ('RGBA', 'LA', 'P') for im in (im1, im2)): |
59 mode += 'A' | 59 mode += 'A' |
60 | 60 |
61 return ( | 61 return ( |
62 im1 if im1.mode == mode else im1.convert(mode), | 62 im1 if im1.mode == mode else im1.convert(mode), |
63 im2 if im2.mode == mode else im2.convert(mode), | 63 im2 if im2.mode == mode else im2.convert(mode), |
64 ) | 64 ) |
65 | 65 |
66 def filter_grayscale(image, baseDir): | |
67 alpha = get_alpha(image) | |
68 image = image.convert('L') | |
69 | |
70 if alpha: | |
71 image.putalpha(alpha) | |
72 | |
73 return image | |
74 | |
75 def filter_contrastToAlpha(image, baseDir): | 66 def filter_contrastToAlpha(image, baseDir): |
76 alpha = Image.new('L', image.size, 255) | 67 alpha = Image.new('L', image.size, 255) |
77 alpha.paste(image, mask=get_alpha(image)) | 68 alpha.paste(image, mask=get_alpha(image)) |
78 alpha = ImageOps.invert(alpha) | 69 alpha = ImageOps.invert(alpha) |
79 alpha = ImageOps.autocontrast(alpha) | 70 alpha = ImageOps.autocontrast(alpha) |
80 | 71 |
81 return Image.merge('LA', [Image.new('L', image.size), alpha]) | 72 return Image.merge('LA', [Image.new('L', image.size), alpha]) |
82 | 73 |
83 def filter_blend(image, baseDir, *args): | 74 def filter_blend(image, baseDir, filename, opacity): |
84 if len(args) == 2: | 75 image, overlay = ensure_same_mode( |
85 filename, opacity = args | 76 image, |
86 | 77 load_image(os.path.join( |
87 overlay = load_image(os.path.join( | |
88 baseDir, | 78 baseDir, |
89 *filename.split('/') | 79 *filename.split('/') |
90 )) | 80 )) |
91 else: | 81 ) |
92 red, green, blue, opacity = args | |
93 | 82 |
94 overlay = Image.new('RGB', image.size, ( | |
95 int(red), | |
96 int(green), | |
97 int(blue), | |
98 )) | |
99 | |
100 # if the background image has an alpha channel copy it to | |
101 # the overlay, so that transparent areas stay transparent. | |
102 alpha = get_alpha(image) | |
103 if alpha: | |
104 overlay.putalpha(alpha) | |
105 | |
106 image, overlay = ensure_same_mode(image, overlay) | |
107 return Image.blend(image, overlay, float(opacity)) | 83 return Image.blend(image, overlay, float(opacity)) |
108 | 84 |
109 def convertImages(params, files): | 85 def convertImages(params, files): |
110 metadata = params['metadata'] | 86 metadata = params['metadata'] |
111 | 87 |
112 for filename, chain in metadata.items('convert_img'): | 88 for filename, chain in metadata.items('convert_img'): |
113 baseDir = os.path.dirname(metadata.option_source('convert_img', filename)) | 89 baseDir = os.path.dirname(metadata.option_source('convert_img', filename)) |
114 steps = re.split(r'\s*->\s*', chain) | 90 steps = re.split(r'\s*->\s*', chain) |
115 image = load_image(os.path.join(baseDir, *steps.pop(0).split('/'))) | 91 image = load_image(os.path.join(baseDir, *steps.pop(0).split('/'))) |
116 | 92 |
117 for step in steps: | 93 for step in steps: |
118 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups() | 94 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups() |
119 args = re.split(r'\s*,\s*', args) if args else () | 95 args = re.split(r'\s*,\s*', args) if args else () |
120 image = globals()['filter_' + filter](image, baseDir, *args) | 96 image = globals()['filter_' + filter](image, baseDir, *args) |
121 | 97 |
122 f = StringIO() | 98 f = StringIO() |
123 f.name = filename | 99 f.name = filename |
124 image.save(f) | 100 image.save(f) |
125 files[filename] = f.getvalue() | 101 files[filename] = f.getvalue() |
OLD | NEW |