Beispiel #1
0
def main():
    IMG_NAME = 'monkey'
    px = (128, 255, 255)
    #open image
    #read each pixel into memory as the image object im
    with Image.open(IMG_NAME + '.jpg') as im:
        pixels = storePixels(im)
        print("stored")

        sorted_pixels = pixels.copy()
        selection_sort(sorted_pixels)
        print("sorted")
        sorted_im = pixelsToImage(im, sorted_pixels)
        sorted_pixels.save('sorted_' + IMG_NAME + '.jpg', 'JPEG')

        while(True):
            command = input("Type a value for red threshhold or Q to quit:")
            if(command == "Q"):
                break

            threshhold = int(command)

            subi = binarySearchSub([r[0][0] for r in sorted_pixels], \
                                   0,len(sorted_pixels)-1, threshhold)

            pixelsToPoints(im, sorted_pixels[subi:])
            im.show()
        #print("Sublist of reds starts at: ", subi)

    im.save('gray_'+ IMG_NAME + '.jpg', 'JPEG')
Beispiel #2
0
def main():
    global TOGGLE_SLICE
    global RED
    global GREEN
    global BLUE
    global TOLERENCE

    while (True):

        IMG_NAME = 'tinyrose'  #tiny rose
        #IMG_NAME = 'bigrose'   #big rose

        os.system('python mouseptr.py')

        # open image
        # read each pixel into memory as the image object im
        with Image.open(IMG_NAME + '.jpg') as im:
            pixels, yiq_pixels = storePixels(im)  # store rgb pixels

            # selectionSort(yiq_pixels, comparePixels) #sort on first val
            mergeSort(yiq_pixels, comparePixels)  # sort on first val
            ## may need sorted image to see what is going on
            sorted_im = pixelsToImage(im, yiq_pixels)
            sorted_im.save('sorted_' + IMG_NAME + '.jpg', 'JPEG')

            grayScale(im, pixels)  # grayscale pixels in place
            # replace threshold with target color to pivot around
            target = (int(RED) / 255, int(GREEN) / 255, int(BLUE) / 255
                      )  # /255 for conversiono
            print("RED: ", RED)
            print("GREEN: ", GREEN)
            print("BLUE: ", BLUE)
            yiq_target = colorsys.rgb_to_yiq(target[0], target[1], target[2])

            if TOLERENCE:
                tolerance = int(len(yiq_pixels) / 2)
                print("Tolerance: ", tolerance)
                # change the pivot point
                subi = tolerance
                TOLERENCE = False  #reset tolerance
            else:
                # use yiq_target instead  of threshold in search
                subi = binarySearchSub([r[0][0] for r in yiq_pixels], 0,
                                       len(yiq_pixels) - 1, yiq_target[0])

            print("subi:", subi)
            # subi = binarySearchSub([r[0][0] for r in sorted_pixels],
            #                        0, len(sorted_pixels) - 1, threshold)
            if (TOGGLE_SLICE):
                pixelsToPoints(im,
                               yiq_pixels[0:subi])  # put saved pixels on gray
            else:
                pixelsToPoints(im, yiq_pixels[subi:])
            im.show()
        # end with Image.open(IMG_NAME + '.jpg') as im:

        # command = input("Type (Q|q) to save file and quit:")
        # if (command in ('q', 'Q')):
        #     # save my image data from memory to a file with a different name
        #     im.save('highlighted_' + IMG_NAME + '.jpg', 'JPEG')
        #     break

        #taken from: https://stackoverflow.com/questions/34192588/simple-menu-in-python-3
        choice = '0'
        while choice == '0':
            print("Main Choice: Choose 1 of 5 choices")
            print("Choose '(Q|q)' to save file and quit:")
            print("Choose '(R|r)' to reverse the image")
            print("Choose '(T|t)' to set image Tolerance")
            print("Choose '(C|c)' to set image's RGB value")

            choice = input("Please make a choice: ")
            if choice in ('q', 'Q'):
                # save my image data from memory to a file with a different name
                im.save('highlighted_' + IMG_NAME + '.jpg', 'JPEG')
                quit()
            elif choice in ('r', 'R'):
                TOGGLE_SLICE = not TOGGLE_SLICE  # toggle
                print("Do 'R'")
            elif choice in ('t', 'T'):
                TOLERENCE = True
            elif choice in ('c', 'C'):
                r = input("Enter a value for red: ")
                if is_number(r):
                    RED = r
                else:
                    print("Invalid value for Red")
                g = input("Enter a value for green: ")
                if is_number(g):
                    GREEN = g
                else:
                    print("Invalid value for Green")
                b = input("Enter a value for blue: ")
                if is_number(b):
                    BLUE = b
                else:
                    print("Invalid value for Blue")
            else:
                print("I don't understand your choice.")
    def processImage(self):
        global TOGGLE_SLICE
        global RED
        global GREEN
        global BLUE
        global TOLERENCE

        print("filename", self.fileName)
        print(os.path.splitext(self.fileName)[0])
        file = os.path.splitext(self.fileName)[0]
        print(file.split('/'))
        base = file.split('/')
        print(base[1])


        # read each pixel into memory as the image object im
        # with Image.open(IMG_NAME + '.jpg') as im:
        with Image.open(self.fileName) as im:
            pixels, yiq_pixels = storePixels(im)  # store rgb pixels

            # selectionSort(yiq_pixels, comparePixels) #sort on first val
            mergeSort(yiq_pixels, comparePixels)  # sort on first val
            ## may need sorted image to see what is going on
            sorted_im = pixelsToImage(im, yiq_pixels)
            sorted_im.save('sorted_' + base[1] + '.jpg', 'JPEG')
            print("sorted pixels")

            grayScale(im, pixels)  # grayscale pixels in place
            # replace threshold with target color to pivot around
            target = (int(RED) / 255, int(GREEN) / 255, int(BLUE) / 255)  # /255 for conversiono
            print("RED: ", RED)
            print("GREEN: ", GREEN)
            print("BLUE: ", BLUE)
            yiq_target = colorsys.rgb_to_yiq(target[0], target[1], target[2])

            if TOLERENCE:
                tolerance1 = int(len(yiq_pixels) / 2)
                print("Tolerance1: ", tolerance1)
                subi = tolerance1
                # use yiq_target instead  of threshold in search
                subi = binarySearchSub([r[0][0] for r in yiq_pixels],
                                       0, len(yiq_pixels) - 1, yiq_target[0])
                im.show()
                tolerance2 = int(len(yiq_pixels) / 4)
                print("Tolerance2: ", tolerance2)
                subi = tolerance2
                # use yiq_target instead  of threshold in search
                subi = binarySearchSub([r[0][0] for r in yiq_pixels],
                                       0, len(yiq_pixels) - 1, yiq_target[0])
                im.show()
                tolerance3 = int(len(yiq_pixels) / 8)
                print("Tolerance3: ", tolerance3)
                subi = tolerance3
                # use yiq_target instead  of threshold in search
                subi = binarySearchSub([r[0][0] for r in yiq_pixels],
                                       0, len(yiq_pixels) - 1, yiq_target[0])
                im.show()

                TOLERENCE = False  # reset tolerance
            else:
                # use yiq_target instead  of threshold in search
                subi = binarySearchSub([r[0][0] for r in yiq_pixels],
                                       0, len(yiq_pixels) - 1, yiq_target[0])

            print("subi:", subi)
            # subi = binarySearchSub([r[0][0] for r in sorted_pixels],
            #                        0, len(sorted_pixels) - 1, threshold)
            if (TOGGLE_SLICE):
                pixelsToPoints(im, yiq_pixels[0:subi])  # put saved pixels on gray
            else:
                pixelsToPoints(im, yiq_pixels[subi:])
            im.show()
def main():
    #IMG_NAME = 'tinyrose'   #Tiny
    #IMG_NAME = 'bigrose'   #Medium
    #IMG_NAME = 'abq'        #Large 500x300
    IMG_NAME = 'abq_huge'   #Huge 1500x1300

    # open image
    # read each pixel into memory as the image object im
    with Image.open(IMG_NAME + '.jpg') as im:
        pixels = storePixels(im)
        print("stored")

        ### sort copy of pixels ###
        sorted_pixels = pixels.copy()
        #selectionSort(sorted_pixels, comparePixels)
        quickSortIterative(sorted_pixels, 0, len(sorted_pixels)-1, comparePixels)
        print("sorted")
        sorted_im = pixelsToImage(im, sorted_pixels)
        sorted_im.save('sorted_'+ IMG_NAME + '.jpg', 'JPEG')

        # grayscale pixels in place
        #grayScale(im, pixels)

        while(True): #do whileloop in Python does not exist
            command = input("Type a value for red threshold or Q to quit:")
            if(command in ('q', 'Q')):
                save = input("Save File? (Y|y) or press any other key to Quit")
                if (save in ('y', 'Y')):
                    format = input("which format?  enter '1' for 'JPEG';  '2' for 'PNG'")
                    if (format == '1'):
                        print("Saved as JPEG")
                        im.save('highlighted_' + IMG_NAME + '.jpg', 'JPEG')
                    elif (format == '2'):
                        print(" Saved as PNG")
                        im.save('highlighted_' + IMG_NAME + '.png', 'PNG')
                    else:
                        print("Invalid option - file was not saved")
                break   #leave loop

            #might want to test that command is a number or convert it
            threshold = int(command)

            # grayscale pixels in place
            grayScale(im, pixels)

            #search copy for r
            # use a comprehension to find the sorted r values
            # sorted_r_values = [r[0][0] for r in sorted_pixels}
            subi = binarySearchSub([r[0][0] for r in sorted_pixels],
                                   0, len(sorted_pixels)-1, threshold)
            #print("Sublist of resds starts at: ", subi)

            # restore found r
            # uses list slice notation to remove any item before subi
            pixelsToPoints(im, sorted_pixels[subi:])
            #pixelsToPoints(im, sorted_pixels[0:subi])
            im.show()
        #end while(True)

    # end with Image.open(IMG_NAME + '.jpg') as im:

    # save my image data from memory to a file with a different name
    im.save('highlighted_' + IMG_NAME + '.jpg', 'JPEG')