More complex Python script

This commit is contained in:
DrMint 2022-02-21 01:45:15 +01:00
parent eea09925e0
commit 6ca096c409
4 changed files with 61 additions and 30 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
public
!.gitkeep
cwebp
magick

View File

@ -1,4 +1,2 @@
wget https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-1.2.2-rc1-linux-x86-64.tar.gz
tar -xf *.tar.gz
mv libwebp-*/bin/cwebp .
rm -r libwebp-*
wget https://download.imagemagick.org/ImageMagick/download/binaries/magick
chmod +x magick

58
run_prepare_img.py Normal file
View File

@ -0,0 +1,58 @@
import os
import subprocess
exportFolder = "public"
sourceFolder = "../strapi.accords-library.com/public/uploads/"
method = "6"
presets = {
"small": {
"maxSize": "512",
"quality": "60",
"format": "webp"
},
"medium": {
"maxSize": "1024",
"quality": "75",
"format": "webp"
},
"large": {
"maxSize": "2048",
"quality": "80",
"format": "webp"
},
"og": {
"maxSize": "512",
"quality": "60",
"format": "jpg"
}
}
print("Try create main folder:", exportFolder)
if not os.path.isdir(exportFolder): os.mkdir(exportFolder)
for preset in presets:
tmpFolder = exportFolder + "/" + preset
print("Try create preset folder:", tmpFolder)
if not os.path.isdir(tmpFolder): os.mkdir(tmpFolder)
files = os.listdir(sourceFolder)
files = [file for file in files if not file.startswith("thumbnail_") and not file == ".gitkeep"]
for index, file in enumerate(files):
fName, _ = os.path.splitext(file)
for presetName in presets:
preset = presets[presetName]
exportFile = exportFolder + "/" + presetName + "/" + fName + "." + preset["format"]
if not os.path.isfile(exportFile):
command = []
command += ["./magick"]
command += [sourceFolder + file]
command += ["-thumbnail " + preset["maxSize"] + "x" + preset["maxSize"] + "\>"]
command += ["-quality " + preset["quality"]]
if preset["format"] == "webp": command += ["-define webp:alpha-quality=" + preset["quality"]]
if preset["format"] == "webp": command += ["-define webp:method=" + method]
command += [exportFile]
command = " ".join(command)
subprocess.call(command, shell=True)
print("DONE (" + str(index) + "/" + str(len(files)) + ") -", presetName, "-", fName)

View File

@ -1,25 +0,0 @@
mkdir public
mkdir public/small
mkdir public/medium
mkdir public/large
echo "Working on small images"
for f in ../strapi.accords-library.com/public/uploads/*; do
filename=$(basename "$f")
./cwebp -short -m 6 -q 60 -alpha_q 60 -mt -resize 0 512 $f -o public/small/${filename%.*}.webp
done
echo "Working on medium images"
for f in ../strapi.accords-library.com/public/uploads/*; do
filename=$(basename "$f")
./cwebp -short -m 6 -q 75 -alpha_q 75 -mt -resize 0 1024 $f -o public/medium/${filename%.*}.webp
done
echo "Working on large images"
for f in ../strapi.accords-library.com/public/uploads/*; do
filename=$(basename "$f")
./cwebp -short -m 6 -q 80 -alpha_q 80 -mt -resize 0 2048 $f -o public/large/${filename%.*}.webp
done