AttributeError: 'function' object has no attribute 'save' - Python PIL QR Code not saving
AttributeError: 'function' object has no attribute 'save' - Python PIL QR Code not saving
我是编程新手,技术能力不足请见谅。
我正在尝试在 python 中创建二维码生成器,但是,当我尝试增加文件名保存的数字时,出现此错误。
Traceback (most recent call last):
File "/home/sam/Desktop/QR Code Gen/run.py", line 52, in <module>
purchase_code_fn()
File "/home/sam/Desktop/QR Code Gen/run.py", line 32, in purchase_code_fn
qr_code_fn()
File "/home/sam/Desktop/QR Code Gen/run.py", line 41, in qr_code_fn
im.save("filename"+ count + ".png")
AttributeError: 'function' object has no attribute 'save'
>>>
有没有办法纠正这个问题?
(请参阅下面的完整代码 - 它仍然是一个 WIP)
from qrcode import *
import csv
import time
active_csv = csv.writer(open("active_codes.csv", "wb"))
void_csv = csv.writer(open("void_codes.csv", "wb"))
active_csv.writerow([
('product_id'),
('code_id'),
('customer_name'),
('customer_email'),
('date_purchased'),
('date_expiry')])
void_csv.writerow([
('code_id'),
('customer_email'),
('date_expiry')])
count = 0
def purchase_code_fn():
global count
count =+ 1
customer_email = raw_input("Please enter your email: ")
product_id = raw_input("Which product would you like (1 - 5): ")
qr_code_fn()
def qr_code_fn():
qr = QRCode(version=5, error_correction=ERROR_CORRECT_M)
qr.add_data("asaasasa")
qr.make() # Generate the QRCode itself
# im contains a PIL.Image.Image object
im = qr.make_image
im.save("filename"+ count + ".png")
def restart_fn():
restart_prompt = raw_input("Would you like to purchase another code? : ").lower()
if restart_prompt == "yes" or restart_prompt == "y":
purchase_code_fn()
elif restart_prompt =="n" or restart_prompt == "no":
print("exit")
purchase_code_fn()
错误在这里:im = qr.make_image
。您正在将对象 qr 的函数 make_image
存储到 im
中。因为您可以将函数存储在 Python 中的变量中,所以这是一个有效的语法。
因此,您没有调用函数 make_image
,只是存储它。应该是 im = qr.make_image()
.
在您实施 T.Claverie 答案之后 - 您可能会在 .save() 中失败,因为您正在连接字符串和整数。
能否尝试更改以下行:
im.save("filename"+ count + ".png")
成为:
im.save("filename"+ str(count) + ".png")
我是编程新手,技术能力不足请见谅。
我正在尝试在 python 中创建二维码生成器,但是,当我尝试增加文件名保存的数字时,出现此错误。
Traceback (most recent call last):
File "/home/sam/Desktop/QR Code Gen/run.py", line 52, in <module>
purchase_code_fn()
File "/home/sam/Desktop/QR Code Gen/run.py", line 32, in purchase_code_fn
qr_code_fn()
File "/home/sam/Desktop/QR Code Gen/run.py", line 41, in qr_code_fn
im.save("filename"+ count + ".png")
AttributeError: 'function' object has no attribute 'save'
>>>
有没有办法纠正这个问题?
(请参阅下面的完整代码 - 它仍然是一个 WIP)
from qrcode import *
import csv
import time
active_csv = csv.writer(open("active_codes.csv", "wb"))
void_csv = csv.writer(open("void_codes.csv", "wb"))
active_csv.writerow([
('product_id'),
('code_id'),
('customer_name'),
('customer_email'),
('date_purchased'),
('date_expiry')])
void_csv.writerow([
('code_id'),
('customer_email'),
('date_expiry')])
count = 0
def purchase_code_fn():
global count
count =+ 1
customer_email = raw_input("Please enter your email: ")
product_id = raw_input("Which product would you like (1 - 5): ")
qr_code_fn()
def qr_code_fn():
qr = QRCode(version=5, error_correction=ERROR_CORRECT_M)
qr.add_data("asaasasa")
qr.make() # Generate the QRCode itself
# im contains a PIL.Image.Image object
im = qr.make_image
im.save("filename"+ count + ".png")
def restart_fn():
restart_prompt = raw_input("Would you like to purchase another code? : ").lower()
if restart_prompt == "yes" or restart_prompt == "y":
purchase_code_fn()
elif restart_prompt =="n" or restart_prompt == "no":
print("exit")
purchase_code_fn()
错误在这里:im = qr.make_image
。您正在将对象 qr 的函数 make_image
存储到 im
中。因为您可以将函数存储在 Python 中的变量中,所以这是一个有效的语法。
因此,您没有调用函数 make_image
,只是存储它。应该是 im = qr.make_image()
.
在您实施 T.Claverie 答案之后 - 您可能会在 .save() 中失败,因为您正在连接字符串和整数。
能否尝试更改以下行:
im.save("filename"+ count + ".png")
成为:
im.save("filename"+ str(count) + ".png")