Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lossless PNGs optimization upon their appearance in "report" and its subdirectories #252

Merged
merged 10 commits into from
Apr 2, 2024

Conversation

montehurd
Copy link
Collaborator

@montehurd montehurd commented Mar 19, 2024

Reduces PNG file sizes by roughly 30%

Monitors "report" and its subdirectories - isn't otherwise coupled with Pixel code

Quick enough that it doesn't appear to interfere with the BackstopJS image comparison process - running it locally I haven't noticed any odd breakage

Optipng performs the optimization, "-o2" level seems a good balance between speed and file size reduction

Restarts itself if the process ever goes down


EDIT: it's closer to 20%

This calculate the average png size reduction from the pixel-png-optimizer-1 container logs:

docker logs pixel-png-optimizer-1 2>&1 | grep "reduction" | sed 's/.= (.)%/\1/' | sort -nr | awk '{sum+=$1; count++} END {if (count > 0) printf "Average: %.2f\n", sum/count; else print "No matching lines found."}'

@montehurd
Copy link
Collaborator Author

Peter H and I could smoke test this on the Horizon box first if folks prefer that

@montehurd
Copy link
Collaborator Author

I suppose I could also move the three new files into an "optimizePNG" folder 🤔

optimizePNG.sh Outdated

optipng -silent -fix -o2 "$FILE"

exiftool -quiet -overwrite_original_in_place -Software="$OPTIMIZATION_TAG" "$FILE"
Copy link
Collaborator Author

@montehurd montehurd Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lock file prevents this line from triggering another inotifywait create event

(exiftool is used to write "OptimizedWithOptiPNG" to the image's "Software" metadata so there’s an easy way to check if an image has already been optimized, see line 30)

@montehurd
Copy link
Collaborator Author

An alternative approach to monitoring directories for the appearance of pngs would be to run the optimize_png func periodically on every png recursively found in "report" and its subdirs (optimize_png checks metadata to see if an image was already optimized so it should be safe to call more than once on a given image), but I suspect that approach would spike resource usage pretty hard while it churned through unoptimized pngs. We may still want such a func we could call once to optimize all images from past tests. I can add that pretty easily if needed

@montehurd montehurd changed the title Add container for visually lossless PNGs optimization upon their appearance in "report" and its subdirectories Lossless PNGs optimization upon their appearance in "report" and its subdirectories Mar 19, 2024
@jdlrobson jdlrobson merged commit 171abc7 into wikimedia:main Apr 2, 2024
1 check passed
@montehurd montehurd deleted the optimize-png branch May 4, 2024 04:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants