AttributeError:'function'对象没有属性'save' - Python PIL QR Code不保存

时间:2016-05-22 10:40:07

标签: python csv python-imaging-library qr-code attributeerror

我是编程新手,所以我为缺乏技术能力而道歉。

我正在尝试在python中创建一个qrcode生成器,但是,当我尝试增加文件名保存时的数字时,我收到此错误。

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()

2 个答案:

答案 0 :(得分:2)

错误在于:im = qr.make_image。您将im存储在对象qr的函数make_image中。由于您可以将函数存储在Python中的变量中,因此这是一种有效的语法。

所以,你没有调用函数make_image,只是存储它。它应该是im = qr.make_image()

答案 1 :(得分:0)

在你实现T. Claverie回答之后 - 你可能会在.save()中失败,因为你在查询字符串和整数。

您可以尝试更改以下行:

im.save("filename"+ count + ".png") 

是:

im.save("filename"+ str(count) + ".png")