Advertisement
valerio_mazza

Funzione collage

Oct 31st, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. # Funzione che partendo da due immagini: flower1.jpg , flower2.jpg
  2. # contenute nella cartella mediasources: http://www.cc.gatech.edu/~mark.guzdial/mediacomp/mediasources-py2ed.zip
  3. # realizza un collage di 5 immagini con:
  4. # 1a flower1 originale
  5. # 2a flower2 originale
  6. # 3a flower1 in negativo
  7. # 4a flower2 senza il colore blu
  8. # 5a flower1 in negativo senza il colore rosso
  9.  
  10. def collage(pict1, pict2):
  11. # @param pict1: Picture. flower1.jpg
  12. # @param pict1: Picture. flower2.jpg
  13.    canvas = createCanvas(pict1)
  14.    negPict1 = negative(pict1)
  15.    noBluePict2 = removeBlue(pict2)
  16.    noRedPict1 = removeRed(negPict1)
  17.    allPicture = (pict1, pict2, negPict1, noBluePict2, noRedPict1)
  18.    finalCollage = applyCollage(canvas, allPicture)
  19.    show(finalCollage)
  20.    
  21. def createCanvas(pict):
  22. # @param pict: Picture
  23.   canvas = makeEmptyPicture(5*getWidth(pict), getHeight(pict))
  24.   return canvas
  25.    
  26. def negative(pict):
  27. # @param pict: Picture
  28.   negPict1 = makeEmptyPicture( getWidth(pict), getHeight(pict))
  29.   for x in range(getWidth(pict)):
  30.     for y in range(getHeight(pict)):
  31.       pxOriginal = getPixel(pict, x, y)
  32.       pxNegative = getPixel(negPict1, x, y)
  33.       setColor(pxNegative, makeColor(255 - getRed(pxOriginal), 255 - getGreen(pxOriginal), 255 - getBlue(pxOriginal)))
  34.   return negPict1
  35.    
  36. def removeBlue(pict):
  37. # @param pict: Picture
  38.   noBluePict2 =  makeEmptyPicture( getWidth(pict), getHeight(pict))
  39.   for x in range(getWidth(pict)):
  40.     for y in range(getHeight(pict)):
  41.       pxOriginal = getPixel(pict, x, y)
  42.       pxNoBlue = getPixel(noBluePict2, x, y)
  43.       setColor(pxNoBlue, makeColor(getRed(pxOriginal), getGreen(pxOriginal), 0*getBlue(pxOriginal)))
  44.   return noBluePict2      
  45.  
  46. def removeRed(pict):
  47. # @param pict: Picture
  48.   noRedPict1 =  makeEmptyPicture( getWidth(pict), getHeight(pict))
  49.   for x in range(getWidth(pict)):
  50.     for y in range(getHeight(pict)):
  51.       pxOriginal = getPixel(pict, x, y)
  52.       pxNoRed = getPixel(noRedPict1, x, y)
  53.       setColor(pxNoRed, makeColor(0*getRed(pxOriginal), getGreen(pxOriginal), getBlue(pxOriginal)))
  54.   return noRedPict1  
  55.  
  56. def applyCollage(canvas, allPicture):
  57. # @param canvas: Picture
  58. # @param allPicture: String
  59.   startX = 0
  60.   for pic in allPicture:
  61.     for sourceX in range(getWidth(pic)):
  62.       targetX = sourceX + startX
  63.       for sourceY in range(getHeight(pic)):
  64.         targetY = sourceY
  65.         color = getColor(getPixel(pic, sourceX, sourceY))
  66.         setColor(getPixel(canvas ,targetX, targetY), color)      
  67.     startX = targetX
  68.   return canvas
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement