def fap(update, context): filename = datetime.now().strftime("%d%m%y-%H%M%S%f") try: extension = get_image(update, context, path, filename) except: update.message.reply_text("I can't get the image! :(") return update.message.chat.send_action(ChatAction.UPLOAD_PHOTO) with Image(filename=path + filename + extension) as decal: decal.resize(320, 172) w, h = decal.size decal.virtual_pixel = 'transparent' source_points = ((0, 0), (w, 0), (w, h), (0, h)) with Image(filename=launchpad_gif) as template_gif: new = Image() for i in range(len(template_gif.sequence)): with template_gif.sequence[i] as frame: img = Image(image=frame) img.delay = 6 destination_points = (coords_by_frame[i]) order = chain.from_iterable( zip(source_points, destination_points)) arguments = list(chain.from_iterable(order)) decal_current = Image(image=decal) decal_current.matte_color = "rgba(255, 255, 255, 0)" decal_current.distort('perspective', arguments) img.composite(decal_current, left=0, top=0) new.sequence.append(img) decal_current.close() img.close() new.save(filename=path + "result.mp4") result_filename = mp4_fix(path, "result") send_image(update, path, result_filename, ".mp4") new.close() os.remove(path + result_filename + ".mp4") os.remove(path + filename + extension)