1
$\begingroup$

I have a Data Set of 170 images in total. of each image i want to create 36 images like this.

An image is cropped from its top-left corner by cropping 5 percent from the top and from the left side of the image. Then, the new image is scaled back to the original size of 188x188. This cropping method could be also done from three other corners. The same thing could be done with 10 percent cropping. This results in 8 images besides the original image. Afterward, these 9 images are rotated by 0, 90, 180 and 270 degrees. Therefore, 36 synthesized images are derived from each input image.

I need python code for this dataset augmentation.

$\endgroup$

1 Answer 1

3
$\begingroup$

If you need to work on images using Python, the preferred library is PIL. Here I show a function to do the modifications you have delimited. This code makes no effort to manage multiple files, or how to name the files converted. But it does show how to modify the images in the ways you have asked.

Code:

from PIL import Image

def modify_image(im, crop_corner=None, crop_percent=5, rotate=0):
    """
    Crop and rotate an image

    :param im: base image 
    :param crop_corner: crop corner 0-3 clockwise from top left 
    :param crop_percent: crop percentage of size 
    :param rotate: rotate degrees (rotate after crop) 
    :return: a modified image 
    """
    x, y = im.size
    if crop_corner is not None:
        crop_x, crop_y = [int(i * crop_percent / 100.0) for i in (x, y)]
        size_x = x - crop_x
        size_y = y - crop_y
        if crop_corner in (1, 2):
            crop_x = 0
        if crop_corner in (2, 3):
            crop_y = 0
        im = im.crop((crop_x, crop_y, crop_x + size_x-1, crop_y + size_y-1))
        im = im.resize((x, y), Image.BICUBIC)

    im = im.rotate(rotate, expand=True)
    return im

Test Code:

test_image = Image.open('test_image.png')
new_image = modify_image(test_image, rotate=90)
new_image.show()

To save the image see the (Docs)

$\endgroup$
1
  • $\begingroup$ it is different from data augmentation.it is image modification. i will see if this works for me. thanks for you contribution. $\endgroup$ Jun 7, 2017 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.