-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrop_and_append.py
35 lines (31 loc) · 1.29 KB
/
crop_and_append.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
from PIL import Image
def getThumbnailsList(imagePathList, thumbnailSize):
thumbnailsList = []
for imagePath in imagePathList:
try:
with Image.open(imagePath) as im:
im.thumbnail((thumbnailSize[0], thumbnailSize[1]))
thumbnailsList.append(im)
except OSError:
print("cannot create thumbnail for", imagePath)
return thumbnailsList
def mergeGivenImageList(thumbnailsList, thumbnailSize, matrixW, matrixH):
if len(thumbnailsList) > matrixW * matrixH:
raise Exception("Image list can't fit into given matrix size.")
w = matrixW * thumbnailSize[0]
h = matrixH * thumbnailSize[1]
outIm = Image.new("RGB", (w,h))
for index in range(len(thumbnailsList)):
location = ((int(index % matrixW)*thumbnailSize[0]), (int(index / matrixH)*thumbnailSize[1]))
print(location)
outIm.paste(thumbnailsList[index], location)
return outIm
def getthumbnail(imagePathList, outPath, matrixW, matrixH, thumbnailSize=(1200,800)):
outfile = outPath + ".jpg"
try:
outIm = mergeGivenImageList(getThumbnailsList(imagePathList, thumbnailSize), thumbnailSize, matrixW, matrixH)
outIm.save(outfile)
except OSError:
print(OSError)
print("cannot convert")
return True