0

Python script to tag mp3 files..

Here is the script:


import os
import eyed3
from eyed3.mp3 import isMp3File

# Script to tag the mp3 files, given a folder (and possibly sub-folders)
# containing mp3 files.
# This script will take the folder name and assign it as album, file name
# and assign it as title track, and a given string as the artist and album
# artist

def updateMp3Tags(fileName, album, artist, title):
      audiofile = eyed3.load(fileName)
      audiofile.tag.artist = unicode(artist)
      audiofile.tag.album = unicode(album)
      audiofile.tag.album_artist = unicode(artist)
      audiofile.tag.title = unicode(title)
      audiofile.tag.save()

def setupMp3Tags(fileName, dirName, topDir, artist):
      trackTitle = os.path.splitext(fileName)[0]
      album = os.path.relpath(dirName, topDir).replace("\\", "_")
      print "File: " + fileName + " title: " + trackTitle
      print "ALBUM: " + album + " artist: " + artist
      updateMp3Tags(os.path.join(dirName, fileName), album, artist, trackTitle)


def tagMp3Files(topDir, artist):
      if not os.path.exists(topDir):
            print topDir + " does not exist"
            return
      
      for dirName, subDirList, fileList in os.walk(topDir):
            print "Renaming files in === " + dirName + " ==="
            # Skip the files that are not mp3
            for fileName in fileList:
                  if not isMp3File(os.path.join(dirName, fileName)):
                        print "Skipping " + fileName + " ..."
                  else:
                        setupMp3Tags(fileName, dirName, topDir, artist)

                  
tagMp3Files(r"C:\kannadaAudio", "Vidyabhushana")

Advertisements
0

Recursive Triangles with Turtle

After much fight, here is the code for recursive triangles in Turtle:


import turtle

# We will draw equilateral triangles, one inside the other

def set_turtle_position(myTurtle, x, y):
    myTurtle.penup()
    myTurtle.setx(x)
    myTurtle.sety(y)
    myTurtle.pendown()

def get_turtle_position(myTurtle):
    return (myTurtle.xcor(), myTurtle.ycor())

def draw_triangle(myTurtle, length, isReverse):
    myTurtle.setheading(0)

    if (isReverse):
        pacman.color("blue", "white")
    else:
        pacman.color("blue", "green")

    pacman.begin_fill()
    for i in range(1, 4):
        myTurtle.forward(length)
        if (isReverse):
            myTurtle.right(120)
        else:
            myTurtle.left(120)
    pacman.end_fill()

def draw_recursive_triangles_internal(myTurtle, length, curDepth, maxDepth, position):
    if (curDepth > maxDepth):
        return
            
    (x1, y1) = position
    (x2, y2) = (x1+(length/2), y1)
    
    set_turtle_position(myTurtle, x1, y1)
    myTurtle.left(60)
    myTurtle.fd(length/2)
    draw_triangle(myTurtle, length/2, 1)

    (x3, y3) = get_turtle_position(myTurtle)
        
    draw_recursive_triangles_internal(myTurtle, length/2, curDepth+1, maxDepth, (x2, y2))
    draw_recursive_triangles_internal(myTurtle, length/2, curDepth+1, maxDepth, (x3, y3))
    draw_recursive_triangles_internal(myTurtle, length/2, curDepth+1, maxDepth, (x1, y1))
    

def draw_recursive_triangles(myTurtle, length, maxDepth):
    currentDepth = 1
    position = get_turtle_position(myTurtle)

    draw_recursive_triangles_internal(myTurtle, length, currentDepth, maxDepth, position)

# Basic screen and turtle configuration
canvas = turtle.Screen()
pacman = turtle.Turtle()
pacman.shape("turtle")
pacman.shapesize(1, 1, 1)
pacman.pensize(2)
canvas.bgcolor("white")
pacman.speed(10)

# Initial turtle position
set_turtle_position(pacman, -180, -150)
pacman.setx(-180)
pacman.sety(-150)
pacman.clear()

draw_triangle(pacman, 400, 0)
draw_recursive_triangles(pacman, 400, 5)

canvas.exitonclick()


0

Turtle Code to draw a diamond flower

Is like this:


import turtle

numDiamonds = 40
angleOfRotation = 360/numDiamonds

def draw_diamond(myTurtle, length):
myTurtle.left(15)
myTurtle.forward(length)
myTurtle.right(30)
myTurtle.forward(length)
myTurtle.right(150)
myTurtle.forward(length)
myTurtle.right(30)
myTurtle.forward(length)
myTurtle.right(165)

def draw_diamond_flower(myTurtle, length):

for i in range(1, numDiamonds+1):
draw_diamond(myTurtle, length)
myTurtle.right(angleOfRotation)

def draw_flower_stick(myTurtle, length):
myTurtle.right(180)
myTurtle.forward(length)

canvas = turtle.Screen()
pacman = turtle.Turtle()
pacman.shape("turtle")
pacman.shapesize(1, 1, 1)
pacman.color("blue")
pacman.pensize(2)
canvas.bgcolor("white")
pacman.speed(0)

pacman.left(90)
draw_diamond_flower(pacman, 80)
draw_flower_stick(pacman, 300)
canvas.exitonclick()

0

Drawing Circle with Squares

I am currently going through “Foundations of Python” Course on Udacity and they got an interesting an assignment to draw circles with theĀ Turtle package.

import turtle

def draw_square(myTurtle, length):
    for i in range(1, 5):
        myTurtle.forward(length)
        myTurtle.right(90)

def draw_circle_with_square(myTurtle, radius):
    numSquares = 30
    angleOfRotation = 360/numSquares
    
    for i in range(1, numSquares + 1):
        myTurtle.right(angleOfRotation)
        draw_square(myTurtle, radius)

canvas = turtle.Screen()
pacman = turtle.Turtle()
pacman.shape("turtle")
pacman.shapesize(0.1, 0.1, 0.1)
pacman.color("red")
canvas.bgcolor("yellow")

draw_circle_with_square(pacman, 100)