コード例 #1
0
def rotateImage(image, theta):
    width = image.width()
    height = image.height()
    newWidth = int((image.width()**2 + image.height()**2)**.5)
    newHeight =int((image.width()**2 + image.height()**2)**.5)
    newImage1 = GraphicsImage(newWidth, newHeight)
    newImage2 = GraphicsImage(newWidth, newHeight)
    angle = math.radians(theta)    
    hEdgeDiff = newHeight - height
    wEdgeDiff = newWidth - width
    h = (newWidth // 2)
    k = (newHeight // 2)
    print(height, width, newHeight, newWidth)
    
    for row in range(height):
        for col in range(width):            
            
            r,g,b = image.getPixel(row, col)
            newImage1.setPixel(row + hEdgeDiff//2, col + wEdgeDiff//2, r, g, b)     
            
    for row in range(newHeight):
        for col in range(newWidth):     
            x = col - h
            y = row - k            
            x1 = int(x*math.cos(angle) + y*math.sin(angle))
            y1 = int(y*math.cos(angle) - x*math.sin(angle))            
            x = x1 + h
            y = y1 + k                    
            if (x >= 0 and y >= 0) and (x < newWidth and y < newHeight):
                r,g,b = newImage1.getPixel(y,x)                
            else:
                r,g,b = 0,0, 0
            newImage2.setPixel(row, col, r, g, b)
            
    return newImage2
コード例 #2
0
# Load the original image.
origImage = GraphicsImage(filename)

# Create an empty image that will contain the new flipped image.
width = origImage.width()
height = origImage.height()
newImage = GraphicsImage(width, height)

# Iterate over the image and copy the pixels to the new image to
# produce the flipped image (vertical flip to an upside down image)
newRow = height - 1
for row in range(height):
    for col in range(width):
        newCol = col
        pixel = origImage.getPixel(row, col)
        newImage.setPixel(newRow, newCol, pixel)
    newRow = newRow - 1

# Save the new image with a new name.
newImage.save("flipped-" + filename)

# 5. Textbook R.29
# show only the green component of each pixel of the image to green

# Draw the image
win = GraphicsWindow()
canvas = win.canvas()
canvas.drawImage(newImage)
win.wait()