| 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 | 9 |
| 10 try: | 10 try: |
| 11 from PIL import Image | 11 from PIL import Image |
| 12 from PIL import ImageOps | 12 from PIL import ImageOps |
| 13 except ImportError: | 13 except ImportError: |
| 14 import Image | 14 import Image |
| 15 import ImageOps | 15 import ImageOps |
| 16 | 16 |
| 17 from imageCompression import image_to_file | 17 from imageCompression import image_to_file |
| 18 | 18 |
| 19 def load_image(path): | 19 def load_image(path): |
| 20 image = Image.open(path) | 20 image = Image.open(path) |
| 21 # Make sure the image is loaded, some versions of PIL load images lazily. | 21 # Make sure the image is loaded, some versions of PIL load images lazily. |
| 22 image.load() | 22 image.load() |
| 23 return image | 23 return image |
| 24 | 24 |
| 25 def ensure_same_mode(im1, im2): | |
| 26 # if both images already have the same mode (and palette, in | |
| 27 # case of mode P), don't convert anything. Images with mode P, | |
| 28 # and a different palette, are the only case where images | |
| 29 # using the same mode, will be incompatible with each other. | |
| 30 if im1.mode == im2.mode and (im1.mode != 'P' or im1.getpalette() == im2.getpal
ette()): | |
| 31 return (im1, im2) | |
| 32 | |
| 33 # if any given image has a mode that supports colors convert both | |
| 34 # images to RGB or RGBA, otherwise convert both images to L or LA. | |
| 35 # If any given image has an alpha channel (or mode P which | |
| 36 # can store transparent pixels too) convert both images | |
| 37 # to RGBA or LA, otherwise convert both images to RGB or L. | |
| 38 mode = max( | |
| 39 Image.getmodebase(im1.mode), | |
| 40 Image.getmodebase(im2.mode), | |
| 41 | |
| 42 key=('L', 'RGB').index | |
| 43 ) | |
| 44 | |
| 45 if any(im.mode in ('RGBA', 'LA', 'P') for im in (im1, im2)): | |
| 46 mode += 'A' | |
| 47 | |
| 48 return ( | |
| 49 im1 if im1.mode == mode else im1.convert(mode), | |
| 50 im2 if im2.mode == mode else im2.convert(mode), | |
| 51 ) | |
| 52 | |
| 53 def filter_contrastToAlpha(image, baseDir): | 25 def filter_contrastToAlpha(image, baseDir): |
| 54 # In order to generate an alpha channel for images using a palette, we must | 26 # In order to generate an alpha channel for images using a palette, we must |
| 55 # convert the image to RGBA. It's important to use RGBA, not LA (grayscale+alp
ha), | 27 # convert the image to RGBA. It's important to use RGBA, not LA (grayscale+alp
ha), |
| 56 # since PIL can't reliably convert P to LA. Also initially, we created an | 28 # since PIL can't reliably convert P to LA. Also initially, we created an |
| 57 # alpha channel by replacing opaque pixels with a high mark and transparent | 29 # alpha channel by replacing opaque pixels with a high mark and transparent |
| 58 # pixels with a low mark. However, it turned out that you can't rely on the | 30 # pixels with a low mark. However, it turned out that you can't rely on the |
| 59 # value of Image.info['transparency'] since in case the transparency is | 31 # value of Image.info['transparency'] since in case the transparency is |
| 60 # specified in bytes (pngout loves to do that), we can't associate that value | 32 # specified in bytes (pngout loves to do that), we can't associate that value |
| 61 # with transparent pixels. Moreover, some versions of PIL raise a warning | 33 # with transparent pixels. Moreover, some versions of PIL raise a warning |
| 62 # when such images are pasted into another image. | 34 # when such images are pasted into another image. |
| 63 if image.mode == 'P' and 'transparency' in image.info: | 35 if image.mode == 'P' and 'transparency' in image.info: |
| 64 image = image.convert('RGBA') | 36 image = image.convert('RGBA') |
| 65 | 37 |
| 66 # Image.paste() ignores the alpha channel of the pasted image, but expects | 38 # Image.paste() ignores the alpha channel of the pasted image, but expects |
| 67 # the mask to be passed as separate argument. So we have to extract the alpha | 39 # the mask to be passed as separate argument. So we have to extract the alpha |
| 68 # channel (if any) from the image we are going to paste. | 40 # channel (if any) from the image we are going to paste. |
| 69 if image.mode in ('RGBA', 'LA'): | 41 if image.mode in ('RGBA', 'LA'): |
| 70 mask = image.split()[image.getbands().index('A')] | 42 mask = image.split()[image.getbands().index('A')] |
| 71 else: | 43 else: |
| 72 mask = None | 44 mask = None |
| 73 | 45 |
| 74 alpha = Image.new('L', image.size, 255) | 46 alpha = Image.new('L', image.size, 255) |
| 75 alpha.paste(image, mask=mask) | 47 alpha.paste(image, mask=mask) |
| 76 alpha = ImageOps.invert(alpha) | 48 alpha = ImageOps.invert(alpha) |
| 77 alpha = ImageOps.autocontrast(alpha) | 49 alpha = ImageOps.autocontrast(alpha) |
| 78 | 50 |
| 79 return Image.merge('LA', [Image.new('L', image.size), alpha]) | 51 return Image.merge('LA', [Image.new('L', image.size), alpha]) |
| 80 | 52 |
| 81 def filter_blend(image, baseDir, filename, opacity): | |
| 82 image, overlay = ensure_same_mode( | |
| 83 image, | |
| 84 load_image(os.path.join( | |
| 85 baseDir, | |
| 86 *filename.split('/') | |
| 87 )) | |
| 88 ) | |
| 89 | |
| 90 return Image.blend(image, overlay, float(opacity)) | |
| 91 | |
| 92 def convertImages(params, files): | 53 def convertImages(params, files): |
| 93 metadata = params['metadata'] | 54 metadata = params['metadata'] |
| 94 | 55 |
| 95 for filename, chain in metadata.items('convert_img'): | 56 for filename, chain in metadata.items('convert_img'): |
| 96 baseDir = os.path.dirname(metadata.option_source('convert_img', filename)) | 57 baseDir = os.path.dirname(metadata.option_source('convert_img', filename)) |
| 97 steps = re.split(r'\s*->\s*', chain) | 58 steps = re.split(r'\s*->\s*', chain) |
| 98 image = load_image(os.path.join(baseDir, *steps.pop(0).split('/'))) | 59 image = load_image(os.path.join(baseDir, *steps.pop(0).split('/'))) |
| 99 | 60 |
| 100 for step in steps: | 61 for step in steps: |
| 101 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups() | 62 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups() |
| 102 args = re.split(r'\s*,\s*', args) if args else () | 63 args = re.split(r'\s*,\s*', args) if args else () |
| 103 image = globals()['filter_' + filter](image, baseDir, *args) | 64 image = globals()['filter_' + filter](image, baseDir, *args) |
| 104 | 65 |
| 105 file = image_to_file(image, filename) | 66 file = image_to_file(image, filename) |
| 106 try: | 67 try: |
| 107 files[filename] = file.read() | 68 files[filename] = file.read() |
| 108 finally: | 69 finally: |
| 109 file.close() | 70 file.close() |
| OLD | NEW |