Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. #Charles Power
  2. #Consulted Dylan Ray, UNC Chapel Hill Mathematics
  3.  
  4. from cImage import *
  5. import sys, traceback
  6.  
  7. def flag(w, h):
  8. newimg = EmptyImage(w, h)
  9.  
  10. for row in range(h):
  11. #for each row in (h) height
  12. for column in range(w):
  13. #for each column in row
  14. if column < w/3:
  15. #if pixel is in 1/3 make red
  16. red = 255
  17. green = 0
  18. blue = 0
  19. elif column < (w/3 + w/3):
  20. #if pixel is in 2/3 make white
  21. red = 255
  22. green = 255
  23. blue = 255
  24. else:
  25. #if pixel is in 3/3 make blue
  26. red= 0
  27. green = 0
  28. blue = 255
  29.  
  30.  
  31. p = Pixel(red, green, blue)
  32. newimg.setPixel(column,row,p)
  33.  
  34. return newimg
  35.  
  36. def flip_horizontal(img):
  37. """Flip the specified image left to right and return the modified image"""
  38. w = img.getWidth()
  39. h = img.getHeight()
  40.  
  41. newimg = EmptyImage(w,h)
  42.  
  43. for y in range(h):
  44. #iterate through each pixel in row
  45. for x in range(w):
  46. #gets current pixel in row
  47. p = img.getPixel(x,y)
  48. newimg.setPixel((w-1) - x, y, p)
  49.  
  50. return newimg
  51.  
  52. def sepia_tone(img):
  53. """Apply the sepia tone transformation to the specified image, and return the modified image"""
  54. w = img.getWidth()
  55. h = img.getHeight()
  56.  
  57. newimg = EmptyImage(w,h)
  58.  
  59. for y in range(h):
  60. #iterate through each pixel in image
  61. for x in range(w):
  62. #iterate through each pixel in row
  63. p = img.getPixel(x,y)
  64. # gets the current pixel's red intensity
  65. r = p.getRed()
  66. g = p.getGreen()
  67. b = p.getBlue()
  68. #apply formula to get new pixel
  69. newR = int(r * 0.393 + g * 0.769 + b * 0.189)
  70. newG = int(r * 0.349 + g * 0.686 + b * 0.168)
  71. newB = int(r * 0.272 + g * 0.534 + b * 0.131)
  72.  
  73. if newR > 255:
  74. newR = 255
  75. if newG > 255:
  76. newG = 255
  77. if newB > 255:
  78. newB = 255
  79.  
  80. newpixel = Pixel(newR, newG, newB)
  81. newimg.setPixel(x,y,newpixel)
  82.  
  83. return newimg
  84.  
  85. def convert_to_gray_scale(img):
  86. """Apply the gray scale transformation to the specified image, and return the modified image"""
  87. w = img.getWidth()
  88. h = img.getHeight()
  89.  
  90. newimg = EmptyImage(w,h)
  91.  
  92. for y in range(h):
  93. #iterate through each pixel in image
  94. for x in range(w):
  95. #iterate through each pixel in row
  96. p = img.getPixel(x,y)
  97. # gets the current pixel's red intensity
  98. r = p.getRed()
  99. g = p.getGreen()
  100. b = p.getBlue()
  101. #apply formula to get new pixel
  102. newR = int(r * 0.21 + g * 0.71 + b * 0.07)
  103. newG = int(r * 0.21 + g * 0.71 + b * 0.07)
  104. newB = int(r * 0.21 + g * 0.71 + b * 0.07)
  105.  
  106. if newR > 255:
  107. newR = 255
  108. if newG > 255:
  109. newG = 255
  110. if newB > 255:
  111. newB = 255
  112.  
  113. newpixel = Pixel(newR, newG, newB)
  114. newimg.setPixel(x,y,newpixel)
  115.  
  116. return newimg
  117.  
  118. def flip_vertical(img):
  119. """Flip the specified image upside down and return the modified image"""
  120. w = img.getWidth()
  121. h = img.getHeight()
  122.  
  123. newimg = EmptyImage(w,h)
  124.  
  125. for y in range(h):
  126. #iterate through each pixel in row
  127. for x in range(w):
  128. #gets current pixel in row
  129. p = img.getPixel(x,y)
  130. newimg.setPixel(x, (h-1) - y, p)
  131.  
  132. return newimg
  133.  
  134. def rotate_right_90(img):
  135. """Rotate the specified image 90 degrees to the right and return the modified image"""
  136. w = img.getWidth()
  137. h = img.getHeight()
  138.  
  139. newimg = EmptyImage(h,w)
  140.  
  141. for y in range(h):
  142. #iterate through each pixel in row
  143. for x in range(w):
  144. #gets current pixel in row
  145. p = img.getPixel(x,y)
  146. newimg.setPixel(y, x, p)
  147. return flip_horizontal(newimg)
  148.  
  149.  
  150. #construct a new image that
  151. #is a copy of img in which
  152. #each pixel retains all the red
  153. #and loses the green and blue components.
  154. #the function returns the modified image
  155. def red_filter(img):
  156. """Apply the red filter transformation to the specified image, and return the modified image"""
  157.  
  158. w = img.getWidth()
  159. h = img.getHeight()
  160.  
  161. newimg = EmptyImage(w,h)
  162.  
  163. for y in range(h):
  164. for x in range(w):
  165. p = img.getPixel(x,y)
  166.  
  167.  
  168. newpixel = Pixel(p.getRed(),0,0)
  169.  
  170. newimg.setPixel(x,y,newpixel)
  171.  
  172. return newimg
  173.  
  174. #construct a new image that
  175. #is a copy of img in which
  176. #each pixel's red, green, and blue
  177. #components are replaced with their complement with respect to 255
  178. #the function returns the modified image
  179. def negative(img):
  180. """Apply the negative transformation to the specified image, and return the modified image"""
  181.  
  182. w = img.getWidth()
  183. h = img.getHeight()
  184.  
  185. newimg = EmptyImage(w,h)
  186.  
  187. for y in range(h):
  188. for x in range(w):
  189. p = img.getPixel(x,y)
  190.  
  191.  
  192. newpixel = Pixel(255-p.getRed(),255-p.getGreen(),255 - p.getBlue())
  193.  
  194. newimg.setPixel(x,y,newpixel)
  195.  
  196. return newimg
  197.  
  198. ##DO NOT MODIFY THE FUNCTIONS BELOW
  199. ##THEY ARE USED FOR TESTING YOUR FUNCTIONS
  200. ##AND DISPLAYING IMAGES
  201.  
  202. #display the specified image in a window
  203. def display_image(img, t):
  204. """Display the specified image in a window"""
  205. win = ImageWin(title = t, width = img.getWidth(), height = img.getHeight())
  206. img.draw(win)
  207.  
  208. #don't modify this function
  209. def test_one(transform, filename):
  210. transformName = str(transform).split()[1]
  211. print("Starting "+ transformName)
  212.  
  213. try:
  214. #create an image corresponding to the specified file
  215. img = Image(filename)
  216.  
  217. #apply the specified transformation to the image
  218. img = transform(img)
  219.  
  220. #show the transformed image
  221. display_image(img, transformName)
  222. except:
  223. print("\t"+transformName + ":Error")
  224.  
  225. #don't modify this function
  226. #I will use it to test all your functions
  227. def test_all():
  228. transforms = [flip_horizontal, sepia_tone, convert_to_gray_scale, flip_vertical, rotate_right_90]
  229. for t in transforms:
  230. test_one(t, "penguins.gif")
  231. #test_all()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement