-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patheditImage.py
52 lines (40 loc) · 1.68 KB
/
editImage.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
from PIL import Image
import urllib.request
#This Code formats and resizes the image and add a black background to the image
def Reformat_Image(lst):
for i in range(0,len(lst)):
try:
image = Image.open(urllib.request.urlopen(lst[i]["url"]))
image_size = image.size
width = image_size[0]
height = image_size[1]
except:
break
if(width != height):
bigside = width if width > height else height
#Resize
background = Image.new('RGBA', (bigside, bigside), (0, 0, 0, 255))
offset = (int(round(((bigside - width) / 2), 0)), int(round(((bigside - height) / 2),0)))
baseheight = 1000
background.paste(image, offset)
wpercent = (baseheight/float(background.size[0]))
hsize = int((float(background.size[1])*float(wpercent)))
background = background.resize((hsize,baseheight), Image.ANTIALIAS)
image = background
else:
print("Image is not resized due to some error!")
#Add Black Background
im1 = Image.open(r'src/background.png')
im2 = image
bg_w, bg_h = im1.size
img_w, img_h = im2.size
offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2)
back_im = im1.copy()
back_im.paste(im2 , offset)
#Save Image
imgID = lst[i]["imgID"]
back_im.save("processed/"+imgID)
image = Image.open("processed/"+imgID)
back = back_im.resize((1920,1080))
back.save("processed/"+imgID)
print("Image " + imgID + " has been edited and saved!")