# Copyright for the Uniform Repository Service (c) 1995 - 2020,
# by Gerald Banon. All rights reserved.
# Version 2.1
# makeThumbnail
# called in MakeThumbnail using the tcl command:
# set imageSize [exec $pythonPath makeThumbnail.py $imageFilePath $thumbnailDirectoryPath $fileRootName $targetFileExtension $targetFlag $imageName]
# $targetFlag == sys.argv[5] values is 0 or 1, 1 means to process the target file


# Python
# import os
import sys
# os.getcwd()
# os.chdir(sys.argv[1] + "/col/iconet.com.br/banon/2008/11.01.15.09/doc")
# from PIL import Image - commented by GJFB in 2018-11-10 - this command didn't work at USP - it resuls in "ImportError: No module named PIL"
# the code below was added by GJFB in 2019-06-20 - required with OS of mtc-m12: Linux version 3.10.0-862.14.4.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP Wed Sep 26 15:12:11 UTC 2018
# after running yum install python-imaging, this OS accepted only "from PIL import Image"
try:
	import Image
except ImportError:
	from PIL import Image	
	
# im = Image.open(sys.argv[1] + ".jpg")
inputImage = Image.open(sys.argv[1])
if sys.argv[5] == "1": print inputImage.size

# FILE = open(sys.argv[1] + "JPGDim.txt","w")
# FILE.write(str(im.size))
# FILE.close()

# if sys.argv[2] == "S":

# maximum width is 120 and maximum height is 80

if sys.argv[5] == "1":
# process the target file
# 1 - thumbnail appearing in the search result page
	if inputImage.size[0] / (inputImage.size[1] + 0.) < 120 / 80.:
		newSize = int(round(inputImage.size[0] * 80. / inputImage.size[1])), 80
	else:
		newSize = 120, int(round(inputImage.size[1] * 120. / inputImage.size[0]))
	outputImage = inputImage.resize(newSize, Image.ANTIALIAS)
	outputImage.save(sys.argv[2] + "/" + sys.argv[3] + "1" + sys.argv[4])
#	print 11
	
# 2 - thumbnail appearing in Archival Unit
	if inputImage.size[0] < inputImage.size[1]:
		newSize = int(round(inputImage.size[0] * 240. / inputImage.size[1])), 240
	else:
		newSize = 240, int(round(inputImage.size[1] * 240. / inputImage.size[0]))
	outputImage = inputImage.resize(newSize, Image.ANTIALIAS)
	outputImage.save(sys.argv[2] + "/" + sys.argv[3] + "2" + sys.argv[4])
#	print 22
	
# 3 - thumbnail appearing next to the full metadata
	if inputImage.size[0] < inputImage.size[1]:
		newSize = int(round(inputImage.size[0] * 360. / inputImage.size[1])), 360
	else:
		newSize = 360, int(round(inputImage.size[1] * 360. / inputImage.size[0]))
	outputImage = inputImage.resize(newSize, Image.ANTIALIAS)
	outputImage.save(sys.argv[2] + "/" + sys.argv[3] + "3" + sys.argv[4])
#	print 33
	
# 4 - thumbnail appearing in the gallery
newSize = int(round(inputImage.size[0] * 60. / inputImage.size[1])), 60
outputImage = inputImage.resize(newSize, Image.ANTIALIAS)
outputImage.save(sys.argv[2] + "/" + sys.argv[3] + "4-" + sys.argv[6])

# 5 - image displayed in the gallery
# in 2012-12-15 height was changed from 400 to 480
# in 2012-12-15 width was changed from 600 to 720

if inputImage.size[0] / (inputImage.size[1] + 0.) < 720 / 480.:
	newSize = int(round(inputImage.size[0] * 480. / inputImage.size[1])), 480
else:
	newSize = 720, int(round(inputImage.size[1] * 720. / inputImage.size[0]))
outputImage = inputImage.resize(newSize, Image.ANTIALIAS)
outputImage.save(sys.argv[2] + "/" + sys.argv[3] + "5-" + sys.argv[6])
# print outputImage.size

# FILE = open(sys.argv[1] + "PGMDim.txt","w")
# FILE.write(str(im2.size))
# FILE.close()
