Automatically resize of images

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
bhnuser
Posts: 39
Joined: Fri Apr 06, 2018 7:35 am

Automatically resize of images

Post by bhnuser » Thu Jun 21, 2018 6:18 am

Hello everybody,

i am looking for a "simple" script to resize images for our ActiveDirectory. The feature is, that the imported picture should be resized to a bytesize of 100 KB and scale automatically. Width and higth is not possible and can have a higher resolution.
Does a script exist or someone know how to realize?

Greez

User avatar
jvierra
Posts: 13507
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Automatically resize of images

Post by jvierra » Thu Jun 21, 2018 6:48 am

$image = [system.drawing.Bitmap]::FromFile(<filepath>)

See attached script.
Attachments
Set-ImageSize.ps1
(4.58 KiB) Downloaded 27 times

User avatar
bhnuser
Posts: 39
Joined: Fri Apr 06, 2018 7:35 am

Re: Automatically resize of images

Post by bhnuser » Thu Jun 21, 2018 10:58 pm

Thank you very much for the fast answer.
What I can say is that this script looks very nice and i can find a use in it. But i have one more question: Can i set a fixedlength of 100 KB and let the script scale the image automatically to the right size of 100 KB? If yes, how i need to modify the script?

User avatar
jvierra
Posts: 13507
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Automatically resize of images

Post by jvierra » Fri Jun 22, 2018 12:18 am

Sorry but that is not how images work. An image requires a specific number of bytes. A JPEG image requires as many bytes ass there are cells times the color depth. A JPEG can be compressed but, as with all compression, the size of the result is entirely dependent on the content of the source. It cannot be predicted or requested.

A 300Mb JPEG uncompressed can be reduced by anywhere from 2 to more than 10 times using 80% compression. Any raw JOEG can be reduced by at least 30% in most cases using lossless compression.

You could compress in a loop by incremental amounts until you reach a desired size or just compress by 80% an hoe it is enough.

Resizing a JPEG does not reduce the size of the file.

Search for command line utilities designed to reduce image files. These are common for web development for compressing images for web display. YOu can also use the code posted to access the JEG as an image file (bitmap) and use the codecs to compress the image.

Locked