Advertisement
ClearCode

Merging + ImageChops

May 18th, 2022
4,876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. from PIL import Image, ImageChops
  2.  
  3. panda = Image.open('picture.jpg').convert('RGBA')
  4. owl = Image.open('owl.jpg') # same size as panda image
  5. python = Image.open('python.png') # smaller than the panda image + has transparency
  6.  
  7. # merging images with image.methods()
  8. image_blend = Image.blend(panda,owl,10) # both images need same size and mode
  9. # image_composite = Image.composite(panda,owl,Image.new('L',panda.size,50))
  10.  
  11. # image paste
  12. panda.paste(python,(100,300), mask = python)
  13.  
  14. # channel operations
  15. # image_overlay = ImageChops.overlay(panda,owl)
  16. # image_darker = ImageChops.darker(panda,owl)
  17. # image_lighter = ImageChops.lighter(panda,owl)
  18. # image_soft_light = ImageChops.soft_light(panda,owl)
  19. # image_hard_light = ImageChops.hard_light(panda,owl)
  20. # image_difference = ImageChops.difference(panda,owl)
  21. # image_modulo = ImageChops.add_modulo(panda,owl)
  22. # image_screen = ImageChops.screen(panda,owl)
  23.  
  24. # more complex channel operations
  25. # image_add = ImageChops.add(panda,owl,scale = 2.0,offset = 100)
  26. # image_subtract = ImageChops.subtract(panda,owl,scale = 2.0,offset = 100)
  27. # image_logical_and = ImageChops.logical_and(panda.convert('1'),owl.convert('1'))
  28. # image_logical_or = ImageChops.logical_or(panda.convert('1'),owl.convert('1'))
  29.  
  30. # super easy
  31. # image_invert = ImageChops.invert(panda)
  32.  
  33. # masking
  34. # mask must have a specfic mode: RGBA, 1 or L
  35. # mask = Image.open('mask.png')
  36. # image_masked_alpha = Image.alpha_composite(panda.convert('RGBA'),mask)
  37. # image_masked = Image.composite(owl,panda,mask.convert('L'))
  38.  
  39. panda.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement