Tips for image file size on disc (MBS?)

One customer is asking for the ability to generate an image which must be less than 10Mb on disc.
(Etsy have a limit)

The source could in theory be anything up to 32000 x 32000 pixels in size (although that is extreme)

Is there anything that can tell me before/ at the point of saving , how large the saved image would be on disc?

PNG, JPG are viable
MBS Plugins are acceptable

I just want to avoid having to save , check the size, redo until small enough.

You could load with GraphicsMagick classes, e.g. GMimageMBS class.
Then you could try various things like scaling down in a loop. Maybe also trying various JPEG compression levels.

In my web work, I have found JPEG will be smaller than PNG for the same image, but PNG is a must if you need the image to have a mask.

Doing it in a loop sounds sensible, but that implies saving to disc over and over again, which I would be wanting to avoid.
Im really looking for
If I use THESE settings, how big will the file be ?

and check that in a loop , so that the save occurs one time only.

The stupid thing about Etsy is that they apparently insist on an image that is 2000 pixels or wider, and yet also impose a MB limit.

I’ve done a similar thing before by saving to the temp folder in the loop, then saving the correct sized file at the user’s request. Then you can show X by Y pixels = Z MB to allow for selection by the user.

You would do a binary search and maybe do a starting scale.
e.g. start with 5000x5000 limit (proportional scale down), compress to string (not file) and check length.
Then maybe go up/down.

Xojo has the ability to convert a picture to jpeg, but in memory, so you don’t need to write to disk.

dim p as picture = xxx
dim picureData as MemoryBlock = p.toData(Picture.JPEG)
dim pictureSize as Integer = pictureData.size

See https://documentation.xojo.com/api/graphics/picture.html#picture-todata

1 Like

Grand. Thanks!

1 Like