Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrupted PNG in .resize() #2236

Open
Marcelo-Marinho opened this issue Nov 8, 2024 · 0 comments
Open

Corrupted PNG in .resize() #2236

Marcelo-Marinho opened this issue Nov 8, 2024 · 0 comments
Labels
bug Issues that report (apparent) bugs.

Comments

@Marcelo-Marinho
Copy link

Marcelo-Marinho commented Nov 8, 2024

So, I was trying to make a "auto editor", but in process I made a "auto shorts video", but the image of the pngtuber just corrupts when I try to give it a zoom in effect with this: lambda t: 1 + 0.01 * t . Someone can help me?

Expected Behavior

The image should zoom in and rotate a little without problems

Actual Behavior

The png corrupts and becomes transparent where it shouldn't
Video (in portuguese) showing the problem: https://www.reddit.com/r/moviepy/comments/1gmfzd5/corrupted_png_in_resize/ (yes, I posted in reddit)

Steps to Reproduce the Problem

The problem just appear when I put it in my code .resize(lambda t: 1 + 0.01 * t)

Function video_gen() code

def video_gen():
    #def move_img(time):
    #    return('center', 'top')

    speak = (
        AudioFileClip("assets/gen/fala_google_IA.wav")
        .fx(afx.audio_fadein, 1)
    )

    tempo_vid = speak.duration

    vids_possible = os.listdir("./assets/background-vid")
    bg_vid = (
        VideoFileClip("./assets/background-vid/" + str(random.choice(vids_possible)))
        .subclip(0, int(tempo_vid)+1)
        .fx(vfx.fadein, 1)
        .fx(vfx.colorx, 1)
    )

    try:
        music_bg = (
            AudioFileClip("assets/audio/" + bg_music())
            .subclip(0, int(tempo_vid)+1)
            .fx(afx.volumex, 0.25)
            .fx(afx.audio_fadein, 1)
            #.fx(afx.audio_normalize)
        )
    except:
        print("Error in AI Choice, random choice now")
        musics = os.listdir("./assets/audio")
        music_bg = (
            AudioFileClip("assets/audio/" + random.choice(musics))
            .subclip(0, int(tempo_vid)+1)
            .fx(afx.volumex, 0.25)
            .fx(afx.audio_fadein, 1)
            #.fx(afx.audio_normalize)
        )

    happy_img = (
        ImageClip("assets/img/feliz.png", duration=2)
        .resize((640,640))
        .resize(lambda t: 1 + 0.01 * t) # problem here
        .rotate(lambda t: 1 + 0.75 * t)
        .set_position(('center', 1024-620))
    )
    angry_img = (
        ImageClip("assets/img/bravo.png", duration=2)
        .resize((640,640))
        .resize(lambda t: 1 + 0.01 * t) # problem here
        .rotate(lambda t: 1 + 0.75 * t)
        .set_position(('center', 1024-620))
    )
    normal_img = (
        ImageClip("assets/img/serio.png", duration=2)
        .resize((640,640))
        .resize(lambda t: 1 + 0.01 * t) # problem here
        .rotate(lambda t: 1 + 0.75 * t)
        .set_position(('center', 1024-620))
    )
    dont_like_img = (
        ImageClip("assets/img/desgosto.png", duration=2)
        .resize((640,640))
        .resize(lambda t: 1 + 0.01 * t) # problem here
        .rotate(lambda t: 1 + 0.75 * t)
        .set_position(('center', 1024-620))
    )

    c_img_1 = (
        ImageClip("assets/img/img_1.jpg", duration=2)
        .resize((512, 512))
        .set_position(('center', 'top'))
        .fx(vfx.fadein, 1)
    )

    c_img_2 = (
        ImageClip("assets/img/img_2.jpg", duration=2)
        .resize((512, 512))
        .set_position(('center', 'top'))
    )

    c_img_3 = (
        ImageClip("assets/img/img_3.jpg", duration=2)
        .resize((512, 512))
        .set_position(('center', 'top'))
    )

    c_img_4 = (
        ImageClip("assets/img/img_4.jpg", duration=2)
        .resize((512, 512))
        .set_position(('center', 'top'))
    )


    imgs_array = []
    try:
        subs = pysrt.open('./assets/gen/roteiro.srt')
        temp = 0
        for sub in subs:
            text = sub.text.lower()
            start_time = sub.start
            end_time = sub.end

            txt = text.split(" ")
            file = open('./assets/gen/' + 'roteiro.txt', 'r', encoding='utf-8')
            texto_de_fala = replace_special_chars(file.read()).replace(",", ".").replace("!", ".").replace("?", ".").lower()

            a = list(filter(None, texto_de_fala.split(" ")[temp:len(txt) + temp]))

            if not a == text.split(" "):
                for i in a:
                    if (i.lower() in lista_png):
                        print("png find!", i)
                        temp += 1
                        if i == "[alegre]":
                            imgs_array.append(happy_img.set_start(convert_time(str(start_time))).set_duration(random.randint(2, 5)))
                        elif i == "[brava]":
                            imgs_array.append(angry_img.set_start(convert_time(str(start_time))).set_duration(random.randint(2, 5)))
                        elif i == "[desgosto]":
                            imgs_array.append(dont_like_img.set_start(convert_time(str(start_time))).set_duration(random.randint(2, 5)))
                        else:
                            imgs_array.append(normal_img.set_start(convert_time(str(start_time))).set_duration(random.randint(2, 5)))
                        break
                    elif (not (i.lower() in lista_png)) and (i ==  a[len(a)-1]):
                        print("png doesnt find! Last Word: ", i)
                        temp += 1
            temp = temp + len(txt)

            if temp > len(texto_de_fala.split(" ")):
                temp = len(texto_de_fala.split(" ")) - 6

            print(start_time, " : ", end_time)
    except:
        print("Error, generating one image each 5 seconds")
        print(tempo_vid)
        x_imgs = int(tempo_vid) // 5
        for i in range(0, x_imgs):
            ch = random.randint(0, 3)
            if ch == 0:
                imgs_array.append(happy_img.set_start(i * 5).set_duration(random.randint(2, 5)))
            elif ch == 1:
                imgs_array.append(angry_img.set_start(i * 5).set_duration(random.randint(2, 5)))
            else:
                imgs_array.append(normal_img.set_start(i * 5).set_duration(random.randint(2, 5)))

    generator = lambda text: TextClip(text.encode('utf8'), font='./assets/font/ComicHelvetic_Heavy.otf', fontsize=32, color='yellow', stroke_color='black', stroke_width=0.5, align='center')
    subs = SubtitlesClip('./assets/gen/roteiro.srt', generator)

    #print(TextClip.list('font'))
    bg_music()

    complement_imgs = []
    print("time for each image: ", tempo_vid / 2, " | ", tempo_vid // 2)

    print("bg_vid size:", bg_vid.size)
    print("happy_img size:", happy_img.size)
    print("angry_img size:", angry_img.size)
    print("normal_img size:", normal_img.size)
    print("dont_like_img size:", dont_like_img.size)
    print("c_img_1 size:", c_img_1.size)
    print("c_img_2 size:", c_img_2.size)
    print("c_img_3 size:", c_img_3.size)
    print("c_img_4 size:", c_img_4.size)

    print("happy_img shape:", happy_img.get_frame(0).shape)
    print("angry_img shape:", angry_img.get_frame(0).shape)
    print("normal_img shape:", normal_img.get_frame(0).shape)
    print("dont_like_img shape:", dont_like_img.get_frame(0).shape)
    print("c_img_1 shape:", c_img_1.get_frame(0).shape)
    print("c_img_2 shape:", c_img_2.get_frame(0).shape)
    print("c_img_3 shape:", c_img_3.get_frame(0).shape)
    print("c_img_4 shape:", c_img_4.get_frame(0).shape)

    for i in range(0, 4):
        if i == 0:
            complement_imgs.append(c_img_1.set_start(i * (int(tempo_vid) // 4)).set_duration(tempo_vid // 4))
        elif i == 1:
            complement_imgs.append(c_img_2.set_start(i * (int(tempo_vid) // 4)).set_duration(tempo_vid // 4))
        elif i == 2:
            complement_imgs.append(c_img_3.set_start(i * (int(tempo_vid) // 4)).set_duration(tempo_vid // 4))
        else:
            complement_imgs.append(c_img_4.set_start(i * (int(tempo_vid) // 4)).set_duration(tempo_vid // 4))

    array_comp = [bg_vid]
    for x in complement_imgs:
        array_comp.append(x)
    #imgs_final = concatenate_videoclips(imgs_array[::-1], method='compose')
    #array_comp.append(imgs_final)
    for x in imgs_array[::-1]:
        print("duration: ", x.duration, " | start: ", x.start_time)
        array_comp.append(x)
    array_comp.append(subs.set_position(('center', 'center')))
    
    final_video = CompositeVideoClip(array_comp, size=(576,1024)).subclip(0, int(tempo_vid)+1)
    audio_final = CompositeAudioClip([speak, music_bg])
    final_video.audio = audio_final

    print("final_video size: ", final_video.size)
    final_video.write_videofile("output_videos/video_" + tema + ".mp4")

Specifications

  • Python Version: 3.12.3
  • MoviePy Version: 1.0.3
  • Platform Name: Windows 10
  • Platform Version: 22H2
@Marcelo-Marinho Marcelo-Marinho added the bug Issues that report (apparent) bugs. label Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issues that report (apparent) bugs.
Projects
None yet
Development

No branches or pull requests

1 participant