Python中的魔杖保存()方法
每当我们操作图像并想要保留图像以供进一步图像时,我们使用save()函数。 save()
函数将图像保存到文件或文件名中。它将最终处理的图像保存在磁盘中。
Syntax :
# image manipulation code
wand.image.save(file = file_object or
filename='filename.format')
Parameters :
It has only two parameter and takes only one at a time.
Parameter | Input Type | Description |
---|---|---|
file | file object | a file object to write to the file parameter |
filename | basestring | a filename object to write to the file parameter |
Now let’s see code to save image.
Example #1: Save image to the disk.
# import Image from wand.image module
from wand.image import Image
# read image using
with Image(filename ='koala.png') as img:
# manipulate image
img.rotate(90 * r)
# save final image after
img.save(filename ='final.png')
Output :
In output an image named koala.png will be saved in disk
Example #2:
We can save image to output stream using save() function too. For example, the following code converts koala.png image into JPEG, gzips it, and then saves it to koala.jpg.gz:
# import gzip
import gzip
# import Image from wand.image module
from wand.image import Image
# create gz compressed file
gz = gzip.open('koala.jpg.gz')
# read image using Image() function
with Image(filename ='pikachu.png') as img:
# get format of image
img.format = 'jpeg'
# save image to output stream using save() function
img.save(file = gz)
gz.close()
Output :
A compressed file named koala.jpg.gz get saved in disk
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.