I came across this thread as I needed JPEG quality estimation myself. After some tests with the Python port of the ImageMagick heuristic that @eddygeek posted here earlier, I ultimately went for a slightly different approach based on least squares matching of an image's quantization tables with the "standard" JPEG tables. See code here:
https://github.com/KBNLresearch/jpeg-quality-demo/blob/main/jpegquality-lsm.py
This also reports a metric that characterizes how similar the image's quantization tables are to the "standard" tables, which is useful as a measure of confidence (in general the quality estimates will become less reliable as the quantization tables deviate more from the standard tables).
See below blog post for an in-depth discussion of the method, and some tests I did with it (including a comparison with the ImageMagick heuristic):
My tests showed this can give quite different results than the ImageMagick heuristic, but the quality estimates are very close (and mostly identical) to those provided by the FotoForensics service.
Note that the code code requires a recent (if I'm not mistaken 8.3 or more recent) version of Pillow, see the note at the end of the blog post.
For some additional context, this blog post covers some of the problems I ran into with the ImageMagick heuristic (and this also led me to create my alternative implementation).