fix ox.image in python3

This commit is contained in:
j 2014-10-01 10:48:06 +02:00
parent c2de06d9d8
commit 6dfa80b646
3 changed files with 20 additions and 13 deletions

View file

@ -1,11 +1,10 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
# GPL 2008 # GPL 2008
import sys
import re import re
import string import string
from six.moves.html_entities import name2codepoint from six.moves.html_entities import name2codepoint
from six import unichr from six import unichr, PY2
# Configuration for add_links() function # Configuration for add_links() function
@ -25,7 +24,7 @@ link_target_attribute_re = re.compile(r'(<a [^>]*?)target=[^\s>]+')
html_gunk_re = re.compile(r'(?:<br clear="all">|<i><\/i>|<b><\/b>|<em><\/em>|<strong><\/strong>|<\/?smallcaps>|<\/?uppercase>)', re.IGNORECASE) html_gunk_re = re.compile(r'(?:<br clear="all">|<i><\/i>|<b><\/b>|<em><\/em>|<strong><\/strong>|<\/?smallcaps>|<\/?uppercase>)', re.IGNORECASE)
hard_coded_bullets_re = re.compile(r'((?:<p>(?:%s).*?[a-zA-Z].*?</p>\s*)+)' % '|'.join([re.escape(x) for x in DOTS]), re.DOTALL) hard_coded_bullets_re = re.compile(r'((?:<p>(?:%s).*?[a-zA-Z].*?</p>\s*)+)' % '|'.join([re.escape(x) for x in DOTS]), re.DOTALL)
trailing_empty_content_re = re.compile(r'(?:<p>(?:&nbsp;|\s|<br \/>)*?</p>\s*)+\Z') trailing_empty_content_re = re.compile(r'(?:<p>(?:&nbsp;|\s|<br \/>)*?</p>\s*)+\Z')
if sys.version[0] == 2: if PY2:
del x # Temporary variable del x # Temporary variable
def escape(html): def escape(html):

View file

@ -3,15 +3,23 @@
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
from __future__ import division from __future__ import division
from hashlib import sha1
try:
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
except:
import Image import Image
import ImageDraw import ImageDraw
import ImageFont import ImageFont
ZONE_INDEX = [] ZONE_INDEX = []
for pixel_index in range(64): for pixel_index in range(64):
x, y = pixel_index % 8, int(pixel_index / 8) x, y = pixel_index % 8, int(pixel_index / 8)
ZONE_INDEX.append(int(x / 2) + int(y / 4) * 4) ZONE_INDEX.append(int(x / 2) + int(y / 4) * 4)
del x
del y
def drawText(image, position, text, font_file, font_size, color): def drawText(image, position, text, font_file, font_size, color):
draw = ImageDraw.Draw(image) draw = ImageDraw.Draw(image)
@ -20,7 +28,7 @@ def drawText(image, position, text, font_file, font_size, color):
return draw.textsize(text, font=font) return draw.textsize(text, font=font)
def getHSL(rgb): def getHSL(rgb):
rgb = map(lambda x: x / 255, rgb) rgb = [x / 255 for x in rgb]
maximum = max(rgb) maximum = max(rgb)
minimum = min(rgb) minimum = min(rgb)
hsl = [0.0, 0.0, 0.0] hsl = [0.0, 0.0, 0.0]
@ -60,7 +68,7 @@ def getImageHash(image_file, mode):
zone_values[ZONE_INDEX[pixel_index]].append(pixel_value) zone_values[ZONE_INDEX[pixel_index]].append(pixel_value)
for zone_index, pixel_values in enumerate(zone_values): for zone_index, pixel_values in enumerate(zone_values):
# get the mean for each color channel # get the mean for each color channel
mean = map(lambda x: int(round(sum(x) / 8)), zip(*pixel_values)) mean = [int(round(sum(x) / 8)) for x in zip(*pixel_values)]
# store the mean color of each zone as an 8-bit value: # store the mean color of each zone as an 8-bit value:
# RRRGGGBB # RRRGGGBB
color_index = sum(( color_index = sum((
@ -128,7 +136,7 @@ def getRGB(hsl):
rgb[i] = v1 + ((v2 - v1) * 6 * (2/3 - v3)) rgb[i] = v1 + ((v2 - v1) * 6 * (2/3 - v3))
else: else:
rgb[i] = v1 rgb[i] = v1
return tuple(map(lambda x: int(x * 255), rgb)) return tuple([int(x * 255) for x in rgb])
def getTextSize(image, text, font_file, font_size): def getTextSize(image, text, font_file, font_size):
draw = ImageDraw.Draw(image) draw = ImageDraw.Draw(image)

View file

@ -1,16 +1,16 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
import sys
from ox.utils import json from six import PY2
from .utils import json
def minify(source, comment=''): def minify(source, comment=''):
# see https://github.com/douglascrockford/JSMin/blob/master/README # see https://github.com/douglascrockford/JSMin/blob/master/README
def get_next_non_whitespace_token(): def get_next_non_whitespace_token():
pass pass
# python2 performance with unicode string is terrible # python2 performance with unicode string is terrible
if sys.version[0] == '2': if PY2:
if isinstance(source, unicode): if isinstance(source, unicode):
source = source.encode('utf-8') source = source.encode('utf-8')
if isinstance(comment, unicode): if isinstance(comment, unicode):