##The purpose of this script is to convert .tif files from two folders, (sketches and finished art) to png and create a gallery that is viewable.
##This saves time by letting me just throw my art pieces directly from Photoshop to these two folders and have it auto update the gallery.
import shutil
import sys
import os
from PIL import Image
from pathlib import Path
from sys import platform
from multiprocessing import Process
def slave_convert_process(infile): #Each image conversion is run in a different process. This is called by master_convert_process, infile refers to the filename of the tif file to be converted.
outfile = infile[:-3] + "png"
print("new filename : " + outfile)
im = Image.open(infile)
out = im.convert("RGB")
maxsize = (1024,1024)
out.thumbnail(maxsize, Image.ANTIALIAS)
out.save(outfile)
if __name__ == '__main__': #Needed for Windows. If this is not here, the whole program is run on each process when only slave_convert_process should.
if platform == "linux" or platform == "linux2":
os.chdir('/mnt/iis/gall')
if platform == "win32":
os.chdir('v:/gall')
os.chdir("art") #from gall directory -> art (contains Alpha and Release Folder).
tif_list_alpha = []
tif_list_release= []
class tif_image_name_and_date():
name = ""
date = ""
def __init__(self,name,date):
self.name=name
self.date=date
def get_tif_names(chdir_loc, tif_list):
os.chdir(chdir_loc)
#print(os.listdir())
for infile in os.listdir():
if(infile[-3:] == "tif"):
tif_list.append(tif_image_name_and_date(infile,os.path.getmtime(infile)))
os.chdir('..')
get_tif_names("Alpha", tif_list_alpha)
get_tif_names("Release", tif_list_release)
processes = []
print(tif_list_alpha[0].name)
def master_convert_process(chdir_loc, tif_list): #calls, slave_convert_process, A thread is made for each image to convert.
os.chdir(chdir_loc)
for infile in tif_list:
print(infile.name)
p = Process(target=slave_convert_process, args=(infile.name,))
p.start()
processes.append(p)
for p in processes:
p.join()
os.chdir('..')
master_convert_process("Alpha", tif_list_alpha)
master_convert_process("Release", tif_list_release)
def folder_move(str_group): #Move png files to the top level png folder.
os.chdir(str_group)
files = os.listdir()
for f in files:
if(f.endswith(".png")):
print(f, "evaluated to true")
os.chdir("..")
os.chdir("png")
try:
os.remove(f)
except OSError:
pass
os.chdir('..')
os.chdir(str_group)
p = Path(f).absolute()
parent_dir = p.parents[1]
p.rename(parent_dir / p.name)
os.chdir('..')
shutil.move(f, 'png')
os.chdir(str_group)
#png_list.append(f)
os.chdir('..')
folder_move("Alpha")
folder_move("Release")
tif_list_alpha.sort(key=lambda x: x.date, reverse=False) #Sort albums by their date modified.
tif_list_release.sort(key=lambda x: x.date, reverse=False)
png_list_alpha=[]
png_list_release=[]
for i in tif_list_alpha: #Final images are in png, not tif. Create a list of the generated png files, sorted by date.
png_list_alpha.append(i.name.replace('.tif', '.png'))
for i in tif_list_release:
png_list_release.append(i.name.replace('.tif', '.png'))
# print(os.listdir("./")) #Create html code for album generation below.
img_content_alpha = []
album_id = 'Alpha'
data_ngid_2 = 1000
data_ngid = 1
img_content_rel = []
album_id_rel = 'Release'
data_ngid_2_rel = 2000
data_ngid_rel = 2
intro = """<html>
<head>
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://unpkg.com/nanogallery2/dist/css/nanogallery2.min.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="https://unpkg.com/nanogallery2/dist/jquery.nanogallery2.min.js"></script>
<style>
.standard_text {
font-family: Cambria, "Hoefler Text", "Liberation Serif", Times, "Times New Roman", "serif";
color: #E8E6E3;
position:fixed;
left:10px;
bottom:0px;
height:30px;
width:100%;
}
</style>
</head>
<body style="background-color:#181A1B;">
<h3 style="color:#E8E6E3;">
Artsy Boi
</h3>
<div id="nanogallery2" // gallery configuration data-nanogallery2='{
"thumbnailWidth": "auto",
"thumbnailHeight": 300,
"itemsBaseURL": "https://3dslice.net/gall/art/png/",
"galleryTheme": {
"navigationBreadcrumb": { "background": "#000" }
},
"viewerTheme": {
"background": "#000"
}
}'>
<!-- content of the gallery -->\n"""
final_cont = """
</div>
<div class="standard_text">
Gallery made by <a href="https://3dslice.net/gall/generator8.py" style="color: #E8E6E3"> this script</a> and nanogallery2
</div>
"""
def img_content_gen(img_content, album_id, data_ngid_2, data_ngid, png_list):
for i in range(0, len(png_list)):
img_content.append('<a href="' + png_list[i] + '" data-ngid="' + str(data_ngid_2+i) + '" data-ngalbumid="' + str(data_ngid) + '" data-ngthumb="' + png_list[i] + '">' + str(i+1) + '</a>')
img_content_gen(img_content_alpha, album_id, data_ngid_2, data_ngid, png_list_alpha)
img_content_gen(img_content_rel, album_id_rel, data_ngid_2_rel, data_ngid_rel, png_list_release)
center_alpha = '<!-- ' + album_id + ' -->\n'
center_release = '<!-- ' + album_id_rel + ' -->\n'
title_alpha = ""
title_release = ""
img_content_str_alpha = ""
img_content_str_rel = ""
end = """
</body>
</html>"""
def center_final_gen(album_id, png_list, data_ngid, img_content, center, title):
title = '<a href="" data-ngkind="album" data-ngid="' + str(data_ngid) + '" data-ngthumb="' + str(png_list[len(png_list)-1]) + '">' + album_id + '</a>'
img_content.reverse()
img_content.insert(0, title)
center_final_gen(album_id, png_list_alpha, data_ngid, img_content_alpha, center_alpha, title_alpha)
center_final_gen(album_id_rel, png_list_release, data_ngid_rel, img_content_rel, center_release, title_release)
for i in range(0, len(img_content_alpha)):
img_content_str_alpha += img_content_alpha[i]+'\n'
for i in range(0, len(img_content_rel)):
img_content_str_rel += img_content_rel[i]+'\n'
final = intro + center_alpha + img_content_str_alpha + center_release + img_content_str_rel + final_cont + end
filename = "index_art.htm"
os.chdir('..')
myfile = open(filename, 'w')
myfile.write(final)
myfile.close()