M
M
Maxim Barabanov2015-06-18 04:56:54
Django
Maxim Barabanov, 2015-06-18 04:56:54

Why might paste not work in PIL?

I make watermark on django 1.8, python 3.4. On the Internet, I found a way through imagekit, with this function:

def process(self, image):
        """Adds a watermark to an image."""
        BASE_DIR = os.path.dirname(os.path.dirname(__file__))
        mark = Image.open(os.path.join(BASE_DIR, "static/images/logo.png"))
        #mark = self.reduce_opacity(mark, 0.5)
        if mark.mode != 'RGBA':
            mark = mark.convert('RGBA')
        if image.mode != 'RGBA':
            image = image.convert('RGBA')
        # create a transparent layer the size of the image and draw the
        # watermark in that layer.
        # scale, but preserve the aspect ratio
        ratio = min(
            float(image.size[0]-10) / mark.size[0],
            float(image.size[1]-10) / mark.size[1]
        )
        w = int(mark.size[0] * ratio)
        h = int(mark.size[1] * ratio)
        mark = mark.resize((w, h))
        l = (image.size[0] - w) / 2
        t = (image.size[1] - h) / 2
        layer = Image.new('RGBA', image.size, (0, 0, 0, 0))
        layer.paste(mark, (l, t, w, h))
        # composite the watermark with the layer
        return Image.composite(layer, image, layer)

for some reason this code crashes on the penultimate line ( layer.paste(mark, (l, t, w, h))). Moreover, if you comment it out and simply return it mark, the function will work. Trace inside I can only go to 1 level, crashes on a similar one self.im.paste(im, box), only im there is no longer there Image, but ImagingCore.
I thought it was a problem with the libraries - I uploaded it to the Linux server, checked for the presence of libpng and libjpeg, reinstalled PIllow just in case - the problem is the same.
What could be the reason?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Barabanov, 2015-06-18
@reb00ter

in general, the whole problem is in data types

l = int((image.size[0] - w) / 2)
t = int((image.size[1] - h) / 2)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question