Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: imageConversion.py

Issue 6406754824880128: Issue 2038 - Convert palette images with transparency to RGBA before pasting (Closed)
Left Patch Set: Fixed typo in comment Created Feb. 24, 2015, 4:10 p.m.
Right Patch Set: Addressed comments Created Feb. 24, 2015, 5:19 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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:
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 im2 if im2.mode == mode else im2.convert(mode), 50 im2 if im2.mode == mode else im2.convert(mode),
51 ) 51 )
52 52
53 def filter_contrastToAlpha(image, baseDir): 53 def filter_contrastToAlpha(image, baseDir):
54 # In order to generate an alpha channel for images using a palette, we must 54 # 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), 55 # 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 56 # 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 57 # 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 58 # 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 59 # value of Image.info['transparency'] since in case the transparency is
60 # specified in bytes (pngout loves to that shit), we can't associate that 60 # specified in bytes (pngout loves to do that), we can't associate that value
61 # value with transparent pixels. Moreover, some versions of PIL raise a 61 # with transparent pixels. Moreover, some versions of PIL raise a warning
62 # warning when such images are pasted into another image. 62 # when such images are pasted into another image.
63 if image.mode == 'P' and 'transparency' in image.info: 63 if image.mode == 'P' and 'transparency' in image.info:
64 image = image.convert("RGBA") 64 image = image.convert('RGBA')
65 65
66 # Image.paste() ignores the alpha channel of the pasted image, but expects 66 # Image.paste() ignores the alpha channel of the pasted image, but expects
67 # the mask to be passed as sperate argument. So we have to extract the alpha 67 # 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. 68 # channel (if any) from the image we are going to paste.
69 if image.mode in ('RGBA', 'LA'): 69 if image.mode in ('RGBA', 'LA'):
70 mask = image.split()[image.getbands().index('A')] 70 mask = image.split()[image.getbands().index('A')]
71 else: 71 else:
72 mask = None 72 mask = None
73 73
74 alpha = Image.new('L', image.size, 255) 74 alpha = Image.new('L', image.size, 255)
75 alpha.paste(image, mask=mask) 75 alpha.paste(image, mask=mask)
76 alpha = ImageOps.invert(alpha) 76 alpha = ImageOps.invert(alpha)
77 alpha = ImageOps.autocontrast(alpha) 77 alpha = ImageOps.autocontrast(alpha)
(...skipping 22 matching lines...) Expand all
100 for step in steps: 100 for step in steps:
101 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups() 101 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups()
102 args = re.split(r'\s*,\s*', args) if args else () 102 args = re.split(r'\s*,\s*', args) if args else ()
103 image = globals()['filter_' + filter](image, baseDir, *args) 103 image = globals()['filter_' + filter](image, baseDir, *args)
104 104
105 file = image_to_file(image, filename) 105 file = image_to_file(image, filename)
106 try: 106 try:
107 files[filename] = file.read() 107 files[filename] = file.read()
108 finally: 108 finally:
109 file.close() 109 file.close()
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld