Upsizing Undersized Images

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
arcking
Posts: 10
Joined: 2011-06-10T11:29:39-07:00
Authentication code: 8675308

Upsizing Undersized Images

Post by arcking »

Ideally, I'm looking for a way to upsize small images in a folder while leaving images that are larger alone.

Here's what I've come up with (batch file):

Code: Select all

%~d1
CD "%~p1"
MD upsized

FOR %%a in (*.jpg) DO (
	convert %%a	-resize "2400^<"  upsized\%%a
)

PAUSE
It successfully upsizes all of the small images, but also modifies all of the images that are already large enough (they stay the same resolution, but are compressed).

Thanks for your help!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Upsizing Undersized Images

Post by fmw42 »

see note about -quality for jpg images at http://www.imagemagick.org/script/comma ... r5#quality

Your images are probably getting decompressed and then recompressed if you are resaving them as jpg, so you lose quality. Also the quality may not be as high as you started unless you specify the -quality value you want.
arcking
Posts: 10
Joined: 2011-06-10T11:29:39-07:00
Authentication code: 8675308

Re: Upsizing Undersized Images

Post by arcking »

fmw42 wrote:see note about -quality for jpg images at http://www.imagemagick.org/script/comma ... r5#quality

Your images are probably getting decompressed and then recompressed if you are resaving them as jpg, so you lose quality. Also the quality may not be as high as you started unless you specify the -quality value you want.
I'm pretty sure that's what's happening, which is why I want to bypass (just skip them) any images that are already large enough - is there any way to do that?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Upsizing Undersized Images

Post by fmw42 »

With your command line, IM will not resize them but will decompress and recompress the jpgs. I don't know any way to avoid IM processing even to read and write it again, except to write a script to compute the image sizes or what not and skip processing them altogether. But perhaps one of the IM developers may have other suggestions.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Upsizing Undersized Images

Post by anthony »

[quote="arcking"It successfully upsizes all of the small images, but also modifies all of the images that are already large enough (they stay the same resolution, but are compressed).[/quote]

Hmmm it may be that the 'abort' on resize does not preserve the 'taint' flag of the image.
But then I am not certain taint actually aborts a write to the same image.

Hmm quick test using PNG images says it always over writes the image.

Cristy will know more about taint effects.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
arcking
Posts: 10
Joined: 2011-06-10T11:29:39-07:00
Authentication code: 8675308

Re: Upsizing Undersized Images

Post by arcking »

fmw42 wrote:With your command line, IM will not resize them but will decompress and recompress the jpgs. I don't know any way to avoid IM processing even to read and write it again, except to write a script to compute the image sizes or what not and skip processing them altogether. But perhaps one of the IM developers may have other suggestions.
I was thinking I might have to use a script...any suggestions on what I should do the scripting in?

Am I correct in my understanding that any modification done to a JPEG is lossy? In other words, even if I rewrite the file with "-quality 100" it will have lost some (minute) level of detail (compared to the original). Ultimately a script will probably prove to be worthwhile considering that it's going to have to process over 300,000 images and that I can specify a minimum size that is different from what they'll be resized to.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Upsizing Undersized Images

Post by fmw42 »

arcking wrote:[
I was thinking I might have to use a script...any suggestions on what I should do the scripting in?

Am I correct in my understanding that any modification done to a JPEG is lossy? In other words, even if I rewrite the file with "-quality 100" it will have lost some (minute) level of detail (compared to the original). Ultimately a script will probably prove to be worthwhile considering that it's going to have to process over 300,000 images and that I can specify a minimum size that is different from what they'll be resized to.
Yes, as far as I know jpeg even with -quality 100 with still give some loss. You could use the lossless JP2000 from the jasper library or some other lossless image format, but then the file size will likely be larger.

I script in bash unix. That is pretty much all I know. But you have many options for IM APIs as well.

Here is an example:

#/bin/bash
cd yourdirectory
list=`ls`
for name in $list; do
area=`convert $name -ping -format "%[fx:w*h]" info:`
if [ $area -lt 100000 ]; then
echo "$name $area"
fi
done

put this in a text file and then type

bash textfilename
arcking
Posts: 10
Joined: 2011-06-10T11:29:39-07:00
Authentication code: 8675308

Re: Upsizing Undersized Images

Post by arcking »

Thanks for your help! Got a Windows batch script working great - for reference, the meat of the file is posted below:

Code: Select all

SET minsize=2925
SET newsize=3000

SETLOCAL ENABLEDELAYEDEXPANSION

FOR %%A in (*.jpg) DO (
	ECHO %%A
	FOR /F "tokens=* delims=" %%B in ('convert %%A -ping -format %%w info:') Do (
		SET width=%%B
	)
	FOR /F "tokens=* delims=" %%B in ('convert %%A -ping -format %%h info:') Do (
		SET height=%%B
	)
	IF !width! LSS %minsize% (
		IF !height! LSS %minsize% (
			ECHO: - Resized
			ECHO "%%A","Resized" >>upsized\Resize_log_file.csv
			convert %%A   -resize %newsize%x%newsize%  upsized\%%A
		)
	)
	ECHO.
)

PAUSE
Post Reply