-
Notifications
You must be signed in to change notification settings - Fork 11
/
preparephotoshelper.py
90 lines (67 loc) · 2.31 KB
/
preparephotoshelper.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
81
82
83
84
85
86
87
88
89
90
import os
import glob
import sys
import math
import shutil
import multiprocessing
# Install the PIL Library from Python Package Manager in ArcGIS Pro:
# Click 'Add Packages' and select 'pil' then 'Install'
from PIL import Image
from PIL import ImageStat
from PIL import ImageEnhance
import arcpy
TARGET_LUM = 100
def getSupportedImages(directory):
imgTypes = ('jpg', 'jpeg')
imgList = []
for img in imgTypes:
imgList.extend(glob.glob(directory + "/*.{}".format(img)))
return imgList
def processImage(x, output_dir, enh, imgS):
#arcpy.SetProgressor("step","Processing Photos...",0,len(image_list))
# for x in image_list:
newpath = os.path.join(output_dir, os.path.basename(x))
img = Image.open(x)
if 'exif' in img.info:
exif = img.info['exif']
else:
exif = None
#Resize
widthSize = imgS
heightSize = imgS
(width, height) = img.size # get the size of the input image
if width > imgS or height > imgS: #only resize if height or width is greater than maxdim
if width > height:
heightSize = (imgS * height) / width
elif height > width:
widthSize = (imgS * width) / height
img = img.resize((int(widthSize), int(heightSize)), Image.ANTIALIAS)
#Enhance Photo
if enh:
lum = brightness(img)
enhancer = ImageEnhance.Brightness(img)
img = enhancer.enhance(1.0 + ((TARGET_LUM - lum) / lum))
if exif:
img.save(newpath, exif=exif)
else:
img.save(newpath)
del img
#arcpy.SetProgressorPosition()
return
def brightness(im):
stat = ImageStat.Stat(im)
gs = (math.sqrt(0.241 * (r ** 2) + 0.691 * (g ** 2) + 0.068 * (b**2))
for r, b, g in im.getdata())
return sum(gs) / stat.count[0]
def execute(inputDir, outDir, enh, imgS):
imgList = getSupportedImages(inputDir)
pythonExePath = sys.exec_prefix + "\\pythonw.exe"
multiprocessing.set_executable(pythonExePath)
cpus = 1
if int(multiprocessing.cpu_count() / 2) > 1:
cpus = int(multiprocessing.cpu_count() / 2)
pool = multiprocessing.Pool(processes=cpus)
if imgList:
pool.starmap(processImage, [(img, outDir, enh, imgS) for img in imgList])
else:
arcpy.AddError("No images available to process. Check input directory for JPG photos")