Python Tools of PIL.Image Library Quick multi-stitching

We especially terminal automated testing, automated testing step screenshot save operation is often required in the test, but one
Ge case more pictures, see a lot of trouble to provide a train of thought here, and that is the stitching together multiple images,
inflicted a picture, you can synthesize N * M ** grids, if the picture is not enough, automatically fill in the blank white **

Specific code as follows

Python `` `
class ImageUtils (Object):
@staticmethod
DEF image_compose (imgDir, picwidth, picHigh, savepath, Row =. 1, column =. 1):
'' '
: param imgDir: Image path
: param picWidth: Broadband zoom view
: param picHigh: zoom view height
: param savePath: save path
: param row: x line
: param column: x columns
: return:
'' '
IMAGES_FORMAT = [' .png ',' .PNG ',' .jpg ', ".JPG "] # image format
# get the name of all the pictures in the photo Gallery address
image_names = [name for name in os.listdir (imgDir) for Item in IMAGES_FORMAT IF
os.path.splitext (name) [1] == Item]
# define image stitching function
to_image = Image.new ( 'RGB', (column * picWidth,row * picHigh)) # Create a new map
# traversal cycle, the position corresponding to each image pasted to the order
from_image = None
for y in range(1, row + 1):
for x in range(1, column + 1):
if column * (y - 1) + x - 1 > len(image_names) - 1:
from_image = Image.new('RGB', (picWidth, picHigh), (255, 255, 255))
else:
from_image = Image.open(imgDir + image_names[column * (y - 1) + x - 1]).resize(
(picWidth, picHigh), Image.ANTIALIAS)
to_image.paste(from_image, ((x - 1) * picWidth, (y - 1) * picHigh))

return to_image.save(savePath) # 保存新图

if __name__ == "__main__":
ImageUtils.image_compose(os.getcwd() + "/image/" ,360,640,'111.jpg',4,4)
```

Look at the final operating results, below is my realization of a game with an auxiliary plug-in script, save all the steps auxiliary operations, and finally the synthesis of a picture,

 

 More technical exchange, please add QQ group: 1085210541

Guess you like

Origin www.cnblogs.com/luoman/p/12584320.html