M
M
Maxim2016-12-16 14:39:12
Python
Maxim, 2016-12-16 14:39:12

How to insert an image into an image using Pillow?

Hello everyone, there is an extensive photo collage task, at the moment it is not possible to insert one photo into another

from PIL import Image

im = Image.open('lenna.png')
au = Image.open('audi.png')

Next job in the debugger
(Pdb) !im.mode
'RGB'
(Pdb) !au.mode
'RGBA'
(Pdb) !k = au.convert(mode='RGB')
(Pdb) !k.mode
'RGB'
(Pdb) !im.paste(k, (0,0,100,100))
*** ValueError: images do not match
(Pdb)

The error should not occur if the images are the same, as you can see, I converted the inserted photo, what should I do with this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2016-12-16
@maximkv25

Asked by himself, answered by himself
I didn't quite understand the documentation at first
. Image sizes must match, otherwise the "BOX" argument must be specified explicitly

N
NaName, 2016-12-16
@NaName

from PIL import Image
import glob

foreground_name = 'BFB_Logo.png'
foreground = Image.open(foreground_name)
foreground = foreground.convert('RGBA')

for file_name in glob.glob('*.jpg'):
    print (file_name)
    background_name = file_name
    background = Image.open(background_name)

    x = int((background.size[0] / 2) - (foreground.size[0] / 2))
    y = int((background.size[1] / 2) - (foreground.size[1] / 2))

    background = background.convert('RGBA')
    background.paste(foreground, (x, y), mask = foreground)
    background.save('__' + background_name.split('.')[0] + '.jpg','JPEG')

print('Gotovo bla-a')

I used this to insert into the center.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question