创建更平滑不稳定波形背后的数学

2024-05-15 05:55:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python编写了一个程序,可以得到每一帧的平均颜色,从而创建一张这样的海报:

这是我使用PIL创建自己图像的函数:

def create_poster_wave():
    # creates png with pixel height equal to the analysed frames, width is 2/3 of height (standard poster format)
    # draws a line each loop whose width depends on relative brightness of frame to create wave effect
    height = file_len()
    width = int(height / 1.5)
    mid = int(width / 2)
    min_line_width = int(width / 8)
    img = Image.new('RGB', (width, height), color='black')
    with open(MOVIE_TITLE) as file:
        color_list = [tuple(map(int, i.split(','))) for i in file]
    counter = 0
    draw = ImageDraw.Draw(img)
    brightness = get_color_brightness(color_list[0])
    last_len = int(min_line_width ** (1 + 0.1 * brightness)) # brightness = value between 0&1, by converting color to grayscale
    for color in color_list:
        brightness = get_color_brightness(color)
        actual_len = int(min_line_width * (1 + brightness))
        actual_len = int((actual_len + last_len) / 2)
        last_len = actual_len
        left = (mid - actual_len, counter)
        right = (mid + actual_len, counter)
        draw.line([left, right], fill=color)
        counter += 1
        print(counter)
    del draw
    img.save("PosterWave.png")

如你所见,我为每一帧颜色画了一条水平线。线条的长度取决于颜色的亮度(我假设原始创建者也这么做了)(亮度是0-1之间的值,通过转换为灰度然后除以255)和最后一行的长度,以平滑大的跳跃。但我得到的结果是:

正如你所看到的,两条线之间的间隙太大了,根本不会给人一种波形的错觉。你对创建更平滑的直线对齐所需的数学有什么建议吗?非常感谢


Tags: toimglen颜色linecounterminwidth

热门问题