Convert JPEG from progressive to baseline losslessly
Convert JPEG from progressive to baseline losslessly
I hope this is the correct section, forgive me if it's not.
I need to convert some progressive/interlaced JPEGs to baseline format. A simple "convert" or "convert -interlace none" does it, but I've been told «I'm fairly certain that convert is performing a conversion from the DCT domain into the real space domain and then just recompress as a non-progressive JPG. This will indeed lead to round-off errors causing a degradation in image quality». Can the conversion be lossless?
Background: the conversion is for them to be more easily processable by imagemagick itself (interlaced images take too much memory to convert); I've considered "safe" for cheap processing by IM only JPEGs with SOF tag "0x0 = Baseline DCT, Huffman coding" and I'm trying to convert all the others (which are all interlaced in some way according to "identify -verbose" as well, it seems). Even more background here, probably not interesting.
I need to convert some progressive/interlaced JPEGs to baseline format. A simple "convert" or "convert -interlace none" does it, but I've been told «I'm fairly certain that convert is performing a conversion from the DCT domain into the real space domain and then just recompress as a non-progressive JPG. This will indeed lead to round-off errors causing a degradation in image quality». Can the conversion be lossless?
Background: the conversion is for them to be more easily processable by imagemagick itself (interlaced images take too much memory to convert); I've considered "safe" for cheap processing by IM only JPEGs with SOF tag "0x0 = Baseline DCT, Huffman coding" and I'm trying to convert all the others (which are all interlaced in some way according to "identify -verbose" as well, it seems). Even more background here, probably not interesting.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert JPEG from progressive to baseline losslessly
As I understand it, IM will decompress and recompress for a convert from jpg to jpg, even if you are not changing interlacing or progressive modes. You would need to use some non-IM tools, if there are any that allow for mode changes without decompression and recompression. see http://www.imagemagick.org/Usage/formats/#jpg_lossless
If I have misunderstood or there are JPG experts who know otherwise, feel free to correct my comments.
If I have misunderstood or there are JPG experts who know otherwise, feel free to correct my comments.
Re: Convert JPEG from progressive to baseline losslessly
Use jpegtran. I remember that it can do so in a lossless way.
Re: Convert JPEG from progressive to baseline losslessly
Thank you, I didn't see anything in the docs but looks like a null transform works, likehenrywho wrote:Use jpegtran. I remember that it can do so in a lossless way.
Code: Select all
jpegtran in.jpg > out.jpg
Code: Select all
jpegtran -perfect in.jpg > out.jpg
- Dane Vandeputte
- Posts: 14
- Joined: 2012-07-01T18:26:53-07:00
- Authentication code: 13
- Location: Illinois, USA
Re: Convert JPEG from progressive to baseline losslessly
Yes, it has been lossless. The -perfect isn't necessary in this case, but it doesn't hurt. Also, if you want to keep all of the original metadata intact, you should use -copy all. If you want to make sure that the resulting files are as small as possible, you can use -optimize, although the processing time may increase a bit.Nemo_bis wrote:It "works" as in it produces a non-interlaced image from an interlaced one (and three times faster than convert), but I'm not able to say if this has been lossless. I can assume it was?
Digital image processing and photography enthusiast
Re: Convert JPEG from progressive to baseline losslessly
Thank you very much.Dane Vandeputte wrote:Yes, it has been lossless. The -perfect isn't necessary in this case, but it doesn't hurt. Also, if you want to keep all of the original metadata intact, you should use -copy all. If you want to make sure that the resulting files are as small as possible, you can use -optimize, although the processing time may increase a bit.Nemo_bis wrote:It "works" as in it produces a non-interlaced image from an interlaced one (and three times faster than convert), but I'm not able to say if this has been lossless. I can assume it was?
- Dane Vandeputte
- Posts: 14
- Joined: 2012-07-01T18:26:53-07:00
- Authentication code: 13
- Location: Illinois, USA
Re: Convert JPEG from progressive to baseline losslessly
You're most welcome.Nemo_bis wrote:Thank you very much.
Digital image processing and photography enthusiast
Re: Convert JPEG from progressive to baseline losslessly
Just found this post - great! I and several other users of Sony's New Walkman NW-A45 have discovered that this item can ONLY display artwork from Baseline JPEGs. This means that we now have to convert ALL (or most) of our artwork in +1000 folders
So now the 1 Million $ question: is there any way to make *jpegtran* convert all files in all subfolders? E.g. all "folder.jpg" files in all sub-directories to /home/user/Music/ ?
NB. I am just a normal user of Linux (Kubuntu) and not so skilled when it comes to Konsole and command line interface - so any detailed help is greatly appreciated!
(If jpegtran cannot make a batch conversion in all subfolders, it will be VERY time consuming to do it manually. The NW cannot even use "folder.jpg" files in the folder, you will have to embed the artwork in the FLAC files - but that is another story! Conversion first ).
So now the 1 Million $ question: is there any way to make *jpegtran* convert all files in all subfolders? E.g. all "folder.jpg" files in all sub-directories to /home/user/Music/ ?
NB. I am just a normal user of Linux (Kubuntu) and not so skilled when it comes to Konsole and command line interface - so any detailed help is greatly appreciated!
(If jpegtran cannot make a batch conversion in all subfolders, it will be VERY time consuming to do it manually. The NW cannot even use "folder.jpg" files in the folder, you will have to embed the artwork in the FLAC files - but that is another story! Conversion first ).
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert JPEG from progressive to baseline losslessly
If jpegtran cannot do it in a batch, then you should be able to do a simple for loop over each image in a folder and call jpegtran in the loop.
Re: Convert JPEG from progressive to baseline losslessly
My structure is like this: in my main music folder = /home/user/Music/ I have Artist folders (a lot) and in each Artist folder there will be one or many Album folders. Each Album folder contains one "folder.jpg" artwork file which needs to be converted to Baseline JPEG.
So if I could set the current folder to /home/user/Music/ - and then make a batch file or "script" which would run through ALL sub-folders and convert all "folder.jpg" files into baseline - then I would save A LOT of time. (yes all my artwork files are exactly named "folder.jpg").
Some 30 years ago I was taught Pascal, but haven't used it since. At that time PCs were just emerging - with the DOS OS! So I understand it when you write "a ... for loop" - but that's not enough.
It would be awesome if you could help explain the *exact* syntax I should use to reach my goal. In those bygone days I could write a text batch file (*.BAT) in DOS/Windows and do some automated stuff.
Back to the present day: should I just open a console and navigate to /home/user/Music/ - and from there issue one or more command lines? How exactly should the line or lines look like (syntax)?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert JPEG from progressive to baseline losslessly
Sorry, one of the Windows users will have to help you. I am on a Mac which uses Unix and the scripting is very different. But you can probably search google or stack overflow and get some information about bat file for loop scripting syntax and examples.
Re: Convert JPEG from progressive to baseline losslessly
How can a Windows user help? I am running Linux (Kubuntu) and to my (limited) knowledge Linux and Unix are much closer to each other?
I get the sense that I just need perhaps ONE line in my console (Konsole in Kubuntu) involving jpegtran and a for...loop - and then all my "folder.jpg" files will be converted!
So close, and yet so far away. As mentioned I am not a real programmer - and just glancing at stackoverflow.com it seems like I will be totally out of my league. But OK, I can give it a try.
Anyway thanks for your replies so far.
Really though, if I could just get this ONE line in my console right, then in a few seconds all my graphic files would be converted.
And THEN I could return to the SONY user group and help so many people there!
I get the sense that I just need perhaps ONE line in my console (Konsole in Kubuntu) involving jpegtran and a for...loop - and then all my "folder.jpg" files will be converted!
So close, and yet so far away. As mentioned I am not a real programmer - and just glancing at stackoverflow.com it seems like I will be totally out of my league. But OK, I can give it a try.
Anyway thanks for your replies so far.
Really though, if I could just get this ONE line in my console right, then in a few seconds all my graphic files would be converted.
And THEN I could return to the SONY user group and help so many people there!
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert JPEG from progressive to baseline losslessly
Sorry, I misunderstood. You talked about DOS and Windows batch, so I assumed you were on Windows.
Given one directory for images and no spaces in your file names, you can do:
Sorry I do not know jpegtran. So you will have to test on one image regarding arguments and naming. My command sends the output to the same name as the input, so that it should be overwritten. If you do not want that, then you will have to provide a path to some existing output directory as a path in front of -o $img such as -o path_to_output_directory/$img.
To loop over all subdirectories, you would need to gather the list of directory names and do a second loop outside the first loop and the put the path to each directory in front of the input image $img at the end of the jpegtran command. Or change the cd to each new subdirectory from the loop.
See https://linux.die.net/man/1/jpegtran
and
https://www.tutorialspoint.com/unix/for-loop.htm
ls is a unix command to get a simple list of filenames from the current directory.
See
https://www.tecmint.com/15-basic-ls-com ... -in-linux/
Given one directory for images and no spaces in your file names, you can do:
Code: Select all
cd to directory with images
list=$(ls)
for img in $list; do
jpegtran -o $img $img
done
To loop over all subdirectories, you would need to gather the list of directory names and do a second loop outside the first loop and the put the path to each directory in front of the input image $img at the end of the jpegtran command. Or change the cd to each new subdirectory from the loop.
See https://linux.die.net/man/1/jpegtran
and
https://www.tutorialspoint.com/unix/for-loop.htm
ls is a unix command to get a simple list of filenames from the current directory.
See
https://www.tecmint.com/15-basic-ls-com ... -in-linux/
Re: Convert JPEG from progressive to baseline losslessly
Thanks a lot, I believe you helped my a great step forward. I will work along the lines you have indicated...
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert JPEG from progressive to baseline losslessly
P.S.
ls -d *
lists only the subdirectories in a given directory.
ls -d *
lists only the subdirectories in a given directory.