aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Woods2023-02-22 01:09:37 -0500
committerBrian Woods2023-02-22 01:13:11 -0500
commit8fe5bf1295799a63aca7a16469771e257e5cfc49 (patch)
treea3ab4aeb3a682c5039ce15a880fe0dae97ed4d8b
parent85f977cabc3f4bb976cec15bf2c2a2674542f424 (diff)
Add webp scripts and makefiles
Add scripts for testing compression quality and converting collections of pngs to webps.
-rw-r--r--webp/Makefile.double36
-rw-r--r--webp/Makefile.single25
-rw-r--r--webp/README.md13
-rwxr-xr-xwebp/lossless_z_gen_graph.m36
-rwxr-xr-xwebp/lossless_z_gen_images.sh20
-rwxr-xr-xwebp/lossy_nearlossless_gen_graph.m44
-rwxr-xr-xwebp/lossy_nearlossless_gen_images.sh21
7 files changed, 195 insertions, 0 deletions
diff --git a/webp/Makefile.double b/webp/Makefile.double
new file mode 100644
index 0000000..dec381c
--- /dev/null
+++ b/webp/Makefile.double
@@ -0,0 +1,36 @@
+# SPDX-FileCopyrightText: 2023 Brian Woods
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+SRC_DIR=src
+HIGH_OUT_DIR=high_quality
+LOW_OUT_DIR=low_quality
+
+HIGH_WEBP_OPT=-near_lossless 60
+LOW_WEBP_OPT=-af -pass 6 -psnr 38
+
+SRC=$(shell find src/ -type f -name '*.png')
+HIGH_OUT=$(patsubst ${SRC_DIR}/%.png, ${HIGH_OUT_DIR}/%.webp, $(SRC))
+LOW_OUT=$(patsubst ${SRC_DIR}/%.png, ${LOW_OUT_DIR}/%.webp, $(SRC))
+
+.PHONY: all clean high low
+.SECONDARY: main-build-high main-build-low
+
+all: high low
+high: pre-build-high main-build-high
+low: pre-build-low main-build-low
+
+pre-build-high:
+ ( cd ${SRC_DIR}; find ./ -mindepth 1 -type d ) | xargs -I{} mkdir -p "${HIGH_OUT_DIR}/{}"
+pre-build-low:
+ ( cd ${SRC_DIR}; find ./ -mindepth 1 -type d ) | xargs -I{} mkdir -p "${LOW_OUT_DIR}/{}"
+
+main-build-high: ${HIGH_OUT}
+main-build-low: ${LOW_OUT}
+
+${HIGH_OUT_DIR}/%.webp: ${SRC_DIR}/%.png
+ cwebp ${HIGH_WEBP_OPT} "$<" -o "$@"
+${LOW_OUT_DIR}/%.webp: ${SRC_DIR}/%.png
+ cwebp ${LOW_WEBP_OPT} "$<" -o "$@"
+
+clean:
+ rm -rf ${HIGH_OUT_DIR} ${LOW_OUT_DIR}
diff --git a/webp/Makefile.single b/webp/Makefile.single
new file mode 100644
index 0000000..c84a4b8
--- /dev/null
+++ b/webp/Makefile.single
@@ -0,0 +1,25 @@
+# SPDX-FileCopyrightText: 2023 Brian Woods
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+SRC_DIR=src
+OUT_DIR=high_quality
+WEBP_OPT=-near_lossless 60
+
+SRC=$(shell find src/ -type f -name '*.png')
+OUT=$(patsubst ${SRC_DIR}/%.png, ${OUT_DIR}/%.webp, $(SRC))
+
+.PHONY: all clean
+.SECONDARY: main-build
+
+all: pre-build main-build
+
+pre-build:
+ ( cd ${SRC_DIR}; find ./ -mindepth 1 -type d ) | xargs -I{} mkdir -p "${OUT_DIR}/{}"
+
+main-build: ${OUT}
+
+${OUT_DIR}/%.webp: ${SRC_DIR}/%.png
+ cwebp ${WEBP_OPT} "$<" -o "$@"
+
+clean:
+ rm -rf ${OUT_DIR}
diff --git a/webp/README.md b/webp/README.md
new file mode 100644
index 0000000..a256710
--- /dev/null
+++ b/webp/README.md
@@ -0,0 +1,13 @@
+# webp scripts
+## lossless_z_gen_\* scripts
+Create images with various compression levels and see how that effects
+image size. Has both a bash script to generate the images and an GNU
+octave script to generate the graph.
+## lossy_nearlossless_gen_\* scipts
+Much like the one but varies the nearlossless preprocessing level. This
+just shows the file size but you need to look at the files because since
+it's lossy there will be visual compression artifacts.
+## Makefiles
+Just make files to convert directories of on going work PNGs to webps.
+There's a makefile for a single output and dual outputs for both a high
+and low quality compression settings.
diff --git a/webp/lossless_z_gen_graph.m b/webp/lossless_z_gen_graph.m
new file mode 100755
index 0000000..d363c49
--- /dev/null
+++ b/webp/lossless_z_gen_graph.m
@@ -0,0 +1,36 @@
+#!/usr/bin/octave
+% SPDX-FileCopyrightText: 2023 Brian Woods
+% SPDX-License-Identifier: GPL-2.0-or-later
+
+files = {'lossless_z_images/PNG_01-z_VAR.webp', ...
+ 'lossless_z_images/PNG_02-z_VAR.webp', ...
+ 'lossless_z_images/PNG_03-z_VAR.webp', ...
+ 'lossless_z_images/PNG_04-z_VAR.webp'};
+var_values = 0:1:9;
+files_len = length(files);
+var_values_len = length(var_values);
+results = zeros(files_len, var_values_len);
+
+for i = 1:files_len
+ for j = 1:var_values_len;
+ filepath = strrep(files{i}, 'VAR', num2str(var_values(j), "%d"));
+ [file, ~, ~] = stat(filepath);
+ results(i,j) = file.size;
+ end
+end
+
+% normalize
+for i = 1:files_len
+ results(i,:) = results(i,:)/max(results(i,:));
+end
+
+f = figure('visible','off');
+plot(var_values, results(1,:),"b-");
+hold on;
+plot(var_values, results(2,:), "g-");
+plot(var_values, results(3,:), "r-");
+plot(var_values, results(4,:), "c-");
+xlabel ("z compression level");
+ylabel ("normalized file size");
+legend ("PNG 01", "PNG 02", "PNG 03", "PNG 04");
+print('lossless_z_graph.png');
diff --git a/webp/lossless_z_gen_images.sh b/webp/lossless_z_gen_images.sh
new file mode 100755
index 0000000..d6e3789
--- /dev/null
+++ b/webp/lossless_z_gen_images.sh
@@ -0,0 +1,20 @@
+#!/bin/bash
+# SPDX-FileCopyrightText: 2023 Brian Woods
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+OUTDIR=lossless_z_images
+
+IN=" $( ls orig/*.png )"
+
+mkdir -p "$OUTDIR"
+for INFILE in $IN ; do
+ # z range 0 to 9
+ for Z in {0..9} ; do
+ cwebp \
+ -lossless \
+ -z $Z \
+ -resize 1920 1080 \
+ "$INFILE" \
+ -o "$OUTDIR/$( basename -s .png $INFILE )-z_$Z.webp"
+ done
+done
diff --git a/webp/lossy_nearlossless_gen_graph.m b/webp/lossy_nearlossless_gen_graph.m
new file mode 100755
index 0000000..cdc33a6
--- /dev/null
+++ b/webp/lossy_nearlossless_gen_graph.m
@@ -0,0 +1,44 @@
+#!/usr/bin/octave
+% SPDX-FileCopyrightText: 2023 Brian Woods
+% SPDX-License-Identifier: GPL-2.0-or-later
+
+graph_file = 'lossy_nearlossless_graph.png';
+img_dir = 'lossy_nearlossless_images/';
+varname = "nl";
+var_values = {'000', '010', '020', '030', ...
+ '040', '050', '060', '070', ...
+ '080', '090', '100'};
+
+files = {strrep(strcat(img_dir, 'PNG_01-VARN_VAR.webp'), 'VARN', varname), ...
+ strrep(strcat(img_dir, 'PNG_02-VARN_VAR.webp'), 'VARN', varname), ...
+ strrep(strcat(img_dir, 'PNG_03-VARN_VAR.webp'), 'VARN', varname), ...
+ strrep(strcat(img_dir, 'PNG_04-VARN_VAR.webp'), 'VARN', varname)};
+files_len = length(files);
+var_values_len = length(var_values);
+results = zeros(files_len, var_values_len);
+
+for i = 1:files_len
+ for j = 1:var_values_len;
+ filepath = strrep(files{i}, 'VAR', var_values{j});
+ [file, ~, ~] = stat(filepath);
+ results(i,j) = file.size;
+ end
+end
+
+% normalize
+for i = 1:files_len
+ results(i,:) = results(i,:)/max(results(i,:));
+end
+
+var_values_idx = str2double(var_values);
+f = figure('visible','off');
+plot(var_values_idx, results(1,:),"b-");
+hold on;
+plot(var_values_idx, results(2,:), "g-");
+plot(var_values_idx, results(3,:), "r-");
+plot(var_values_idx, results(4,:), "c-");
+xlabel ("near lossless preprocessing (0 is more visual compression)");
+ylabel ("normalized file size");
+h = legend ("PNG 01", "PNG 02", "PNG 03", "PNG 04");
+legend(h, "location", "northeastoutside");
+print(graph_file);
diff --git a/webp/lossy_nearlossless_gen_images.sh b/webp/lossy_nearlossless_gen_images.sh
new file mode 100755
index 0000000..4e65a76
--- /dev/null
+++ b/webp/lossy_nearlossless_gen_images.sh
@@ -0,0 +1,21 @@
+#!/bin/bash
+# SPDX-FileCopyrightText: 2023 Brian Woods
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+OUTDIR=lossy_nearlossless_images
+
+IN=" $( ls orig/*.png )"
+
+mkdir -p "$OUTDIR"
+for INFILE in $IN ; do
+ for Z in {0..10} ; do
+ NUM=$(( Z * 10 ))
+ NUM_STR=$( printf "%03d\n" "$NUM" )
+ cwebp \
+ -mt \
+ -near_lossless $NUM \
+ -resize 1920 1080 \
+ "$INFILE" \
+ -o "$OUTDIR/$( basename -s .png $INFILE )-nl_$NUM_STR.webp"
+ done
+done