Sunday, February 19, 2017

Edit photo - Square crop - Image resizing in Python

from skimage import data, io
import sys
from skimage.color import gray2rgb

try:
    rgb_image = data.imread(sys.argv[1])
    height = rgb_image.shape[0]
    width = rgb_image.shape[1]

    if height == width:
        print("already square image")
   
    else:
        if(len(rgb_image.shape) < 3):
            rgb_image = gray2rgb(rgb_image)
           
        right_image = rgb_image.copy()
        left_image = rgb_image.copy()
        center_image = rgb_image.copy()
       
        if height < width:
            right_image = right_image[0:height, 0:height, :]
            left_image = left_image[0:height, int(width - height):width, :]
            center_image = center_image[0:height, int(width/2 - height/2) : int(width/2 + height/2), :]               
            io.imsave("right_image.jpg", right_image)
            io.imsave("left_image.jpg", left_image)
            io.imsave("center_image.jpg", center_image)
           
        else:
            right_image = right_image[0:width, 0:width, :]
            left_image = left_image[int(height - width):height, 0:width, :]
            center_image = center_image[int(height/2 - width/2) : int(height/2 + width/2), 0:width, :]           
            io.imsave("top_image.jpg", right_image)
            io.imsave("bottom_image.jpg", left_image)               
            io.imsave("middle_image.jpg", center_image)
       
except Exception as e:
    print(e)


Original:


Output:




Original:


Output:



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.