Does anyone have code they can share to resize images using FastAI2 while preserving aspect ratio for dataset creation?
Update - some progress with this:
pil = PILImage.create(fullimg[0])
pil=pil.resize_max(resample = Image.BICUBIC,max_h=500, max_w=500)
img_quality=100
newname = 'resize.jpg'
savefile = resize_dir/ newname;savefile
pil.save(savefile, quality=img_quality)
#checking new image
check = PILImage.create(savefile)
check.size # verify image size
I was trying to pull the ResizeRatio code out from transforms to just make a resizer class but the transforms have a lot of built in dependencies.
I’d like to have prebuiilt datasets for various px sizes instead of redoing it every time I train basically.
The resize_max appears to preserve aspect ratio but not 100% sure (comments in the code would be…):
def resize_max(x: Image.Image, resample=0, max_px=None, max_h=None, max_w=None):
"`resize` `x` to `max_px`, or `max_h`, or `max_w`"
h,w = x.shape
if max_px and x.n_px>max_px: h,w = Tuple(h,w).mul(math.sqrt(max_px/x.n_px))
if max_h and h>max_h: h,w = (max_h ,max_h*w/h)
if max_w and w>max_w: h,w = (max_w*h/w,max_w )
return x.reshape(round(h), round(w), resample=resample)