Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tempFile.seek(-height*3, SEEK_CUR)
- ## Rotates the image 90 degrees to the right.
- # @param imgFile the image that will be rotated
- # @param start where the pixels in the bitmap begin
- # @param width the width of the image
- # @param height the height of the image
- # @param the extra padding for each row of bytes
- #
- def rotateRight(imgFile, start, width, height, padding) :
- # Create a temp image with dimensions opposite to the original
- tempFile = open("queen-mary-temp.bmp", "wb+")
- imgFile.seek(0)
- theBytes = imgFile.read()
- tempFile.write(bytes(theBytes))
- imgFile.seek(18)
- tempFile.seek(22)
- theBytes = imgFile.read(4)
- tempFile.write(bytes(theBytes))
- imgFile.seek(22)
- tempFile.seek(18)
- theBytes = imgFile.read(4)
- tempFile.write(bytes(theBytes))
- # Move to the first pixel in the image.
- imgFile.seek(start) # Go to the start of the pixels.
- for row in range(height) :
- tempFile.seek(start + (width-1) * height*3 + row*3) # Go to the end of the pixels for the temp file
- for col in range(width) :
- theBytes = imgFile.read(3) # Read the pixel bytes.
- tempFile.write(bytes(theBytes)) # Writes the bytes to the temp image
- tempFile.seek(-height*3, SEEK_CUR) # The problem line
- # Skip the padding at the end of the row.
- imgFile.seek(padding, SEEK_CUR)
- # Go to the beginning for both images
- imgFile.seek(0)
- tempFile.seek(0)
- # Copies the temp data to the original image
- theBytes = tempFile.read()
- imgFile.write(bytes(theBytes))
- tempFile.close()
- from sys import exit
- from imageprocessingtoolkit import *
- # Read the name of the file to be processed.
- #filename = input("Enter the name of the image file to be processed: ")
- filename = "queen-mary.bmp"
- # Open as a binary file for reading and writing.
- imgFile = open(filename, "rb+")
- # Extract the image information.
- fileSize = readInt(imgFile, 2)
- start = readInt(imgFile, 10)
- width = readInt(imgFile, 18)
- height = readInt(imgFile, 22)
- # Scan lines must occupy multiples of four bytes.
- scanlineSize = width * 3
- if scanlineSize % 4 == 0 :
- padding = 0
- else :
- padding = 4 - scanlineSize % 4
- # Make sure this is a valid image.
- if fileSize != (start + (scanlineSize + padding) * height) :
- exit("Not a 24-bit true color image file.")
- done = False
- while not done :
- # Prompt the user for the type of processing.
- print("How should the image be processed?")
- print("1 - create image negative")
- print("2 - create image grayscale")
- print("3 - adjust brigthness")
- print("4 - flip vertically")
- print("5 - flip horizontally")
- print("6 - rotate to the left")
- print("7 - rotate to the right")
- print("8 - apply mean (`average') filter")
- print("9 - save and quit")
- response = int(input("Enter your choice: "))
- # Process the image.
- if response == 1 :
- createNegative(imgFile, start, width, height, padding)
- elif response == 2 :
- createGrayscale(imgFile, start, width, height, padding)
- elif response == 3 :
- amount = float(input("Adjustment between -1.0 and 1.0: "))
- adjustBrightness(imgFile, amount, start, width, height, padding)
- elif response == 4 :
- flipVertically(imgFile, start, width, height, padding)
- elif response == 5 :
- flipHorizontally(imgFile, start, width, height, padding)
- elif response == 6 :
- rotateLeft(imgFile, start, width, height, padding)
- elif response == 7 :
- rotateRight(imgFile, start, width, height, padding)
- elif response == 8 :
- meanFilter(imgFile, start, width, height, padding)
- if response == 9 :
- done = True
- imgFile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement