Automatically resize of images

Ask your 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.
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
bhnuser
Posts: 48
Last visit: Tue Nov 21, 2023 10:33 pm

Automatically resize of images

Post by bhnuser »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Automatically resize of images

Post by jvierra »

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

See attached script.
Attachments
Set-ImageSize.ps1
(4.58 KiB) Downloaded 158 times
User avatar
bhnuser
Posts: 48
Last visit: Tue Nov 21, 2023 10:33 pm

Re: Automatically resize of images

Post by bhnuser »

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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Automatically resize of images

Post by jvierra »

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.
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked