<jupyter_start><jupyter_text>**ALBUM ART GAN NOTEBOOK***by <NAME><jupyter_code>import pandas as pd
import numpy as np
import sklearn as sk
import os.path
import requests

df = pd.read_csv("masterAlbumList.csv")
col_names = ['album_id', 'link_to_image', 'album_name', 'artist_name', 'artist_id', 'genre', 'year']

#first 58 lines were unnecessary appendages of the column legend
#df = df.drop(df.index[:58])

#see original size of dataframe
print("Original Size of dataframe",df.shape)

#csv list only eliminated duplicates based off of album id, but not based off album name
#ive found some artists have the same album uploaded more than once on their page, but the album has different ids
#we need to get rid of duplicates based on the album name and artist
b = df.drop_duplicates(subset=['album_name', 'artist_name'], keep="first")

#lets see how many there are now,
print("Size of dataframe after dropping duplicates",b.shape)
#check if there are any null or NaN values 
print(b.isnull().any())

#use this to check if there is a specific artist in the dataset
print("The Black Keys" in b["artist_name"].values)

#number of different artists
unique_artists = b["artist_name"].unique()
print("Number of unique artists: ",len(unique_artists))

#use this to see what different genres there are in the dataset
unique_genres = b["genre"].unique()
#number of different genres
print("Number of unique genres: ",len(unique_genres))
#theres a lot so i just wanted to see the first 50
print(unique_genres[ :])

#okay, now if we want to create separate dataframes of subsets, such as genre, or year, or artist, we can do so

metalgenres = ['post-hardcore','metallic hardcore','metalcore','melodic metalcore','christian alternative rock',
              'progressive post-hardcore', 'alternative metal','american metalcore','christian metal','christian hardcore',
              'alabama metal','melodic hardcore' ,'deathcore', 'groove metal','death metal','christian deathcore','hardcore',
              'beatdown','boston hardcore','djent','screamo' ,'swancore','post-post-hardcore','black metal','psychobilly', 
              'pornogrind','experimental black metal','danish metal']

metal = b.loc[b['genre'].isin(metalgenres)]
print(metal.shape)


indiegenres = ['indie','pop punk','indie pop','mathcore','indie folk','indie rock','mathcore','canadian punk','modern alternative rock',
              'indie garage rock','american post-rock','instrumental post-rock','pop emo','american shoegaze','texas pop punk',
              'manchester indie','auckland indie','australian indie','detroit indie','indie anthem-folk','folk-pop',
              'folk punk','midwest emo','bay area indie','canadian folk','canadian punk','diy emo','canadian pop punk',
              'indie garage rock',]

indie = b.loc[b['genre'].isin(indiegenres)]
print(indie.shape)    


trippygenres = ['indie pop','electropowerpop','art pop','cyberpunk','dream pop','alternative dance','ambient','neo mellow',
               'modern dream pop','metropopolis','finnish dance pop','electropop','europop' ,'chamber psych','new rave',
               'deep pop edm','vapor pop', 'bass trap','pop edm', 'edm','indie electropop','american shoegaze','deep house','deep tropical house',
               'tropical house','filter house','aussietronica','lo-fi beats','glitch hop','glitch','shamanic','deep psytrance',
               'electro house','deep melodic euro house','house','acid rock' ,'psychedelic trance','forest psy',
               'deep progressive trance','progressive psytrance','psychill','dutch edm']

trippy = b.loc[b['genre'].isin(trippygenres)]
print(trippy.shape) 

#here we define a function that will iterate through your dataframe,
#visit the images, and download those images to the folder specified
#if the path to folder is not specified, it will create/download to a directory called "albumartimages"

def getAndSaveAlbumArts(df, path = 'albumartimages/'):
    isdir = os.path.isdir(path)
    df = df[['link_to_image','album_name','artist_name']]
    if isdir is not True:
        os.mkdir(path)
    for i, j in df.iterrows():
        artistAlbumName = j[2]+ "-" + j[1]
        artistAlbumName = artistAlbumName[:200] if len(artistAlbumName) > 200 else artistAlbumName
        fileName = artistAlbumName + ".jpg"
        fileName = fileName.replace(r'/', ' ')
        url = j[0]
        r = requests.get(url)
        fullPath = path + fileName
        with open(fullPath, 'wb') as f:
            f.write(r.content)
    print("done")

    
getAndSaveAlbumArts(trippy, path = 'trippy/')
"""
This cell is a function to resize the images after been downloaded into a directory.
Coming from Spotify, most images are 640x640, but for our GAN, we want them to 
be a bit more manageable in size so we'll resize them to 128x128
"""

from PIL import Image
import os, sys

src = "trippy/"
dst = "trippy_resized/"
dirs = os.listdir( src )


def resize():
    if os.path.isdir(dst) is not True:
        os.mkdir(dst)
    i=0
    for item in dirs:
        if (os.path.isfile(src+item) and item != ".DS_Store"):
            im = Image.open(src+item)
            f, e = os.path.splitext(src+item)
            imResize = im.resize((128,128), Image.ANTIALIAS)
            output_file_name = os.path.join(dst, str(i) + ".jpeg")
            imResize.save(output_file_name, 'JPEG', quality=90)
            i+=1

#resize()
"""Once the images are resized, they all need to be normalized 
and then they can be saved to a pickle file or a python object 
so they can be read in easier than going through image by image
in a directory"""
#https://towardsdatascience.com/generating-modern-arts-using-generative-adversarial-network-gan-on-spell-39f67f83c7b4

import os, sys
import numpy as np
from PIL import Image
def normalize_images():
    IMAGE_SIZE = 128
    IMAGE_CHANNELS = 3
    IMAGE_DIR = 'resized_imgs/'
    
    images_path = IMAGE_DIR 

    #create an empty array all the images will go into
    training_data = []
    
    for filename in os.listdir(images_path):
        path = os.path.join(images_path, filename)
        #only need this if the image is not the right dimensions, but we're pulling from the already resized folder
        image = Image.open(path).resize((IMAGE_SIZE, IMAGE_SIZE), Image.ANTIALIAS)
        image = image.convert('RGB')
        image = np.asarray(image)
        image = (image / 127.5) - 1
        training_data.append(np.asarray(image))
    
    #reshape the whole array
    training_data = np.reshape(training_data, (-1, IMAGE_SIZE, IMAGE_SIZE, IMAGE_CHANNELS))
    #training_data = training_data / 127.5-1
    np.savez_compressed('images_normalized.npy', training_data)
    
#normalize_images()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup