2023-10-18|閱讀時間 ‧ 約 0 分鐘

code lnterpreter 圖片生成視頻


粒子消散效果指令:

 

我想將照片塊狀消散效果應用到這張圖片上。你能幫我嗎?為我提供下載生成的視頻的連結。使用下面的代碼

1import imageio

import numpy as np

import random

# Load the image

image_path = "[INSERT IMAGE PATH HERE]"

image = imageio.imread(image_path)

# Define the block size

block_size = 4

# Get the dimensions of the image

height, width, _ = image.shape

# Make sure the image dimensions are divisible by block size

height -= height % block_size

width -= width % block_size

# Crop the image to the new dimensions

image = image[:height, :width]

# Create a copy of the blocks dictionary to use for the animation

anim_blocks = blocks.copy()

# Create a dictionary to store the velocity of each block, with further reduced initial speed range

velocities = {key: [random.uniform(-2.5, 2.5), random.uniform(-2.5, 2.5)] for key in anim_blocks.keys()}

# Create a dictionary to store the start frame for each block, with upper blocks starting earlier and a random offset

start_frames = {key: int((key[0] / num_blocks_y) * num_frames) + random.randint(-num_frames//10, num_frames//10) for key in anim_blocks.keys()}

# Create a writer object to write the frames to a MP4 file

writer = imageio.get_writer('/mnt/data/animation_gradual_random.mp4', fps=24)

# Create a progress bar for the loop

pbar = tqdm(total=num_frames)

# Loop over the frames

for frame in range(num_frames):

# Create a blank image for this frame

frame_img = np.full(img_array.shape, 0, dtype=np.uint8)

# Loop over the blocks

for key in list(anim_blocks.keys()):

# If the block has not started to disintegrate yet, skip it

if frame < start_frames[key]:

# Draw the block on the frame image

frame_img[key[0]*block_size:(key[0]+1)*block_size, key[1]*block_size:(key[1]+1)*block_size] = anim_blocks[key]

continue

# Update the position of the block

key_new = (int(key[0] + velocities[key][1]), int(key[1] + velocities[key][0]))

# If the block has moved off the image, remove it from the dictionary

if key_new[0] < 0 or key_new[0] >= num_blocks_y or key_new[1] < 0 or key_new[1] >= num_blocks_x:

del anim_blocks[key]

continue

# Update the velocity of the block

velocities[key][0] -= air_resistance * velocities[key][0]

velocities[key][1] += gravity - air_resistance * velocities[key][1]

# Draw the block on the frame image

frame_img[key_new[0]*block_size:(key_new[0]+1)*block_size, key_new[1]*block_size:(key_new[1]+1)*block_size] = anim_blocks[key]

# Move the block in the dictionaries

anim_blocks[key_new] = anim_blocks.pop(key)

velocities[key_new] = velocities.pop(key)

# Add the frame to the MP4 file

writer.append_data(frame_img)

# Update the progress bar

pbar.update(1)

# Close the writer

writer.close()

# Close the progress bar

pbar.close()

'/mnt/data/animation_gradual_random.mp4'

如提示需要填寫變數,可以讓chatgpt自己來執行。

保證粒子塊的大小在“2*2的塊”以下即可。

如果尺寸太大,效果會有折扣。

分享至
成為作者繼續創作的動力吧!
© 2024 vocus All rights reserved.