# Resizing pictures for file size

**URL:** <https://www.ruby-forum.com/t/resizing-pictures-for-file-size/127988>\
**Category:** Ruby\
**Created:** [February 3, 2008, 11:30pm UTC](https://www.ruby-forum.com/t/resizing-pictures-for-file-size/127988 "2008-02-03T23:30:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JSPablo\_FernSSSSnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@JSPablo\_FernSSSSnd](https://www.ruby-forum.com/u/JSPablo_FernSSSSnd)\
**Post date:** [February 3, 2008, 11:30pm UTC](https://www.ruby-forum.com/t/resizing-pictures-for-file-size/127988/1 "2008-02-03T23:30:49Z")

</div>

Hello,

I’m using rmagick but I’m open to other libraries if needed. I want to  
resize an image, scale actually (mantaining the aspect ratio) so that  
the  
size of the file is lower than a certain amount of bytes. Any ideas  
how to  
do this?

Thanks.

---

<div class="post-metadata">

**Author:** ![JSPablo\_FernSSSSnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@JSPablo\_FernSSSSnd](https://www.ruby-forum.com/u/JSPablo_FernSSSSnd)\
**Post date:** [February 4, 2008, 12:35am UTC](https://www.ruby-forum.com/t/resizing-pictures-for-file-size/127988/2 "2008-02-04T00:35:25Z")

</div>

J. Pablo Fernández wrote:

> –  
> J. Pablo Fernández [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) ([http://pupeno.com](http://pupeno.com))

I can’t speak for other libraries, but this is not possible with  
RMagick. This question comes up from time-to-time on the ImageMagick  
forum but the answer is always “not possible” other than just by  
trial-and-error.

---

<div class="post-metadata">

**Author:** ![JSPablo\_FernSSSSnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@JSPablo\_FernSSSSnd](https://www.ruby-forum.com/u/JSPablo_FernSSSSnd)\
**Post date:** [February 4, 2008, 2:18am UTC](https://www.ruby-forum.com/t/resizing-pictures-for-file-size/127988/3 "2008-02-04T02:18:01Z")

</div>

On Mon, 2008-02-04 at 08:34 +0900, Tim H. wrote:

> > ## Thanks.
> > 
> > J. Pablo Fernández [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) ([http://pupeno.com](http://pupeno.com))
> 
> I can’t speak for other libraries, but this is not possible with  
> RMagick. This question comes up from time-to-time on the ImageMagick  
> forum but the answer is always “not possible” other than just by  
> trial-and-error.

What exactly is not possible? Resize according to the certain amount of  
bytes after the resize?

---

<div class="post-metadata">

**Author:** ![JSPablo\_FernSSSSnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@JSPablo\_FernSSSSnd](https://www.ruby-forum.com/u/JSPablo_FernSSSSnd)\
**Post date:** [February 4, 2008, 2:30am UTC](https://www.ruby-forum.com/t/resizing-pictures-for-file-size/127988/4 "2008-02-04T02:30:19Z")

</div>

David Moreno wrote:

> > >
> 
> bytes after the resize?

There is no method in RMagick that takes an image and a file size in  
bytes and returns an image that, when saved to a file, produces a file  
close to or no larger than the specified size. The file size depends on  
the image format, the compression level, and the image itself.

---

<div class="post-metadata">

**Author:** ![JSPablo\_FernSSSSnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@JSPablo\_FernSSSSnd](https://www.ruby-forum.com/u/JSPablo_FernSSSSnd)\
**Post date:** [February 4, 2008, 3:10am UTC](https://www.ruby-forum.com/t/resizing-pictures-for-file-size/127988/5 "2008-02-04T03:10:10Z")

</div>

Marcin R. wrote:

> mean and min from qualities.
> 
> now you have 2 qualities - one that 50% of images should fit with size -  
> and second that almost all should fit.
> 
> that’s assuming that your surce images have simillar resolutions.
> 
> greets

Sounds like a plan. Maybe the OP will adopt it if he can’t find a  
library that will do what he wants automatically.

---

<div class="post-metadata">

**Author:** ![JSPablo\_FernSSSSnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@JSPablo\_FernSSSSnd](https://www.ruby-forum.com/u/JSPablo_FernSSSSnd)\
**Post date:** [February 4, 2008, 3:03am UTC](https://www.ruby-forum.com/t/resizing-pictures-for-file-size/127988/6 "2008-02-04T03:03:42Z")

</div>

> There is no method in RMagick that takes an image and a file size in  
> bytes and returns an image that, when saved to a file, produces a file  
> close to or no larger than the specified size. The file size depends on  
> the image format, the compression level, and the image itself.

There are tools that can estaminate size of file after compresion - but  
they’ll never give you exact size without actual compression.

So … you take script, 20-30 simple images, take each one, start with  
some quality - and incrementally decrase untill size fits, then you take  
mean and min from qualities.

now you have 2 qualities - one that 50% of images should fit with size -  
and second that almost all should fit.

that’s assuming that your surce images have simillar resolutions.

greets
