【问题标题】:How Can I Make This Python Script Work With Python 3?如何使这个 Python 脚本与 Python 3 一起工作?
【发布时间】:2012-08-08 20:38:34
【问题描述】:

我下载了这个脚本来帮助我转换一些 PNG。然而,从 2003 年开始,我第一次尝试运行它时,它给了我异常语法的错误。我设法解决了这个问题并再次运行它。然后它给了我打印语法的错误。我也修复了这些。现在我完全不知道除了脚本不起作用之外发生了什么。

脚本是:

from struct import *
from zlib import *
import stat
import sys
import os

def getNormalizedPNG(filename):
    pngheader = "\x89PNG\r\n\x1a\n"

    file = open(filename, "rb")
    oldPNG = file.read()
    file.close()

    if oldPNG[:8] != pngheader:
        return None

    newPNG = oldPNG[:8]

    chunkPos = len(newPNG)

    # For each chunk in the PNG file    
    while chunkPos < len(oldPNG):

        # Reading chunk
        chunkLength = oldPNG[chunkPos:chunkPos+4]
        chunkLength = unpack(">L", chunkLength)[0]
        chunkType = oldPNG[chunkPos+4 : chunkPos+8]
        chunkData = oldPNG[chunkPos+8:chunkPos+8+chunkLength]
        chunkCRC = oldPNG[chunkPos+chunkLength+8:chunkPos+chunkLength+12]
        chunkCRC = unpack(">L", chunkCRC)[0]
        chunkPos += chunkLength + 12

        # Parsing the header chunk
        if chunkType == "IHDR":
            width = unpack(">L", chunkData[0:4])[0]
            height = unpack(">L", chunkData[4:8])[0]

        # Parsing the image chunk
        if chunkType == "IDAT":
            try:
                # Uncompressing the image chunk
                bufSize = width * height * 4 + height
                chunkData = decompress( chunkData, -8, bufSize)

            except Exception as e:
                # The PNG image is normalized
                return None

            # Swapping red & blue bytes for each pixel
            newdata = ""
            for y in xrange(height):
                i = len(newdata)
                newdata += chunkData[i]
                for x in xrange(width):
                    i = len(newdata)
                newdata += chunkData[i+2]
                    newdata += chunkData[i+1]
                    newdata += chunkData[i+0]
                    newdata += chunkData[i+3]

            # Compressing the image chunk
            chunkData = newdata
            chunkData = compress( chunkData )
            chunkLength = len( chunkData )
            chunkCRC = crc32(chunkType)
            chunkCRC = crc32(chunkData, chunkCRC)
            chunkCRC = (chunkCRC + 0x100000000) % 0x100000000

        # Removing CgBI chunk        
        if chunkType != "CgBI":
            newPNG += pack(">L", chunkLength)
            newPNG += chunkType
            if chunkLength > 0:
                newPNG += chunkData
            newPNG += pack(">L", chunkCRC)

        # Stopping the PNG file parsing
        if chunkType == "IEND":
            break

    return newPNG

def updatePNG(filename):
    data = getNormalizedPNG(filename)
    if data != None:
        file = open(filename, "wb")
        file.write(data)
        file.close()
        return True
    return data

def getFiles(base):
    global _dirs
    global _pngs
    if base == ".":
        _dirs = []
        _pngs = []

    if base in _dirs:
        return

    files = os.listdir(base)
    for file in files:
        filepath = os.path.join(base, file)
        try:
            st = os.lstat(filepath)
        except os.error:
            continue

        if stat.S_ISDIR(st.st_mode):
            if not filepath in _dirs:
                getFiles(filepath)
                _dirs.append( filepath )

        elif file[-4:].lower() == ".png":
            if not filepath in _pngs:
                _pngs.append( filepath )

    if base == ".":
        return _dirs, _pngs

print ("iPhone PNG Images Normalizer v1.0")
print (" ")
print ("[+] Searching PNG files..."),
dirs, pngs = getFiles(".")
print ("ok")

if len(pngs) == 0:
    print (" ")
    print ("[!] Alert: There are no PNG files found. Move this python file to the folder that contains the PNG files to normalize.")
    exit()

print (" ")
print (" -  %d PNG files were found at this folder (and subfolders).") % len(pngs)
print (" ")
while True:
    normalize = raw_input("[?] Do you want to normalize all images (Y/N)? ").lower()
    if len(normalize) > 0 and (normalize[0] == "y" or normalize[0] == "n"):
        break

normalized = 0
if normalize[0] == "y":
    for ipng in xrange(len(pngs)):
        perc = (float(ipng) / len(pngs)) * 100.0
        print ("%.2f%% %s") % (perc, pngs[ipng])
        if updatePNG(pngs[ipng]):
            normalized += 1
print (" ")
print ("[+] %d PNG files were normalized.") % normalized

现在当我在 DOS 窗口中运行它时,我得到了这个错误:

C:\wamp\www\py>ipin.py
iPhone PNG Images Normalizer v1.0

[+] Searching PNG files...
ok

 -  %d PNG files were found at this folder (and subfolders).
Traceback (most recent call last):
  File "C:\wamp\www\py\ipin.py", line 158, in <module>
    print (" -  %d PNG files were found at this folder (and subfolders).") % len(pngs)
TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

我该怎么办?

【问题讨论】:

  • 你试过在上面运行2to3吗?你也可以安装 2.7 版的 python,这是大多数人无论如何都使用的。
  • 是否有可能获得一些关于有多少人使用每个版本的 Python 的记录统计数据?
  • @NoctisSkytower 据我所知,不,我只是根据轶事证据说话,因为我从未真正遇到过使用 Python 3 的人,而且这里似乎很少有问题使用 Python 3。

标签: python python-3.x


【解决方案1】:

您可能希望将 % 运算符移到括号内。

print (" -  %d PNG files were found at this folder (and subfolders)." % len(pngs))

【讨论】:

  • 现在我得到这些错误:$ ipin.py ./ipin.py: line 25: from: command not found ./ipin.py: line 26: from: command not found ./ipin.py: line 27: import: command not found ./ipin.py: line 28: import: command not found ./ipin.py: line 29: import: command not found ./ipin.py: line 31: syntax error near unexpected token (' ./ipin.py: line 31: ` def getNormalizedPNG(filename):''
  • 对不起,我真的很少使用markdown:/无论如何,使用2to3,我得到了这个错误:/bin/env:python:没有这样的文件或目录
【解决方案2】:

在 Python 2 中,纯字符串文字是字节字符串,而在 Python 3 中,它们是 Unicode 字符串。如果要编写字节串文字,请使用 b 前缀,例如b"\x89PNG\r\n\x1a\n"。 Python 3 在将字节串与 Unicode 字符串混合时非常严格。

其他区别在于,在 Python 3 中,print 现在是普通函数,而不是语句,range 函数返回生成器,就像 Python 2 中的 xrange,而 input 就像 @987654326 @ 在 Python 2 中(在 Python 3 中没有与 Python 2 的 input 函数等效的函数 - 因为它被认为不安全而被删除)。

这是我将代码转换为 Python 3 的尝试(顺便说一下,不鼓励使用 from something import *,因为它可能会无意中隐藏许多名称;仅导入您希望使用的那些名称):

from struct import *
from zlib import *
import stat
import sys
import os

def getNormalizedPNG(filename):
    pngheader = b"\x89PNG\r\n\x1a\n"

    file = open(filename, "rb")
    oldPNG = file.read()
    file.close()

    if oldPNG[:8] != pngheader:
        return None

    newPNG = oldPNG[:8]

    chunkPos = len(newPNG)

    # For each chunk in the PNG file    
    while chunkPos < len(oldPNG):

        # Reading chunk
        chunkLength = oldPNG[chunkPos:chunkPos+4]
        chunkLength = unpack(">L", chunkLength)[0]
        chunkType = oldPNG[chunkPos+4 : chunkPos+8]
        chunkData = oldPNG[chunkPos+8:chunkPos+8+chunkLength]
        chunkCRC = oldPNG[chunkPos+chunkLength+8:chunkPos+chunkLength+12]
        chunkCRC = unpack(">L", chunkCRC)[0]
        chunkPos += chunkLength + 12

        # Parsing the header chunk
        if chunkType == b"IHDR":
            width = unpack(">L", chunkData[0:4])[0]
            height = unpack(">L", chunkData[4:8])[0]

        # Parsing the image chunk
        if chunkType == b"IDAT":
            try:
                # Uncompressing the image chunk
                bufSize = width * height * 4 + height
                chunkData = decompress( chunkData, -8, bufSize)

            except Exception as e:
                # The PNG image is normalized
                return None

            # Swapping red & blue bytes for each pixel
            newdata = b""
            for y in range(height):
                i = len(newdata)
                newdata += chunkData[i]
                for x in range(width):
                    i = len(newdata)
                    newdata += chunkData[i+2]
                    newdata += chunkData[i+1]
                    newdata += chunkData[i+0]
                    newdata += chunkData[i+3]

            # Compressing the image chunk
            chunkData = newdata
            chunkData = compress( chunkData )
            chunkLength = len( chunkData )
            chunkCRC = crc32(chunkType)
            chunkCRC = crc32(chunkData, chunkCRC)
            chunkCRC = (chunkCRC + 0x100000000) % 0x100000000

        # Removing CgBI chunk        
        if chunkType != b"CgBI":
            newPNG += pack(">L", chunkLength)
            newPNG += chunkType
            if chunkLength > 0:
                newPNG += chunkData
            newPNG += pack(">L", chunkCRC)

        # Stopping the PNG file parsing
        if chunkType == b"IEND":
            break

    return newPNG

def updatePNG(filename):
    data = getNormalizedPNG(filename)
    if data != None:
        file = open(filename, "wb")
        file.write(data)
        file.close()
        return True
    return data

def getFiles(base):
    global _dirs
    global _pngs
    if base == ".":
        _dirs = []
        _pngs = []

    if base in _dirs:
        return

    files = os.listdir(base)
    for file in files:
        filepath = os.path.join(base, file)
        try:
            st = os.lstat(filepath)
        except os.error:
            continue

        if stat.S_ISDIR(st.st_mode):
            if not filepath in _dirs:
                getFiles(filepath)
                _dirs.append( filepath )

        elif file[-4:].lower() == ".png":
            if not filepath in _pngs:
                _pngs.append( filepath )

    if base == ".":
        return _dirs, _pngs

print ("iPhone PNG Images Normalizer v1.0")
print (" ")
print ("[+] Searching PNG files..."),
dirs, pngs = getFiles(".")
print ("ok")

if len(pngs) == 0:
    print (" ")
    print ("[!] Alert: There are no PNG files found. Move this python file to the folder that contains the PNG files to normalize.")
    exit()

print (" ")
print (" -  %d PNG files were found at this folder (and subfolders)." % len(pngs))
print (" ")
while True:
    normalize = input("[?] Do you want to normalize all images (Y/N)? ").lower()
    if len(normalize) > 0 and (normalize[0] == "y" or normalize[0] == "n"):
        break

normalized = 0
if normalize[0] == "y":
    for ipng in range(len(pngs)):
        perc = (float(ipng) / len(pngs)) * 100.0
        print ("%.2f%% %s" % (perc, pngs[ipng]))
        if updatePNG(pngs[ipng]):
            normalized += 1
print (" ")
print ("[+] %d PNG files were normalized." % normalized)

【讨论】:

  • 我明白了:$ ipin_py.py ./ipin_py.py: 第 1 行:来自:找不到命令 ./ipin_py.py: 第 2 行:来自:找不到命令 ./ipin_py.py:第 3 行:导入:未找到命令 ./ipin_py.py:第 4 行:导入:未找到命令 ./ipin_py.py:第 5 行:导入:未找到命令 ./ipin_py.py:第 7 行:意外标记附近出现语法错误(' ./ipin_py.py: line 7: def getNormalizedPNG(文件名):'
  • $ 是什么?你还在 Windows 上运行它吗?我问是因为在我看来它没有将其识别为 Python 脚本。如果您在没有 shebang 行 #!/usr/bin/python 的情况下在 Linux 上运行它,则可能会发生这种情况(shebang 的详细信息可能会有所不同)。
【解决方案3】:

当你尝试 % str(len(pngs)) 时会发生什么?

【讨论】:

  • 这不是一个答案(显然不是一个答案);请删除它。
猜你喜欢
  • 2012-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 2016-01-09
相关资源
最近更新 更多