Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pyvips
- # rotate an image with centre crop and reflection fill
- im = pyvips.Image.new_from_file("/home/john/pics/k2.jpg")
- # pyvips rotate always fills any new pixels which appear around the image edges
- # with 0 (black). To have something else in there, we need to expand the image
- # first, then crop it down to just the bit we need.
- # libvips 8.6 adds a "background" kwarg to similarity() and affine() that lets
- # you put some other constant in the new pixels, but for now embed() is the best
- # solution, especially if you want something like reflection fill.
- # This sounds inefficient, but libvips is demand-driven, so pixels which are
- # never used will never be calculated.
- # We expand the image enough that we can be sure we can never see black, no
- # matter the rotation angle or the image proportions.
- new_size = 2 * max(im.width, im.height)
- im2 = im.embed((new_size - im.width) / 2,
- (new_size - im.height) / 2,
- new_size,
- new_size,
- extend='mirror')
- im2 = im2.similarity(angle=20)
- # now crop a piece the size of the original from the centre of the rotated image
- im = im2.crop((im2.width - im.width) / 2,
- (im2.height - im.height) / 2,
- im.width,
- im.height)
- im.write_to_file("x.jpg")
Add Comment
Please, Sign In to add comment