Best compression code

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
acndiu
Posts: 2
Joined: 2019-11-07T03:49:17-07:00
Authentication code: 1152

Best compression code

Post by acndiu » 2019-11-07T03:59:14-07:00

Hi guys, I'm using IM on windows and I have a bunch of .JPG files from various people, not mine and i want to compress these, as they're taking too much space, I've played around with some commands as such :'mogrify -strip -quality 75% *.jpg','convert -strip -interlace Plane -gaussian-blur 0.05 -quality 85% source.jpg result.jpg' sourced online but they don't work and somehow it takes up more space instead.
I also tried the -define jpeg:size=xxxx method but that's not very good as some of my .jpg files are around 700KB or so, and there's deterioration of quality. So i'm looking for one that compresses equally by a certain percentage for all my images be it 10KB or 600KB,
I also came across the -compress method, but there's an error always ,so how can i use the - compress command with mogrify?

snibgo
Posts: 12300
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Best compression code

Post by snibgo » 2019-11-07T04:58:47-07:00

JPEG files are always compressed by the JPEG method, so "-compress" is not relevant.
acndiu wrote:So i'm looking for one that compresses equally by a certain percentage for all my images ...
IM has no option for that. You could do it in a script, operating on each file individually, using "-define jpeg:extent=value" where value is calculated in the script as 90% of the input filesize, or whatever you want.
snibgo's IM pages: im.snibgo.com

acndiu
Posts: 2
Joined: 2019-11-07T03:49:17-07:00
Authentication code: 1152

Re: Best compression code

Post by acndiu » 2019-11-07T19:41:23-07:00

Hi, thanks for the insightful reply, I've tried to do what you've advised me to do so, so far i'm using: "for %i in (file location) DO identify -format "%b" %i
" this code works in getting the file size of all files in Bytes, but how do i store the subsequent value into a variable so that i can compute it afterwards?

User avatar
fmw42
Posts: 25664
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Best compression code

Post by fmw42 » 2019-11-07T19:46:59-07:00

That is more of a scripting issue and not really one of ImageMagick. But perhaps see https://imagemagick.org/Usage/windows/. Sorry, I am not a Windows user. You could try doing a Google search for Windows variables.

User avatar
whugemann
Posts: 294
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Best compression code

Post by whugemann » 2019-11-11T07:00:34-07:00

Although I doubt that this approach makes much sense, here is the Windows batch code that basically does the trick:

Code: Select all

SETLOCAL EnableDelayedExpansion
FOR %%i in (*.jpg) DO (
   FOR /F %%j IN ('identify -format %%b %%i') DO SET S=%%j
      SET S=!S:KB=!
      SET /A S=9*!S!/10
      magick %%i -define jpeg:extent=!S!KB %%~dpni_s%%~xi
)
The above code assumes that all JPEGs are smaller than 1MB, i.e. identify will answer with something like 732KB. Only in this case

Code: Select all

SET S=!S:KB=!
will strip off the 'KB' and the integer math

Code: Select all

SET /A S=9*!S!/10
will yield something meaningful.
Wolfgang Hugemann

snibgo
Posts: 12300
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Best compression code

Post by snibgo » 2019-11-11T07:32:01-07:00

See also %b in https://www.imagemagick.org/script/escape.php to force results in bytes, not KB or MB etc:

Code: Select all

f:\web\im>%IMG7%magick tower.jpg -format %b info:
20.7979MB

f:\web\im>%IMG7%magick tower.jpg -precision 16 -format %b info:
20797901B
snibgo's IM pages: im.snibgo.com

User avatar
whugemann
Posts: 294
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Best compression code

Post by whugemann » 2019-11-11T08:21:19-07:00

Good suggestion, this will make the integer math robust.This would need:

Code: Select all

 SET S=!S:B=!
 SET /A S=9*!S!/1000
an one could leave the extent option at KB.
Wolfgang Hugemann

User avatar
fmw42
Posts: 25664
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Best compression code

Post by fmw42 » 2019-11-11T09:47:33-07:00

You can use %B to get the file size in bytes as a numeral only so that you do not have to strip off the letter "B"

User avatar
whugemann
Posts: 294
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Best compression code

Post by whugemann » 2019-11-11T09:53:13-07:00

And as I see now: Then you also don't need the option -precision 16, as the result is delivered in bytes by default ;-)
Wolfgang Hugemann

snibgo
Posts: 12300
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Best compression code

Post by snibgo » 2019-11-11T09:55:49-07:00

Ah, yes, thanks, %B, much better:

Code: Select all

f:\web\im>%IMG7%magick tower.jpg -format %B info:
20797901
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 25664
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Best compression code

Post by fmw42 » 2019-11-11T10:21:57-07:00

Yes, I asked for that enhancement a while back as it was useful for scripting.

Post Reply