Python Image Volume Change - learnit

Home Top Ad

Post Top Ad

Friday, July 16, 2021

Python Image Volume Change

Python Image Volume Change

Python Image Volume Change

Today I'll show you how to make an audio image. When we add audio, the image changes as well. Before I begin showing you how to write in Python, I'd like to discuss Python and PyGame.


What is Python?

Python is an object-oriented, high-level programming language with dynamic semantics that is interpreted. Its high-level built-in data structures, combined with dynamic typing and dynamic binding, make it very appealing for Rapid Application Development, as well as use as a scripting or glue language to connect existing components. Python's simple, easy-to-learn syntax emphasizes readability and, as a result, lowers program maintenance costs. Python provides support for modules and packages, which promotes program modularity and code reuse. The Python interpreter and extensive standard library are free to use and distribute in source or binary form for all major platforms.


Python is frequently embraced by programmers due to the increased productivity it provides. The edit-test-debug cycle is extremely fast because there is no compilation step. Debugging Python programs is simple: a bug or incorrect input will never result in a segmentation fault. When the interpreter detects an error, it throws an exception.


The interpreter prints a stack trace if the program does not catch the exception. A source level debugger enables you to inspect local and global variables, evaluate arbitrary expressions, set breakpoints, step through code one line at a time, and so on. The debugger is written in Python, demonstrating the language's introspective power. On the other hand, adding a few print statements to the source code is often the quickest way to debug a program: the fast edit-test-debug cycle makes this simple approach very effective.


PyGame

pygame is a Python-based free and open-source cross-platform library for creating multimedia applications such as video games. It abstracts the most common functions using the Simple DirectMedia Layer library and several other popular libraries, making writing these programs easier.


Code


from tkinter import *
import pygame
from tkinter import filedialog
import time
from mutagen.mp3 import MP3
import tkinter.ttk as ttk
root= Tk()
root.title('reanits.com')
root.iconbitmap('1.png')
root.geometry('300x400')
pygame.mixer.init()
def playtime():
    if stopped:
        return
    Currenttime = pygame.mixer.music.get_pos()/1000
    ConvertCurrentTime = time.strftime('%M:%S',time.gmtime(Currenttime))
    song = songbox.get(ACTIVE)
    song = f'D:/Python/Test2/sound/{song}.mp3'
    songmut = MP3(song)
    global SongLength
    SongLength=songmut.info.length
    ConvertSongLength = time.strftime('%M:%S', time.gmtime(SongLength))
    Currenttime += 1
    if int(myslider.get()) == int(SongLength):
        statusbar.config(text=f'Time Elasped:{ConvertSongLength} ')
    elif paused:
        pass
    elif int(myslider.get()) == int(Currenttime):
        SliderPosition = int(SongLength)
        myslider.config(to=SliderPosition, value=int(Currenttime))
    else:
        SliderPosition = int(SongLength)
        myslider.config(to=SliderPosition, value=int(myslider.get()))
        ConvertCurrentTime = time.strftime('%M:%S', time.gmtime(int(myslider.get())))
        statusbar.config(text=f'Time Elasped: {ConvertCurrentTime} of {ConvertSongLength} ')
        nexttime=int(myslider.get())+1
        myslider.config(value=nexttime)
statusbar.after(1000,playtime)
def deletesongmusic():
    stopmusic()
    songbox.delete(ANCHOR)
    pygame.mixer.music.stop()
def deletesongsmusics():
    stopmusic()
    songbox.delete(0,END)
    pygame.mixer.music.stop()
def addsongmusic():
    song = filedialog.askopenfilename(initialdir='sound/', title="Choose File Song", filetypes=(("MP3 file", "*.mp3"),))
    song = song.replace("D:/Python/Test2/sound/","")
    song = song.replace(".mp3", "")
    songbox.insert(END,song)
def addsongsmusic():
    songs = filedialog.askopenfilenames(initialdir='sound/', title="Choose File Song", filetypes=(("MP5 file", "*.mp3"),))
    for song in songs:
        song = song.replace("D:/Python/Test2/sound/", "")
        song = song.replace(".mp3", "")
        songbox.insert(END, song)
def playmusic():
    global stopped
    stopped = False
    song = songbox.get(ACTIVE)
    song = f'D:/Python/Test2/sound/{song}.mp3'
    pygame.mixer.music.load(song)
    pygame.mixer.music.play(loops=0)
    playtime()
    currentvolume = pygame.mixer.music.get_volume()
    currentvolume = currentvolume * 100
    if int(currentvolume) < 1:
        volumemeter.config(image=vol0)
    elif int(currentvolume) > 0 and int(currentvolume) < 25:
        volumemeter.config(image=vol1)
    elif int(currentvolume) >= 25 and int(currentvolume) < 75:
        volumemeter.config(image=vol2)
    elif int(currentvolume) >= 75 and int(currentvolume) <= 100:
        volumemeter.config(image=vol3)
global stopped
stopped = False
def stopmusic():
    statusbar.config(text='')
    myslider.config(value=0)
    pygame.mixer.music.stop()
    songbox.select_clear(ACTIVE)
    statusbar.config(text='')
    global stopped
    stopped = True
    currentvolume = pygame.mixer.music.get_volume()
    currentvolume = currentvolume * 100
    if int(currentvolume) < 1:
        volumemeter.config(image=vol0)
    elif int(currentvolume) > 0 and int(currentvolume) < 25:
        volumemeter.config(image=vol1)
    elif int(currentvolume) >= 25 and int(currentvolume) < 75:
        volumemeter.config(image=vol2)
    elif int(currentvolume) >= 75 and int(currentvolume) <= 100:
        volumemeter.config(image=vol3)
def nextmusic():
    if stopped:
        return
    statusbar.config(text='')
    myslider.config(value=0)
    nextone = songbox.curselection()
    nextone = nextone[0]+1
    song = songbox.get(nextone)
    song = f'D:/Python/Test2/sound/{song}.mp3'
    pygame.mixer.music.load(song)
    pygame.mixer.music.play(loops=0)
    songbox.select_clear(0,END)
    songbox.activate(nextone)
    songbox.selection_set(nextone, last=None
def preback():
    if stopped:
        return
    statusbar.config(text='')
    myslider.config(value=0)
    nextone = songbox.curselection()
    nextone = nextone[0] - 1
    song = songbox.get(nextone)
    song = f'D:/Python/Test2/sound/{song}.mp3'
    pygame.mixer.music.load(song)
    pygame.mixer.music.play(loops=0)
    songbox.select_clear(0, END)
    songbox.activate(nextone)
    songbox.selection_set(nextone, last=None)
global paused
paused = False
def pause(is_paused):
    global paused
    paused = is_paused
    if paused:
        pygame.mixer.music.unpause()
        paused = False
    else:
        pygame.mixer.music.pause()
        paused = True
def slider(x):
    song = songbox.get(ACTIVE)
    song = f'D:/Python/Test2/sound/{song}.mp3'
    pygame.mixer.music.load(song)
    pygame.mixer.music.play(loops=0, start=int(myslider.get()))
def volumn(x):
    pygame.mixer.music.set_volume(volumnslider.get())
    currentvolume = pygame.mixer.music.get_volume()
    currentvolume = currentvolume * 100
    if int(currentvolume) < 1:
        volumemeter.config(image=vol0)
    elif int(currentvolume)>0 and int(currentvolume)< 25:
        volumemeter.config(image=vol1)
    elif int(currentvolume)>=25 and int(currentvolume)<75:
        volumemeter.config(image=vol2)
    elif int(currentvolume)>=75 and int(currentvolume)<=100:
        volumemeter.config(image=vol3)
masterframe = Frame(root)
masterframe.pack(pady=10
songbox= Listbox(masterframe, bg="black", fg="green", width=80,selectbackground="gray",selectforeground="black")
songbox.grid(row=0, column=0)
backbtn= PhotoImage(file='image/forward101.png')
forwardbtn= PhotoImage(file='image/forward101.png')
playbtn= PhotoImage(file='image/Play101.png')
pausebtn= PhotoImage(file='image/pause101.png')
stopbtn= PhotoImage(file='image/Stop101.png')
#Create Icon Volume
global vol0
global vol1
global vol2
global vol3
vol0 = PhotoImage(file='image/volumn-1.png')
vol1 = PhotoImage(file='image/volumn-2.png')
vol2 = PhotoImage(file='image/volumn-3.png')
vol3 = PhotoImage(file='image/volumn-4.png')
controlframe= Frame(masterframe)
controlframe.grid(row=1,column=0,pady=20)
volumemeter = Label(masterframe, image=vol0)
volumemeter.grid(row=1, column=1, padx=10)
volumnframe = LabelFrame(masterframe,text="Volumn")
volumnframe.grid(row=0, column=1,padx=20)
backbutton= Button(controlframe, image=backbtn, borderwidth=0, command=preback)
frowardbutton= Button(controlframe, image=forwardbtn, borderwidth=0,command=nextmusic)
playbutton= Button(controlframe, image=playbtn, borderwidth=0, command=playmusic)
pausebutton= Button(controlframe, image=pausebtn, borderwidth=0, command=lambda: pause(paused))
stopbutton= Button(controlframe, image=stopbtn, borderwidth=0, command=stopmusic)
backbutton.grid(row=0, column=1, padx=10)
frowardbutton.grid(row=0, column=2, padx=20)
playbutton.grid(row=0, column=3, padx=20)
pausebutton.grid(row=0, column=4, padx=20)
stopbutton.grid(row=0, column=5, padx=20)
#create Menu
mymenu = Menu(root)
root.config(menu=mymenu)
addsong = Menu(mymenu)
mymenu.add_cascade(label="file", menu=addsong)
addsong.add_command(label="Open Song or Playlist",command=addsongmusic)
addsong.add_command(label="Open Many Songs or Playlist",command=addsongsmusic)
deletesong = Menu(mymenu)
mymenu.add_cascade(label="Delete", menu=deletesong)
deletesong.add_command(label="Delete Song or Playlist",command=deletesongmusic)
deletesong.add_command(label="Delete All Songs or Playlist",command=deletesongsmusics)
statusbar= Label(root, text='',bd=1, relief=GROOVE, anchor=E)
statusbar.pack(fill=X,side=BOTTOM, ipady=2)
myslider = ttk.Scale(masterframe,from_=0, to=100, orient=HORIZONTAL, value=0, command=slider, length=500)
myslider.grid(row=2,column=0,pady=20)
volumnslider = ttk.Scale(volumnframe,from_=0, to=1, orient=VERTICAL, value=1, command=volumn, length=160)
volumnslider.pack(pady=20)
root.mainloop()


Please Watching My video is Below

No comments:

Post a Comment

Post Top Ad