Convert JPEG from progressive to baseline losslessly

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
Post Reply
Nemo_bis
Posts: 11
Joined: 2012-06-16T01:13:18-07:00
Authentication code: 13

Convert JPEG from progressive to baseline losslessly

Post by Nemo_bis »

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.
User avatar
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

Post by fmw42 »

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.
henrywho
Posts: 188
Joined: 2011-08-17T06:46:40-07:00
Authentication code: 8675308

Re: Convert JPEG from progressive to baseline losslessly

Post by henrywho »

Use jpegtran. I remember that it can do so in a lossless way.
Nemo_bis
Posts: 11
Joined: 2012-06-16T01:13:18-07:00
Authentication code: 13

Re: Convert JPEG from progressive to baseline losslessly

Post by Nemo_bis »

henrywho wrote:Use jpegtran. I remember that it can do so in a lossless way.
Thank you, I didn't see anything in the docs but looks like a null transform works, like

Code: Select all

jpegtran in.jpg > out.jpg
or, to be safe:

Code: Select all

jpegtran -perfect in.jpg > out.jpg
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?
User avatar
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

Post by Dane Vandeputte »

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?
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.
Digital image processing and photography enthusiast :)
Nemo_bis
Posts: 11
Joined: 2012-06-16T01:13:18-07:00
Authentication code: 13

Re: Convert JPEG from progressive to baseline losslessly

Post by Nemo_bis »

Dane Vandeputte wrote:
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?
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.
Thank you very much.
User avatar
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

Post by Dane Vandeputte »

Nemo_bis wrote:Thank you very much.
You're most welcome. :)
Digital image processing and photography enthusiast :)
Pal
Posts: 4
Joined: 2018-12-05T14:51:02-07:00
Authentication code: 1152

Re: Convert JPEG from progressive to baseline losslessly

Post by Pal »

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 ;-) ).
User avatar
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

Post by fmw42 »

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.
Pal
Posts: 4
Joined: 2018-12-05T14:51:02-07:00
Authentication code: 1152

Re: Convert JPEG from progressive to baseline losslessly

Post by Pal »

fmw42 wrote: 2018-12-06T14:41:01-07:00 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.
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)?
User avatar
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

Post by fmw42 »

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.
Pal
Posts: 4
Joined: 2018-12-05T14:51:02-07:00
Authentication code: 1152

Re: Convert JPEG from progressive to baseline losslessly

Post by Pal »

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!
User avatar
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

Post by fmw42 »

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:

Code: Select all

cd to directory with images
list=$(ls)
for img in $list; do
jpegtran -o $img $img
done
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/
Pal
Posts: 4
Joined: 2018-12-05T14:51:02-07:00
Authentication code: 1152

Re: Convert JPEG from progressive to baseline losslessly

Post by Pal »

Thanks a lot, I believe you helped my a great step forward. I will work along the lines you have indicated...
User avatar
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

Post by fmw42 »

P.S.

ls -d *

lists only the subdirectories in a given directory.
Post Reply