2

I want to work with a bunch of images (100+) and I need to keep their aspect ratios (which varies between each other), but resize them to be a maximum of 1000x1000 and have a maximum file size of 100kb.

I have tried the "optimize-images" package but I couldn't get the results I wanted because I couldn't be specific enough. I also tried resizing with imageio, but the size issue remains. I have read different sources and replies to similar issues, but have found no way of doing this.

import imageio
import os

os.chdir("C:\\Users\\abc123\\Pictures\\Resize")

im = imageio.imread("a.jpg")

small = transform.resize(im, (1000,1000), mode="symmetric", preserve_range=True)

Ideally, I will use the walk() method to find all the images in the folder, resize them to 1000x1000, maintain the aspect ratio by filling with blank the differential between the final size and the resized image, and finally apply a 0.8 or 0.75 quality reduction until the file size is =< 100 kb. I realize my code is very basic, but I'm mostly looking for directions/inspiration for how I could tackle this problem. Thanks in advance!

CypherX
  • 7,019
  • 3
  • 25
  • 37
jajajaja
  • 69
  • 3
  • 7
  • 1
    You can find half your answer at https://stackoverflow.com/q/13407717/5987 – Mark Ransom Oct 01 '19 at 00:21
  • 1
    And maybe this will help too: https://stackoverflow.com/a/3008966/5987 if you combine it with `resize()`. – Mark Ransom Oct 01 '19 at 00:25
  • 1
    You can do it without writing any code, just in Terminal with **ImageMagick**. Make a copy of a few images in a separate directory and try it `magick -resize 1000x1000 -define jpeg:extent=100KB *.jpg` – Mark Setchell Oct 01 '19 at 07:48

1 Answers1

1

You could use skimage library.

import numpy as np
from skimage import data, color
from skimage.transform import rescale, resize

grayimage = color.rgb2gray(data.astronaut())
image_rescaled = rescale(grayimage, 1.0 / 4.0, anti_aliasing=False, multichannel = False)
image_resized = resize(grayimage, (grayimage.shape[0] / 4, grayimage.shape[1] / 4),
                       anti_aliasing=True)
plt.imshow(np.hstack([image_rescaled, image_resized]))
plt.title('Rescaled'+ ''.join([" "]*30) +'Resized')
plt.show()

Output:
enter image description here

Saving Image with Reduced File Size

For controlling the file size while saving the image to your file system, you could use PIL library with optimize=True and quality=some_number. See this thread: How to reduce the image file size using PIL.

References

HowTo: Rescale and Resize using skimage Library

CypherX
  • 7,019
  • 3
  • 25
  • 37
  • Thanks! I've been trying skimage, but is there a way to cap the file size to 100Kb with skimage that you know of? – jajajaja Oct 03 '19 at 11:08
  • You could optimize and apply a quality value using PIL library to save the already scaled/resized image and save it on disk. See this: https://stackoverflow.com/questions/10607468/how-to-reduce-the-image-file-size-using-pil. If you must save the file with a certain size, you could write a python loop which would check the file size of the saved image and if the image size is bigger than what your limit is, than it would try with a lower quality value. – CypherX Oct 04 '19 at 01:05