# Copyright for the Uniform Repository Service (c) 1995 - 2025,
# by Gerald Banon and Gabriela 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
# example of tcl call:
# 1 > imageFilePath = C:/Users/geral/OneDrive/URLib 2/col/urlib.net/www/2017/05.22.18.46/doc/foto_pessoal.jpg
# 2 > thumbnailDirectoryPath = C:/Users/geral/OneDrive/URLib 2/col/urlib.net/www/2017/05.22.18.46/images
# 3 > fileRootName = thumbnail
# 4 > targetFileExtension = jpg
# 5 > targetFlag = 0 (the image is not the target file), 1 (the image is the target file) or 2 (the image is not the target file and the target file is a PDF)
# 6 > imageName (ex: foto_pessoal.jpg)
# examples of thumbnail names:
# thumbnail1.jpg
# thumbnail2.jpg
# thumbnail3.jpg
# thumbnail4-foto_pessoal.jpg
# thumbnail5-foto_pessoal.jpg

# Python
# exit()
# 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	# commented by GJFB in 2024-05-29 - works with python 2.6
if sys.argv[5] == "1": print(inputImage.size)	# added by GJFB in 2024-05-29 - works with python 3.12

# 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":	# commented by GJFB in 2025-08-28
if sys.argv[5] in ("1", "2"):	# added by GJFB in 2025-08-28
# 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)	# commented by GJFB in 2024-05-29 - works with python 2.6
	outputImage = inputImage.resize(newSize, Image.Resampling.LANCZOS)	# added by GJFB in 2024-05-29 - works with python 3.12
	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)	# commented by GJFB in 2024-05-29 - works with python 2.6
	outputImage = inputImage.resize(newSize, Image.Resampling.LANCZOS)	# added by GJFB in 2024-05-29 - works with python 3.12
	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)	# commented by GJFB in 2024-05-29 - works with python 2.6
	outputImage = inputImage.resize(newSize, Image.Resampling.LANCZOS)	# added by GJFB in 2024-05-29 - works with python 3.12
	outputImage.save(sys.argv[2] + "/" + sys.argv[3] + "3" + sys.argv[4])
#	print(33)

if not (sys.argv[5] == "2"):	# if added by GJFB in 2025-08-28
# is 0 or 1	
# 4 - thumbnail appearing in the gallery
	newSize = int(round(inputImage.size[0] * 60. / inputImage.size[1])), 60
# outputImage = inputImage.resize(newSize, Image.ANTIALIAS)	# commented by GJFB in 2024-05-29 - works with python 2.6
	outputImage = inputImage.resize(newSize, Image.Resampling.LANCZOS)	# added by GJFB in 2024-05-29 - works with python 3.12
	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)	# commented by GJFB in 2024-05-29 - works with python 2.6
	outputImage = inputImage.resize(newSize, Image.Resampling.LANCZOS)	# added by GJFB in 2024-05-29 - works with python 3.12
	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()
