Answer the question
In order to leave comments, you need to log in
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')
(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)
Answer the question
In order to leave comments, you need to log in
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
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')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question