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

Unified Diff: imageConversion.py

Issue 4826241328742400: Made import of PIL compatible to environments where you have to import Image directly (Closed)
Patch Set: Created Nov. 15, 2013, 9:40 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: imageConversion.py
===================================================================
--- a/imageConversion.py
+++ b/imageConversion.py
@@ -19,7 +19,12 @@
import re
from StringIO import StringIO
-import PIL.Image
+try:
+ from PIL import Image
+ from PIL import ImageOps
+except ImportError:
+ import Image
+ import ImageOps
def get_alpha(image):
if image.mode in ('RGBA', 'LA'):
@@ -48,8 +53,8 @@
# can store transparent pixels too) convert both images
# to RGBA or LA, otherwise convert both images to RGB or L.
mode = max(
- PIL.Image.getmodebase(im1.mode),
- PIL.Image.getmodebase(im2.mode),
+ Image.getmodebase(im1.mode),
+ Image.getmodebase(im2.mode),
key=('L', 'RGB').index
)
@@ -72,27 +77,25 @@
return image
def filter_contrastToAlpha(image, baseDir):
- import PIL.ImageOps
+ alpha = Image.new('L', image.size, 255)
+ alpha.paste(image, mask=get_alpha(image))
+ alpha = ImageOps.invert(alpha)
+ alpha = ImageOps.autocontrast(alpha)
- alpha = PIL.Image.new('L', image.size, 255)
- alpha.paste(image, mask=get_alpha(image))
- alpha = PIL.ImageOps.invert(alpha)
- alpha = PIL.ImageOps.autocontrast(alpha)
-
- return PIL.Image.merge('LA', [PIL.Image.new('L', image.size), alpha])
+ return Image.merge('LA', [Image.new('L', image.size), alpha])
def filter_blend(image, baseDir, *args):
if len(args) == 2:
filename, opacity = args
- overlay = PIL.Image.open(os.path.join(
+ overlay = Image.open(os.path.join(
baseDir,
*filename.split('/')
))
else:
red, green, blue, opacity = args
- overlay = PIL.Image.new('RGB', image.size, (
+ overlay = Image.new('RGB', image.size, (
int(red),
int(green),
int(blue),
@@ -105,7 +108,7 @@
overlay.putalpha(alpha)
image, overlay = ensure_same_mode(image, overlay)
- return PIL.Image.blend(image, overlay, float(opacity))
+ return Image.blend(image, overlay, float(opacity))
def convertImages(params, files):
metadata = params['metadata']
@@ -113,7 +116,7 @@
for filename, chain in metadata.items('convert_img'):
baseDir = os.path.dirname(metadata.option_source('convert_img', filename))
steps = re.split(r'\s*->\s*', chain)
- image = PIL.Image.open(os.path.join(baseDir, *steps.pop(0).split('/')))
+ image = Image.open(os.path.join(baseDir, *steps.pop(0).split('/')))
for step in steps:
filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups()
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld