Replies: 13 comments
-
If that string is not there, then libjxl will not be able to reconstruct the JPEG. |
Beta Was this translation helpful? Give feedback.
-
No, I get that, it’s not the point. Can I rely on the exact wording staying the same? It’s not a number or a boolean but an informal phrase, so I’m a little unsure as to whether I can reliably match against it (in a Bash function). BTW, what I’m trying to do is to automatically convert to JPEG if it can be reconstructed, or to PNG otherwise — so I always choose the smallest lossless option. Maybe there is a more straightforward way to do it? |
Beta Was this translation helpful? Give feedback.
-
Checking for that string should work. |
Beta Was this translation helpful? Give feedback.
-
So it won’t change in future versions? |
Beta Was this translation helpful? Give feedback.
-
I was reluctant to rely on If it's any use to you: Perhaps it would be useful to add an |
Beta Was this translation helpful? Give feedback.
-
Somebody on Reddit suggested an equivalent Bash line, which is simpler than mixing in Pyton: head -c 64 test.jxl | grep -ao "jbrd" | wc -l Or just: if head -c 64 test.jxl | grep -qa "jbrd"; then ... But thanks for the suggestion!
+++ |
Beta Was this translation helpful? Give feedback.
-
Here’s the full solution in case anyone else finds it useful: error() { echo -e "\033[1;31mError:\033[0m $@" 1>&2; }
jxl()
{
if [[ $# -ne 1 ]]; then
error "Wrong number of arguments!"
return 1
elif [[ ! -e "$1" ]]; then
error "$1 doesn’t exist!"
return 1
fi
local type=$(file -b --mime-type "$1")
local out="${1%.*}.jxl"
if [[ $type == "image/png" ]]; then
cjxl -q 100 "$1" "$out"
elif [[ $type == "image/jpeg" ]]; then
cjxl --lossless_jpeg=1 "$1" "$out"
else
error "$1 is not PNG or JPEG!"
return 1
fi
}
unjxl()
{
if [[ $# -ne 1 ]]; then
error "Wrong number of arguments!"
return 1
elif [[ $(file -b --mime-type "$1") != "image/jxl" ]]; then
error "$1 is not JPEG XL!"
return 1
fi
if head -c 64 "$1" | grep -qa "jbrd"; then
local ext="jpg"
else
local ext="png"
fi
djxl "$1" "${1%.*}.$ext"
} |
Beta Was this translation helpful? Give feedback.
-
That's definitely easier! But bear in mind the It would also give a false positive if the file just coincidentally contained the sequence of bytes If the odd false positive/negative isn't a disaster, grepping for |
Beta Was this translation helpful? Give feedback.
-
Damn, then it’s not good enough... |
Beta Was this translation helpful? Give feedback.
-
I guess the only sane solution would be this:
So could someone please add an |
Beta Was this translation helpful? Give feedback.
-
BTW, here’s an alternative approach that relies on jxl_jpeg='^JPEG XL file format container \(ISO/IEC 18181-2\)
JPEG XL image, [0-9]+x[0-9]+, \(possibly\) lossless, [0-9]+-bit RGB
Color space: RGB, D65, sRGB primaries, sRGB transfer function, rendering intent: Relative
JPEG bitstream reconstruction data available$'
jxl_png='^JPEG XL image, [0-9]+x[0-9]+, \(possibly\) lossless, [0-9]+-bit RGB\+Alpha
Color space: RGB, D65, sRGB primaries, sRGB transfer function, rendering intent: Relative$'
unjxl()
{
...
local info=$(jxlinfo "$1")
if [[ "$info" =~ $jxl_jpeg ]]; then
local ext="jpg"
elif [[ "$info" =~ $jxl_png ]]; then
local ext="png"
else
error "Unknown jxlinfo format!"
return 1
fi
...
} |
Beta Was this translation helpful? Give feedback.
-
One option is to call |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
When I inspect a transcoded image with
jxlinfo
, it saysJPEG bitstream reconstruction data available
. Can I rely on that exact string always being there and match against it? If not, it there a more formal way to check whether a JXL is a transcoded JPEG?Beta Was this translation helpful? Give feedback.
All reactions