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:
self.original_image = cv2.imread(self.file_path, cv2.IMREAD_UNCHANGED)

# 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!

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

Browse other questions tagged or ask your own question.