-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgridly.py
80 lines (66 loc) · 1.97 KB
/
gridly.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import sys
from PIL import Image
from os import walk, path, makedirs
from mutagen import File
import imghdr
from random import shuffle
import math
import shutil
#size of each individual thumbnail
size = int(sys.argv[1])
#filelist that hold the mp3filepaths
filelist = []
filepath = raw_input('Enter the directory you want to use(abs path): ')
#read the directory and populate the filelist
for dirpath, dirname, files in walk(filepath):
for file in files:
if path.splitext(file)[1] == '.mp3':
filelist.append([path.join(dirpath, file), path.splitext(file)[0]])
#temporary filelist holder for images temp folder
tempfilelist = []
thumbsize = size, size
temppath = 'temp'
#create the temporary directory in the folder to hold the extracted thumbnails
if not path.exists(temppath):
makedirs(temppath)
#extract the albumart from the mp3 files and reduce the size to thumbnail size
for file in filelist:
try:
im_file = File(file[0])
artwork = im_file.tags['APIC:'].data
with open(path.join('temp', file[1])+'.jpg', 'wb') as img:
img.write(artwork)
tempfilelist.append(path.join('temp', file[1]+'.jpg'))
except:
pass
for file in tempfilelist:
im = Image.open(file)
im.thumbnail(thumbsize)
im.save(file, imghdr.what(file))
#shuffle the list for random order of imagegrid
shuffle(tempfilelist)
sqroot = int(math.floor(math.sqrt(len(tempfilelist))))
newquant = int(math.pow(sqroot, 2))
#blank new image to hold the grid
newimg = Image.new('RGB', (sqroot*size,sqroot*size))
#open all the thumnail images
images = map(Image.open, tempfilelist)
width = size
height = size
#offset variables
x_offset = 0
y_offset = 0
counter = 0
#paste the thumbnail images on the newimage as per the offset
for im in images:
newimg.paste(im, (x_offset,y_offset))
x_offset += width
counter+=1
if(counter == sqroot):
y_offset += height
counter = 0
x_offset = 0
#remove the temporary folder
shutil.rmtree('temp')
#save the newimage
newimg.save(raw_input('Enter the name for final image: '))