コード例 #1
0
def remove_red(p):
    new_red = 0
    green = p.getGreen()
    blue = p.getBlue()
    new_pixel = image.Pixel(new_red, green, blue)
    return new_pixel
コード例 #2
0
win = image.ImageWin(w, h)


def avg(x):
    return sum(x) / len(x)


# for col in range(w):
#     for row in range(h):
#         p = img.getPixel(col, row)
#         newimg.setPixel(2 * col, 2 * row, p)
#         newimg.setPixel(2 * col + 1, 2 * row, p)
#         newimg.setPixel(2 * col, 2 * row + 1, p)
#         newimg.setPixel(2 * col + 1, 2 * row + 1, p)
for col in range(1, w - 1):
    for row in range(1, h - 1):
        indexes = [0, 0], [0, 1], [1, 0], [-1, 0], [0, -1]
        r = []
        g = []
        b = []

        for pixelIndex in indexes:
            pixel = img.getPixel(col + pixelIndex[0], row + pixelIndex[1])
            r.append(pixel.getRed())
            g.append(pixel.getGreen())
            b.append(pixel.getBlue())
        newpixel = image.Pixel(avg(r), avg(g), avg(b))
        newimg.setPixel(col, row, newpixel)

newimg.draw(win)
win.exitonclick()
コード例 #3
0
# WALKTHROUGH -------------------------------------------------------------------
# For this walkthrough, we will write a program to apply a red filter to an image.
# (Note that when you run the code, it will take a few seconds to process.)

import image
import sys

image = image.Image("luther.jpg")
new_img = image.EmptyImage(img.getWidth(), img.getHeight())
win = image.ImageWin(img.getWidth(), img.getHeight())

for i in range(0, img.getWidth()):
    for j in range(0, img.getHeight()):
        old_p = img.getPixel(i, j)
        red = old_p.getRed()
        new_p = image.Pixel(red, 0, 0)
        new_img.setPixel(i, j, new_p)

new_img.draw(win)
win.exitonclick()

# STUDIO -----------------------------------------------------------------------
# For this studio, your job is to write an algorithm that processes an image to
# make it "fuzzy" looking. SEE TEXTBOOK FOR EXAMPLES / TO RUN YOUR CODE.
#
# The algorithm to achieve this effect is actually fairly simple: for each pixel,
# randomly choose one of its neighboring pixels, and use that pixel's color
# instead.
#
# For example, imagine that the table below is a zoomed-in view of the pixels in
# our image, and that we are trying to alter the center pixel (the one whose color
コード例 #4
0
        p = img.getPixel(x+1, y)
        r = p.getRed()
        g = p.getGreen()
        b = p.getBlue()

        Gx += 2 * (r + g + b)

        p = img.getPixel(x+1, y+1)
        r = p.getRed()
        g = p.getGreen()
        b = p.getBlue()

        Gx += (r + g + b)
        Gy += (r + g + b)

        # calculate the length of the gradient (Pythagorean theorem)
        length = math.sqrt((Gx * Gx) + (Gy * Gy))

        # normalise the length of gradient to the range 0 to 255
        length = length / 4328 * 255

        length = int(length)

        # draw the length in the edge image
        newpixel = image.Pixel(length, length, length)
        newimg.setPixel(x, y, newpixel)

newimg.draw(win)
win.exitonclick()
コード例 #5
0
# Q7 remove red
import image

img = image.Image("luther.jpg")
newimg = image.EmptyImage(img.getWidth(), img.getHeight())
win = image.ImageWin()

for col in range(img.getWidth()):
    for row in range(img.getHeight()):
        p = img.getPixel(col, row)

        newred = 0
        green = p.getGreen()
        blue = p.getBlue()

        newpixel = image.Pixel(newred, green, blue)

        newimg.setPixel(col, row, newpixel)

newimg.draw(win)
win.exitonclick()


# Q9 convert an image to black and white
import image

def convertBlackWhite(input_image):
    grayscale_image = image.EmptyImage(input_image.getWidth(), input_image.getHeight())

    for col in range(input_image.getWidth()):
        for row in range(input_image.getHeight()):
コード例 #6
0
ファイル: image2.py プロジェクト: Isaac0620/classroom
import image


pika = image.Image("pikachu.jpg")

win = image.ImageWin(pika.getWidth(), pika.getHeight(), "shock")


pika.draw(win)


for x in range(pika.getWidth()):
	for y in range(pika.getHeight()):
		orig = pika.getPixel(x,y)
		pika.setPixel(x,y,image.Pixel(255- orig.getRed(),255- orig.getGreen(),255- orig.getBlue()))
	pika.draw(win)




win.exit_on_click()
コード例 #7
0
ファイル: Q9.py プロジェクト: leekeh/learning-to-code
import image

img = image.Image("luther.jpg")
win = image.ImageWin(img.getWidth(), img.getHeight())

for row in range(img.getHeight()):
    for col in range(img.getWidth()):
        p = img.getPixel(col, row)
        avghue = (p.getRed() + p.getGreen() + p.getBlue()) / 3
        if avghue < 140:
            newpixel = image.Pixel(0, 0, 0)
        else:
            newpixel = image.Pixel(255, 255, 255)
        img.setPixel(col, row, newpixel)

img.draw(win)
コード例 #8
0
# Exercise 6
# Write a program to remove all the red from an image.
img = image.Image("luther.jpg")
win = image.ImageWin(img.getWidth(), img.getHeight())
img.draw(win)
img.setDelay(1,15)   # setDelay(0) turns off animation, but appears to freeze while processing...

for row in range(img.getHeight()):
    for col in range(img.getWidth()):
        p = img.getPixel(col, row)

        new_red = 0
        new_green = p.getGreen()
        new_blue = p.getBlue()

        new_pixel = image.Pixel(new_red, new_green, new_blue)

        img.setPixel(col, row, new_pixel)

img.draw(win)
win.exitonclick()

# Exercise 7
# Write a function to convert the image to grayscale.

import image

img = image.Image("luther.jpg")
win = image.ImageWin(img.getWidth(), img.getHeight())
img.draw(win)
img.setDelay(1,15)   # setDelay(0) turns off animation, but appears to freeze while processing...
コード例 #9
0
import image

img = image.Image("ucd.jpg")
win = image.ImageWin(img.getWidth(), img.getHeight())
print(img.getWidth(), img.getHeight())
img.draw(win)
img.setDelay(1, 15)  # setDelay(0) turns off animation

for row in range(img.getHeight()):
    for col in range(img.getWidth()):
        p = img.getPixel(col, row)

        red = p.getRed()
        green = p.getGreen()
        blue = p.getBlue()
        avg = (red + green + blue) / 3
        newpixel = image.Pixel(avg, avg, avg)
        print(newpixel)
        img.setPixel(col, row, (avg, avg, avg))

img.draw(win)
win.exitonclick()
コード例 #10
0
ファイル: ImageProc.py プロジェクト: marcusmarinhob/Python
import os
os.system('cls')

print('\n# BEGIN OF THE SCRIPT #############################################')

import image

p = image.Pixel(45, 76, 200)
print(p.getRed())
p.setRed(66)
print(p.getRed())
p.setBlue(p.getGreen())
print(p.getGreen(), p.getBlue())

# Auto git saving #####################################################
save = 1
commitMsg = '1. Image Processing.' 

if save:
    print('\nAUTO GIT SAVING...')
    
    fileName = os.path.basename(__file__)    

    os.system('git add '+ fileName)
    os.system('git commit -m '+'"'+commitMsg+'"')
    os.system('git push')

    print('\nFile Name: '+fileName)
    print('\nCommit Message: '+commitMsg)
    print('\nSAVED AND PUSHED!')
コード例 #11
0
import image

img = image.Image("luther.jpg")
newimg = image.EmptyImage(img.getWidth(), img.getHeight())
win = image.ImageWin()

for col in range(img.getWidth()):
    for row in range(img.getHeight()):
        p = img.getPixel(col, row)
        x = 255 if (p.getRed()+p.getBlue()+p.getGreen())/3 > 140 else 0

        newpixel = image.Pixel(x, x, x)

        newimg.setPixel(col, row, newpixel)

newimg.draw(win)
win.exitonclick()
コード例 #12
0
#In order to manipulate an image, we need to be able to access individual pixels. This capability is provided by a module called image, provided in ActiveCode 1. The image module defines two classes: Image and Pixel.

#If you want to explore image processing on your own outside of the browser you can install the cImage module from http://pypi.org.

#Each Pixel object has three attributes: the red intensity, the green intensity, and the blue intensity. A pixel provides three methods that allow us to ask for the intensity values. They are called getRed, getGreen, and getBlue. In addition, we can ask a pixel to change an intensity value using its setRed, setGreen, and setBlue methods.

Pixel(20,100,50)  #Create a new pixel with 20 red, 100 green, and 50 blue.
getRed() example r = p.getRed()  #Return the red component intensity. #samefor b g
setRed() example p.setRed(100)  #Set the red component intensity to 100. #samefor b g

#heres a example
import image
p = image.Pixel(45, 76, 200)
print(p.getRed())
p.setRed(66)
print(p.getRed())
p.setBlue(p.getGreen())
print(p.getGreen(), p.getBlue())

#that was all about, pixels.  now image objects

Image(filename) img = image.Image(“cy.png”)     Create an Image object from the file cy.png.

EmptyImage() img = image.EmptyImage(100,200)     Create an Image object that has all “White” pixels

getWidth() w = img.getWidth()   Return the width of the image in pixels.

getHeight() h = img.getHeight()    Return the height of the image in pixels.

getPixel(col,row)  p = img.getPixel(35,86)    Return the pixel at column 35, row 86.
コード例 #13
0
'''Take the Luther Bell image and tint it red'''
import image

img = image.Image("luther.jpg")
win = image.ImageWin()
for x in range(img.getWidth()):
    for y in range(img.getHeight()):
        p = img.getPixel(x, y)
        red = p.getRed()
        green = p.getGreen()
        blue = p.getBlue()
        new_p = image.Pixel(255, green, blue)
        #or
        #new_p=image.Pixel(red,0,0)
        img.setPixel(x, y, new_p)
img.draw(win)
win.exitonclick()

コード例 #14
0
import image

img = image.Image("luther.jpg")
win = image.ImageWin(img.getWidth(), img.getHeight())

for row in range(img.getHeight()):
    for col in range(img.getWidth()):
        p = img.getPixel(col, row)
        avghue = (p.getRed()+p.getGreen()+p.getBlue())/3
        newpixel = image.Pixel(avghue, avghue, avghue)
        img.setPixel(col, row, newpixel)
        
img.draw(win)
コード例 #15
0
            while col > 0 and col < width - 1:
                p1 = img.getPixel(col - 1, row - 1)
                p2 = img.getPixel(col, row - 1)
                p3 = img.getPixel(col + 1, row - 1)
                p4 = img.getPixel(col - 1, row)
                p5 = img.getPixel(col, row)
                p6 = img.getPixel(col + 1, row)
                p7 = img.getPixel(col - 1, row + 1)
                p8 = img.getPixel(col, row + 1)
                p9 = img.getPixel(col + 1, row + 1)

                avgRed = (p1.getRed() + p2.getRed() + p3.getRed() +
                          p4.getRed() + p5.getRed() + p6.getRed() +
                          p7.getRed() + p8.getRed() + p9.getRed()) / 9
                avgGreen = (p1.getGreen() + p2.getGreen() + p3.getGreen() +
                            p4.getGreen() + p5.getGreen() + p6.getGreen() +
                            p7.getGreen() + p8.getGreen() + p9.getGreen()) / 9
                avgBlue = (p1.getBlue() + p2.getBlue() + p3.getBlue() +
                           p4.getBlue() + p5.getBlue() + p6.getBlue() +
                           p7.getBlue() + p8.getBlue() + p9.getBlue()) / 9

        newpixel = image.Pixel(avgRed, avgGreen, avgBlue)

        newimg.setPixel(2 * col, 2 * row, newpixel)
        newimg.setPixel(2 * col + 1, 2 * row, newpixel)
        newimg.setPixel(2 * col, 2 * row + 1, newpixel)
        newimg.setPixel(2 * col + 1, 2 * row + 1, newpixel)

newimg.draw(win)
win.exitonclick()
コード例 #16
0
width = source_image.get_width()   #find the width and
height = source_image.get_height()  #height of the image

window = image.ImageWin(width, height)  #create a window

source_image.draw(window)

#Set up Loops to Visit every pixel and do some work

for x in range(width):
    for y in range(height):
        #retrieve the current pixel
        current_pixel = source_image.get_pixel(x,y)
        
        #access the R,G,B component values
        r = current_pixel.get_red()
        g = current_pixel.get_green()
        b = current_pixel.get_blue()
        
        if r+g+b < 30:
            r = 50
            g = y
            b = x
        
        new_pixel = image.Pixel(r, g, b)  #create a new pixel
        source_image.set_pixel(x,y,new_pixel) #create a new pixel
    
    if x % 3 == 0: #to speed up the animation
        source_image.draw(window)  #animate the manipulation
source_image.draw(window)            
コード例 #17
0
import image

img = image.Image("luther.jpg")
win = image.ImageWin()

newimg = image.EmptyImage(img.getWidth(), img.getHeight())

for col in range(img.getWidth()):
    for row in range(img.getHeight()):
        p = img.getPixel(col, row)

        newred = p.getRed() * 0.393 + p.getGreen() * 0.769 + p.getBlue() * 0.189
        newgreen = p.getRed() * 0.349 + p.getGreen() * 0.686 + p.getBlue(
        ) * 0.168
        newblue = p.getRed() * 0.272 + p.getGreen() * 0.534 + p.getBlue(
        ) * 0.131

        newpixel = image.Pixel(newred, newgreen, newblue)

        newimg.setPixel(col, row, newpixel)

newimg.draw(win)
win.exitonclick()
コード例 #18
0
import image
import sys
import random

img = image.Image("luther.jpg")
new_img = image.EmptyImage(img.getWidth(), img.getHeight())
win = image.ImageWin(img.getWidth(), img.getHeight())

for i in range(1, img.getWidth() - 1):
    for j in range(1, img.getHeight() - 1):
        # TODO: Randomly choose the coordinates of a neighboring pixel
        #random.randint(i - 1, i + 1)
        #random.randint(j - 1, j + 1)

        p = img.getPixel(((random.randrange(-1, 2, 2)) + i), ((random.randrange(-1, 2, 2)) + j))

        red = p.getRed()
        green = p.getGreen()
        blue = p.getBlue()
        # TODO: in the new image, set this pixel's color to the neighbor's color
        new_p = image.Pixel(red, green, blue)
        new_img.setPixel(i, j, new_p)

new_img.draw(win)
win.exitonclick()

コード例 #19
0
You could use this image referenced by Luther.jpg.
Tips:

    Be careful! What happens when you get to an edge?
    If you use pixel indexes i and j you can access the neighbors by adding and subtracting one from those numbers, i.e. i+1, i-1, ...
'''
import image
import sys

# Set the timeout to a larger number if timeout is occuring.
sys.getExecutionLimit(30000)

img = image.Image("luther.jpg")
newimg = image.EmptyImage(img.getWidth(), img.getHeight())
win = image.ImageWin()

for i in range(0, img.getWidth()):
    for j in range(0, img.getHeight()):
        old_p = img.getPixel(i, j)
        # TODO: Complete the inner part of this loop to blur the image.
        # new_p = image.Pixel(255, old_p.getBlue(), old_p.getGreen())
        # newimg.setPixel(i, j, new_p)
        red = old_p.getRed()
        blue = old_p.getBlue()
        green = old_p.getGreen()
        new_p = image.Pixel(255, old_p.getBlue(), old_p.getGreen())
        newimg.setPixel(i, j, new_p)

newimg.draw(win)
win.exitonclick()
コード例 #20
0
ファイル: image_test.py プロジェクト: InprissSorce/image
def remove_red(pix):
    new_red = 0
    new_green = pix.green
    new_blue = pix.blue
    new_pix = image.Pixel(new_red, new_green, new_blue)
    return new_pix
コード例 #21
0
import image

img = image.Image("luther.jpg")
newimg = image.EmptyImage(img.getWidth(), img.getHeight())
win = image.ImageWin()

for col in range(img.getWidth()):
    for row in range(img.getHeight()):
        p = img.getPixel(col, row)

        newred = 0
        green = p.getGreen()
        blue = p.getBlue()

        newpixel = image.Pixel(newred, green, blue)

        newimg.setPixel(col, row, newpixel)

newimg.draw(win)
win.exitonclick()
コード例 #22
0
ファイル: image_test.py プロジェクト: InprissSorce/image
def remove_blue(pix):
    new_red = pix.red
    new_green = pix.green
    new_blue = 0
    new_pix = image.Pixel(new_red, new_green, new_blue)
    return new_pix
コード例 #23
0
source_image.draw(window)

#Set up Loops to Visit every pixel and do some work

for x in range(1, width):
    for y in range(1, height):
        #retrieve the current pixel
        current_pixel = source_image.get_pixel(x, y)
        shifted.set_pixel(x - 2, y - 2, current_pixel)

#     if x % 3 == 0: #to speed up the animation
#         source_image.draw(window)  #animate the manipulation
shifted.draw(window)

for x in range(width):
    for y in range(height):
        pixel_one = source_image.get_pixel(x, y)
        pixel_two = shifted.get_pixel(x, y)

        avg_one = (pixel_one.get_red() + pixel_one.get_green() +
                   pixel_one.get_blue()) / 3
        avg_two = (pixel_two.get_red() + pixel_two.get_green() +
                   pixel_two.get_blue()) / 3

        if abs(avg_one - avg_two) > THRESHOLD:
            source_image.set_pixel(x, y, image.Pixel(255, 0, 0))
        #else:
        #source_image.set_pixel(x,y,image.Pixel(255,255,255))
    if x % 3 == 0:  #to speed up the animation
        source_image.draw(window)
source_image.draw(window)
コード例 #24
0
ファイル: image_test.py プロジェクト: InprissSorce/image
def greyscale(pix):
    r, g, b = pix.red, pix.green, pix.blue
    avg = r // 3 + g // 3 + b // 3
    new_pix = image.Pixel(avg, avg, avg)
    return new_pix
コード例 #25
0
def graypixel(oldpixel):
    intensitysum = oldpixel.getRed() + oldpixel.getGreen() + oldpixel.getBlue()
    aveRGB = intensitysum // 3
    newPixel = image.Pixel(aveRGB, aveRGB, aveRGB)
    return newPixel
コード例 #26
0
height = img.get_height()

# make a window to draw on
win = image.ImageWin(width, height)

# draw the original image onto the window
img.draw(win)

# use a nested for loop to look at every pixel in the image
for x in range(width):
    for y in range(height):
        if y <= height / 2:
            # get the current pixel
            this_pixel = img.get_pixel(x, y)
        else:
            distance_from_midline = y - height / 2
            this_pixel = img.get_pixel(x, height / 2 - distance_from_midline)

        # access the amount of red, green and blue for this pixel
        r = this_pixel.get_red()
        g = this_pixel.get_green()
        b = this_pixel.get_blue()

        new_pixel = image.Pixel(r, g, b)

        # reassign the pixel value in the image to be the changed version
        img.set_pixel(x, y, new_pixel)

    # draw the changed image to the window
    img.draw(win)
コード例 #27
0
ファイル: index.py プロジェクト: jsilvia721/steganography
import image

fg = image.Image('secret.png')
newIm = image.EmptyImage(fg.getWidth(), fg.getHeight())

for row in range(fg.getHeight()):
    for col in range(fg.getWidth()):
        fgpix = fg.getPixel(col, row)
        fgr = fgpix.getRed()

        if fgr % 2 == 0:
            newPix = image.Pixel(255, 255, 255)
        else:
            newPix = image.Pixel(0, 0, 0)
        newIm.setPixel(col, row, newPix)

win = image.ImageWin(500, 400)
newIm.draw(win)
win.exitonclick()
コード例 #28
0
ファイル: shocked.py プロジェクト: Isaac0620/classroom
import image

img = image.Image("pika.png")
win = image.ImageWin(img.getWidth(), img.getHeight())
img.draw(win)

for y in range(img.getHeight()):
    for x in range(img.getWidth()):
        p = img.getPixel(x, y)
        r = 255 - p.getRed()
        g = 255 - p.getGreen()
        b = 255 - p.getBlue()

        newPixel = image.Pixel(r, g, b)

        img.setPixel(x, y, newPixel)
    img.draw(win)

win.exitonclick()
"""for i in range(5):
    for j in range(3):
        print(i,j)
    print()


listOfWords = ["hello", "world", "have", "a", "nice", "day"]
for word in listOfWords:
    for letter in word:
        print(letter)
"""
コード例 #29
0
 def __setitem__(self, key, value):
     r, g, b = value
     x, y = key
     self._parent._image.updatePixel(_image_module.Pixel(r, g, b, x, y))
コード例 #30
0
import image

win = image.ImageWin(640, 480, "Image Processing")

myPic = image.EmptyImage(100, 100)
for x in range(myPic.getWidth()):
    for y in range(myPic.getHeight()):
        myPic.setPixel(x, y, image.Pixel(200, 255, 0))

myPic.draw(win)
win.exit_on_click()