Warning: Can not open [/home/conf/public_html/cgi-bin/show_python.log]. Ignore
No title
Download script from show_pictures.py
Related files:
#!C:\Users\tkami\AppData\Local\Programs\Python\Python311\python.exe
##!/usr/bin/python3
# https://aiacademy.jp/media/?p=1804
import os
import sys
import re
import glob
from PIL import Image
from tklib.tkapplication_cgi import tkApplication_CGI
from tklib.tkutils import pint, pfloat, getarg, getintarg, getfloatarg, split_file_path
#debug = True
debug = False
infile = ""
web_root = "/home/conf/public_html"
#web_root = "D:\\tkProg\\tkProg.main\\tkprog_COE\\html"
url_root = ""
img_w = 480
app = tkApplication_CGI()
#=============================
# Treat argments
#=============================
def usage(app = None):
print("")
print("Usage:")
print(" (a) python {} infile")
def updatevars():
global infile
infile = getarg(1, infile)
def execute():
global infile
global web_root
app.init_html()
logfile = app.replace_path(app.script_fullpath, template = ["{dirname}", "{filebody}.log"])
# print("")
# print(f"Open logfile [{logfile}]")
try:
app.redirect(targets = ["stdout", logfile], mode = 'w')
except:
print(f"Warning: Can not write to log file [{logfile}]
")
pass
if debug:
print(f"Show pictures from {infile}
")
path = app.getfirst('path', None)
if path is None:
path = infile
if path is None:
print("Error: The argument path must be given
")
app.end_html()
app.terminate()
str = app.validate_path(path, 'upper_dir|invalid_char|top_dir')
if str != '':
print("
")
print(f"{str}
")
app.end_html()
app.terminate()
infile = os.path.join(web_root, path, "*.png")
files = glob.glob(infile)
if len(files) == 0:
infile = os.path.join(web_root, path, "*.PNG")
files = glob.glob(infile)
if debug:
print(f"path: {path}
")
print(f"infile: {infile}
")
files = glob.glob(infile)
print(f"# of files: {len(files)}
")
if len(infile) == 0:
print("
")
print(f"Error: Can not find {infile}
")
app.end_html()
app.terminate()
outfiles = {}
for i in range(len(files)):
f = files[i]
if debug:
print(f"f: {f}
")
dirname, basename, filebody, ext = split_file_path(f)
m = re.search(r"(\d+)$", filebody)
if m:
index = pint(m.groups()[0])
else:
# index = i + 1
# print(f"filebody: {filebody} index: {index}
")
continue
ext = ext.lower()
inpath = os.path.join(dirname, f"{index:02}{ext}")
outpath = os.path.join(dirname, f"{index:02}-small{ext}")
if debug:
print(f"dirname: {dirname}
")
if '-small.png' in inpath:
continue
if not os.path.exists(inpath):
if debug:
print(f"Rename in line 125:
")
print(f" {f}
")
print(f" to {inpath}
")
try:
os.rename(f, inpath)
except:
print(f"Error: Can not rename {f} to {inpath}
")
print(f"Checck permission
")
if os.path.exists(outpath):
if debug:
print(f"{outpath} exists. Skip.
")
pass
else:
if debug:
print(f"Converting in line 141:")
print(f" {inpath}")
print(f" to {outpath}
")
img = Image.open(inpath)
height = int(img.height * img_w / img.width)
img_resized = img.resize((img_w, height))
try:
img_resized.save(outpath, quality = 90)
except:
print(f"Error: Can not convert {inpath} to {outpath}
")
print(f"Checck permission
")
outfiles[inpath] = 1
outfiles = sorted(outfiles.keys())
path = path.replace('\\', '/')
for f in outfiles:
dirname, basename, filebody, ext = split_file_path(f)
inurl = f"{url_root}/{path}/{filebody}{ext}"
outurl = f"{url_root}/{path}/{filebody}-small{ext}"
# print(f'{inurl} : {outurl}')
print(f'
')
app.end_html()
def main():
updatevars()
execute()
app.terminate("", pause = False)
if __name__ == "__main__":
main()