Esempio n. 1
0
    def savegif(self, path, gifpath, size):
        image = MyImage(path)
        image.convert2grayscale()
        image.squareit()

        # calculate ft for resizing
        imft = ImgFFT(image.data)
        imft.ft()
        im = imft.resize_image(size[0], size[1])

        imrgb = MyRGBImg(np.zeros((size[0], size[1], 3)))
        for c in range(3):
            imrgb.set_channel(im, c)

        # save resized image
        imsave(gifpath, imrgb.data, format="gif")
Esempio n. 2
0
def run_create_test_dataset(folder):
    debug_mode = True

    # set parameters
    testdatasetpath = folder
    mypicname = "./data/Lenna.png"

    n_pictures = 25
    min_a = -10
    max_a = 10
    min_xs = -25
    max_xs = 25
    min_ys = -25
    max_ys = 25
    flip_angle = True

    mylog = Logger("Create gray dataset",
                   testdatasetpath + "main_logfile.txt",
                   debug_mode=debug_mode)

    mylog.log("Creating dataset in:\n" + testdatasetpath)
    mylog.log("Using the picture: " + mypicname)

    mylog.log("Creating dataset with {0} pictures".format(n_pictures))
    mylog.log("With rotations from {0} to {1} degree".format(min_a, max_a))
    mylog.log("With shift in x: from {0} to {1} and y: from {2} to {3}".format(
        min_xs, max_xs, min_ys, max_ys))
    mylog.log(
        "The dataset will be generated by {0} randomly flipping rotations and translations"
        .format("" if flip_angle == True else "not"))

    if not isdir(testdatasetpath):
        mkdir(testdatasetpath)
        mylog.log("Created test dataset path")

    # create a test dataset:

    mypic = MyImage(mypicname)
    mypic.squareit()
    mypic.convert2grayscale()
    mypic.binning(2)
    mypic.normalize()

    mylog.log("Processing done")

    template_folder = join(testdatasetpath, "template_folder")

    if not isdir(template_folder):
        mkdir(template_folder)

    mypic.save(join(template_folder, "template.png"))
    mylog.log("Saved the original image in the template folder")

    if debug_mode:
        mypic.show_image()
        plt.show()

    mylog.log(
        "------------------------------\nCreating dataset\n------------------------------"
    )

    np.random.seed(10)

    logpathdir = join(testdatasetpath, "tlog")
    if not isdir(logpathdir):
        mkdir(logpathdir)

    with open(join(logpathdir, "mytransformations.log"), 'w') as f:
        angles = np.random.uniform(min_a, max_a, n_pictures)
        for i in range(n_pictures):
            image = deepcopy(mypic)
            if flip_angle:
                anglefirst = False if np.random.randint(0, 2) == 0 else True
            else:
                anglefirst = True

            angle = angles[i]
            dx = np.random.randint(min_xs, max_xs)
            dy = np.random.randint(min_ys, max_ys)

            f.write("{0} {1} {2} {3}\n".format(anglefirst, dx, dy, angle))
            mylog.log(
                "Pictrue with: rot first {0}, angle: {1}, shift x: {2}, y: {3} created"
                .format(anglefirst, angle, dx, dy))

            if anglefirst:
                image.rotate(angle)
                image.move(dx, dy)
            else:
                image.move(dx, dy)
                image.rotate(angle)

            if debug_mode:
                image.show_image()
                plt.show()

            image.save(join(testdatasetpath, "pic_" + str(i) + ".png"))