-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.py
More file actions
105 lines (93 loc) · 2.67 KB
/
Copy pathconvert.py
File metadata and controls
105 lines (93 loc) · 2.67 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
##############################################
## FINAL IMAGE CONVERSION/RESIZER ##
##############################################
# Date - 27th May, 2017
# Version - 1
# Use - Run convert.py in each image directory
import os
import os.path
from PIL import Image, ImageEnhance, ImageFilter
#-------------------------
#-- Extensions to select
#-------------------------
ext = (".jpg",".png",".jfif",".jpeg",".gif")
#-------------------------
#-- Base Width
#-------------------------
base = 400
#-------------------------
#-- Header
#-------------------------
print("="*60)
print(" "*18+"HOSTEL IMAGES ENHANCER")
print("="*60)
print("\n\n")
#-------------------------
#-- Resizing Image
#-------------------------
def change(image,dim):
base = dim
wper = base/image.size[0]
maxh = int(float(image.size[1])*wper)
dime = (base,maxh)
image = image.resize(dime)
return image
#-------------------------
#-- Enhance Image
#-------------------------
def beauty(image):
image = ImageEnhance.Color(img).enhance(1.1)
image = ImageEnhance.Brightness(img).enhance(1.1)
return image
#-------------------------
#-- Find Files
#-------------------------
for (dirpath, dirname, fname) in os.walk(os.getcwd()):
#-- Loop over the Files
for fi in fname:
#-- Check if the file is an image or not
if fi.endswith(ext):
try:
img = Image.open(fi)
print("Converting {0}...".format(fi), end=" ")
#-- Get the filename and extension
n,e = fi.split(".")
#-- Create the new filename
new = n+".jpg"
#-- Send the image for enhancing
img = beauty(img)
#-- Check if the image is too small
if img.size[0]<400:
#-- Create the larger image
temp = img.copy()
temp = change(temp,base)
#-- Blur the larger image
temp = temp.filter(ImageFilter.GaussianBlur(20))
#-- Save the image to the temporary file
temp.save("tmp.jpg")
#-- Make the original image larger
over = img.copy()
if over.size[0]<200:
overbase = over.size[0]*2
img = change(img,overbase)
#-- Select the image and paste it onto the larger one
box = (0,0,img.size[0],img.size[1])
posw = int((base-img.size[0])/2)
posh = int((temp.size[1]-img.size[1])/2)
pos = (posw,posh)
region = img.crop(box)
temp.paste(region,pos)
temp.save("tmp.jpg")
print("done!")
#-- If the image size is good
else:
img = change(img,base)
os.remove(fi)
img.save(new)
print("done!")
#-- Remove the temporary image created
if os.path.exists("tmp.jpg"):
os.remove(fi)
os.rename("tmp.jpg",new)
except:
pass