Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. tempFile.seek(-height*3, SEEK_CUR)
  2.  
  3. ## Rotates the image 90 degrees to the right.
  4. # @param imgFile the image that will be rotated
  5. # @param start where the pixels in the bitmap begin
  6. # @param width the width of the image
  7. # @param height the height of the image
  8. # @param the extra padding for each row of bytes
  9. #
  10. def rotateRight(imgFile, start, width, height, padding) :
  11. # Create a temp image with dimensions opposite to the original
  12. tempFile = open("queen-mary-temp.bmp", "wb+")
  13.  
  14. imgFile.seek(0)
  15. theBytes = imgFile.read()
  16. tempFile.write(bytes(theBytes))
  17.  
  18. imgFile.seek(18)
  19. tempFile.seek(22)
  20. theBytes = imgFile.read(4)
  21. tempFile.write(bytes(theBytes))
  22.  
  23. imgFile.seek(22)
  24. tempFile.seek(18)
  25. theBytes = imgFile.read(4)
  26. tempFile.write(bytes(theBytes))
  27.  
  28. # Move to the first pixel in the image.
  29. imgFile.seek(start) # Go to the start of the pixels.
  30. for row in range(height) :
  31. tempFile.seek(start + (width-1) * height*3 + row*3) # Go to the end of the pixels for the temp file
  32. for col in range(width) :
  33. theBytes = imgFile.read(3) # Read the pixel bytes.
  34. tempFile.write(bytes(theBytes)) # Writes the bytes to the temp image
  35. tempFile.seek(-height*3, SEEK_CUR) # The problem line
  36. # Skip the padding at the end of the row.
  37. imgFile.seek(padding, SEEK_CUR)
  38.  
  39. # Go to the beginning for both images
  40. imgFile.seek(0)
  41. tempFile.seek(0)
  42.  
  43. # Copies the temp data to the original image
  44. theBytes = tempFile.read()
  45. imgFile.write(bytes(theBytes))
  46. tempFile.close()
  47.  
  48. from sys import exit
  49. from imageprocessingtoolkit import *
  50.  
  51. # Read the name of the file to be processed.
  52. #filename = input("Enter the name of the image file to be processed: ")
  53. filename = "queen-mary.bmp"
  54.  
  55. # Open as a binary file for reading and writing.
  56. imgFile = open(filename, "rb+")
  57.  
  58. # Extract the image information.
  59. fileSize = readInt(imgFile, 2)
  60. start = readInt(imgFile, 10)
  61. width = readInt(imgFile, 18)
  62. height = readInt(imgFile, 22)
  63.  
  64. # Scan lines must occupy multiples of four bytes.
  65. scanlineSize = width * 3
  66. if scanlineSize % 4 == 0 :
  67. padding = 0
  68. else :
  69. padding = 4 - scanlineSize % 4
  70.  
  71. # Make sure this is a valid image.
  72. if fileSize != (start + (scanlineSize + padding) * height) :
  73. exit("Not a 24-bit true color image file.")
  74.  
  75.  
  76.  
  77. done = False
  78.  
  79. while not done :
  80. # Prompt the user for the type of processing.
  81. print("How should the image be processed?")
  82. print("1 - create image negative")
  83. print("2 - create image grayscale")
  84. print("3 - adjust brigthness")
  85. print("4 - flip vertically")
  86. print("5 - flip horizontally")
  87. print("6 - rotate to the left")
  88. print("7 - rotate to the right")
  89. print("8 - apply mean (`average') filter")
  90. print("9 - save and quit")
  91.  
  92. response = int(input("Enter your choice: "))
  93.  
  94. # Process the image.
  95. if response == 1 :
  96. createNegative(imgFile, start, width, height, padding)
  97. elif response == 2 :
  98. createGrayscale(imgFile, start, width, height, padding)
  99. elif response == 3 :
  100. amount = float(input("Adjustment between -1.0 and 1.0: "))
  101. adjustBrightness(imgFile, amount, start, width, height, padding)
  102. elif response == 4 :
  103. flipVertically(imgFile, start, width, height, padding)
  104. elif response == 5 :
  105. flipHorizontally(imgFile, start, width, height, padding)
  106. elif response == 6 :
  107. rotateLeft(imgFile, start, width, height, padding)
  108. elif response == 7 :
  109. rotateRight(imgFile, start, width, height, padding)
  110. elif response == 8 :
  111. meanFilter(imgFile, start, width, height, padding)
  112.  
  113. if response == 9 :
  114. done = True
  115.  
  116. imgFile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement