cope with PNG 8 pallete with semi transparency (bug 621033)

This commit is contained in:
Andy McKay 2010-12-22 15:35:03 -08:00
Родитель 413165095b
Коммит 10340af6d1
4 изменённых файлов: 57 добавлений и 3 удалений

Двоичные данные
apps/amo/tests/images/transparent-expected.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 1.4 KiB

Двоичные данные
apps/amo/tests/images/transparent.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 726 B

Просмотреть файл

@ -1,4 +1,8 @@
# -*- coding: utf-8 -*-
import os
import tempfile
from django.conf import settings
from django.core.validators import ValidationError
from nose.tools import eq_, assert_raises
@ -44,3 +48,16 @@ def test_slugify():
def test_resize_image():
# src and dst shouldn't be the same.
assert_raises(Exception, resize_image, 't', 't', 'z')
def test_resize_transparency():
src = os.path.join(settings.ROOT, 'apps', 'amo', 'tests',
'images', 'transparent.png')
dest = tempfile.mkstemp()[1]
expected = src.replace('.png', '-expected.png')
try:
resize_image(src, dest, (32, 32), remove_src=False)
assert open(dest, 'rb').read() == open(expected, 'rb').read()
finally:
if os.path.exists(dest):
os.remove(dest)

Просмотреть файл

@ -19,7 +19,7 @@ from django.utils.encoding import smart_str, smart_unicode
from easy_thumbnails import processors
import pytz
from PIL import Image
from PIL import Image, ImageFile, PngImagePlugin
from . import logger_log as log
from translations.models import Translation
@ -241,18 +241,55 @@ def clear_messages(request):
"""
Clear any messages out of the messages framework for the authenticated
user.
Docs:
http://docs.djangoproject.com/en/dev/ref/contrib/messages/#expiration-of-messages
Docs: http://bit.ly/dEhegk
"""
for message in messages.get_messages(request):
pass
# From: http://bit.ly/eTqloE
# Without this, you'll notice a slight grey line on the edges of
# the adblock plus icon.
def patched_chunk_tRNS(self, pos, len):
i16 = PngImagePlugin.i16
s = ImageFile._safe_read(self.fp, len)
if self.im_mode == "P":
self.im_info["transparency"] = map(ord, s)
elif self.im_mode == "L":
self.im_info["transparency"] = i16(s)
elif self.im_mode == "RGB":
self.im_info["transparency"] = i16(s), i16(s[2:]), i16(s[4:])
return s
PngImagePlugin.PngStream.chunk_tRNS = patched_chunk_tRNS
def patched_load(self):
if self.im and self.palette and self.palette.dirty:
apply(self.im.putpalette, self.palette.getdata())
self.palette.dirty = 0
self.palette.rawmode = None
try:
trans = self.info["transparency"]
except KeyError:
self.palette.mode = "RGB"
else:
try:
for i, a in enumerate(trans):
self.im.putpalettealpha(i, a)
except TypeError:
self.im.putpalettealpha(trans, 0)
self.palette.mode = "RGBA"
if self.im:
return self.im.pixel_access(self.readonly)
Image.Image.load = patched_load
def resize_image(src, dst, size, remove_src=True):
"""Resizes and image from src, to dst."""
if src == dst:
raise Exception("src and dst can't be the same: %s" % src)
im = Image.open(src)
im = im.convert('RGBA')
im = processors.scale_and_crop(im, size)
im.save(dst, 'png')