Using a variable as a Save file name ~ im.save(type, '.png')

匆匆过客 提交于 2020-02-23 06:35:08

问题


This is what I'm inputting:

type = 'filename'
im.save(type, '.png')

and what this is supposed to do is save the file in a .png format but with the variable as the name, I'm just not 100% sure how the format/syntax is supposed to be laid out.

Is it the same as if I wanted to print the variable:

Print(type, "This shows up beside the type I think")

edit, Don't pay to much attention to the list, it's mostly there as an example. I not sure how to have a variable right beside a Plain text word like this im.save(type, 'PLAINTEXT')


回答1:


If you're trying to use PIL, please, use it like this:

file_name = 'my_file'
im.save( file_name + '.png' )

will result in my_file.png saved to your disk.




回答2:


I think what you want is string formatting:

type = 'filename'
im.save("{0}.png".format(type))

This will create filename.png.

Also 'Print' != 'print'.



来源:https://stackoverflow.com/questions/23765021/using-a-variable-as-a-save-file-name-im-savetype-png

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!