# OpenCV numpy image tranformation efficiency [closed]

I am doing a few image transformations - resize, offset, and crop. I am doing all these operations separately, but I feel like they can be better combined to get a speed improvement. This is happening hundreds of times in a loop, so I am looking to optimize this code. Any suggestions would be great - I am somewhat new to numpy. Here is my code as it is now:

# original image is only loaded into memory once:

# image is resized...
image_resize = cv2.resize(self.original_image, (0, 0), fx=zoom, fy=zoom)

# offset image
M = np.float32([[1, 0, x_total], [0, 1, y_total]])
image_offset = cv2.warpAffine(image_resize, M, (self.original_image_width, self.original_image_width))

#image crop
image = image_offset[0:self.output_raster_height, 0:self.output_raster_width].copy()

#numpy to pil and away!
corrected_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
from_pil = PImage.fromarray(corrected_img)
from_pil.save(p.stdin, 'JPEG')


Suggestions would be much appreciated!

• Which Python version are you using? – Phrancis Nov 8 '17 at 23:21
• @Phrancis python3 of course. Is there another version? ;-) – M Leonard Nov 8 '17 at 23:23
• There are many undefined variables in this code. Without more context, it's hard to provide a proper review. – IEatBagels Aug 14 '19 at 13:13