在python中操纵一组图像并将其放置在while背景上
我有一组带有操纵参数的图像(PNG),例如每个图像的缩放/旋转/x/y,我想将它们放置在具有上述操纵参数的固定大小(但足够大)空白图像上在python中操纵一组图像并将其放置在while背景上,python,image,python-imaging-library,Python,Image,Python Imaging Library,我有一组带有操纵参数的图像(PNG),例如每个图像的缩放/旋转/x/y,我想将它们放置在具有上述操纵参数的固定大小(但足够大)空白图像上 我想我可以用PIL手动完成这一切,但有些任务似乎很棘手,例如,45度旋转会丢失图像的一些区域,我可能必须先放入一个临时的较大尺寸的占位符,然后进行旋转,最后放入最终占位符。我们是否有一些好的示例/库可以使其更简单或更直观 下面是一个完整的示例,介绍如何使用pycairo创建背景图像并将多个图像粘贴到其中。它部分基于,但扩展为支持图像旋转 #!/usr/bin/
我想我可以用PIL手动完成这一切,但有些任务似乎很棘手,例如,45度旋转会丢失图像的一些区域,我可能必须先放入一个临时的较大尺寸的占位符,然后进行旋转,最后放入最终占位符。我们是否有一些好的示例/库可以使其更简单或更直观 下面是一个完整的示例,介绍如何使用pycairo创建背景图像并将多个图像粘贴到其中。它部分基于,但扩展为支持图像旋转
#!/usr/bin/python
# An example of how to create a background image and paste
# multiple images into it with pycairo.
import cairo
import math
import os
def pre_translate(ctx, tx, ty):
"""Translate a cairo context without taking into account its
scale and rotation"""
mat = ctx.get_matrix()
ctx.set_matrix(cairo.Matrix(mat[0],mat[1],
mat[2],mat[3],
mat[4]+tx,mat[5]+ty))
def draw_image(ctx, image, centerX, centerY, height, width, angle=0):
"""Draw a scaled image on a given context."""
image_surface = cairo.ImageSurface.create_from_png(image)
# calculate proportional scaling
img_height,img_width = (image_surface.get_height(),
image_surface.get_height())
scale_xy = min(1.0*width/img_width,1.0*height / img_height)
# scale, translate, and rotate the image around its center.
ctx.save()
ctx.rotate(angle)
ctx.translate(-img_width/2*scale_xy,-img_height/2*scale_xy)
ctx.scale(scale_xy, scale_xy)
pre_translate(ctx, centerX, centerY)
ctx.set_source_surface(image_surface)
ctx.paint()
ctx.restore()
width,height=512,512
surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, width, height)
cr = cairo.Context (surface)
pre_translate(cr, 0,0)
# Create the background
cr.rectangle(0,0,width,height)
cr.set_source_rgb(0,0,0.5)
cr.fill()
# Read an image
imagefile = 'tux.png'
if not os.path.exists(imagefile):
import urllib
urllib.urlretrieve('http://upload.wikimedia.org/wikipedia/commons/a/af/Tux.png', imagefile)
for x in range(0,width,64):
for y in range(0,height,64):
angle = 1.0*(x+y)/(width+height-128)*2*math.pi
draw_image(cr,
imagefile,
x+32,y+32,
64,64,
angle)
你可以用开罗。参见,例如,听起来开罗是一个向量库?甚至更低层的操作?也许最好在PIL中有一些例子来精确地旋转图像。通过任意坐标旋转、缩放和平移图像在某种意义上已经是一种向量操作了,那么为什么不呢?通过使用cairo,您会松脱什么?cairo中的内置旋转会很好地工作吗?e、 g.对于我上面提到的案例?