Guest User

Untitled

a guest
Nov 21st, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import pyvips
  2.  
  3. # rotate an image with centre crop and reflection fill
  4.  
  5. im = pyvips.Image.new_from_file("/home/john/pics/k2.jpg")
  6.  
  7. # pyvips rotate always fills any new pixels which appear around the image edges
  8. # with 0 (black). To have something else in there, we need to expand the image
  9. # first, then crop it down to just the bit we need.
  10.  
  11. # libvips 8.6 adds a "background" kwarg to similarity() and affine() that lets
  12. # you put some other constant in the new pixels, but for now embed() is the best
  13. # solution, especially if you want something like reflection fill.
  14.  
  15. # This sounds inefficient, but libvips is demand-driven, so pixels which are
  16. # never used will never be calculated.
  17.  
  18. # We expand the image enough that we can be sure we can never see black, no
  19. # matter the rotation angle or the image proportions.
  20.  
  21. new_size = 2 * max(im.width, im.height)
  22. im2 = im.embed((new_size - im.width) / 2,
  23. (new_size - im.height) / 2,
  24. new_size,
  25. new_size,
  26. extend='mirror')
  27.  
  28. im2 = im2.similarity(angle=20)
  29.  
  30. # now crop a piece the size of the original from the centre of the rotated image
  31.  
  32. im = im2.crop((im2.width - im.width) / 2,
  33. (im2.height - im.height) / 2,
  34. im.width,
  35. im.height)
  36.  
  37. im.write_to_file("x.jpg")
Add Comment
Please, Sign In to add comment