我正在尝试运行一个调用PIL的Python脚本(通过Pillow)。该脚本在我的MacBook上完美运行,但我希望它也可以在Windows上运行。我收到“解码器zip不可用”的错误(参见下面的完整输出)。搜索网络让我下载了zlib并重新安装Pillow(如this question中所述)。不幸的是,我得到了同样的错误。
我的问题是:
如何/在哪里安装zlib?我下载了一个zip文件,在zlib网站上找不到任何方向,也没有在其他地方找到。我尝试将解压缩的zlib文件夹放在带有Pillow的Python site-packages文件夹中。
我是否遗漏了会提供错误消息的其他内容?我很惊讶安装Pillow有多困难。
使用Windows 7,Python 2.7,目前从https://pypi.python.org/pypi/Pillow/2.7.0
的zip文件安装Pillow 2.7.0脚本:(基本上它应该在窗口中打开图像并允许用户在图像上绘制矩形)。
import Tkinter as tk
from Tkinter import *
import tkMessageBox
from PIL import ImageTk, Image
import subprocess
crop_regions = []
path = "D:/Temp/OCR_test/K111PS_V5_2"
class ImageCanvas(Frame):
# This class creates a Tkinter canvas and displays the first video frame grab
def __init__(self, master):
Frame.__init__(self, master)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
self.canvas = Canvas(self, width=720, height=480, bd=0, highlightthickness=0)
self.canvas.grid(row=0, column=0, sticky='nsew', padx=4, pady=4)
class ImgTk(tk.Tk):
# This function asks the user to select portions of the first video frame grab that contain data to OCR
# Coordinates of data field locations are saved as a global variable to be passed to the rest of the script
def __init__(self):
tk.Tk.__init__(self)
tkMessageBox.showinfo(
message="On the following image, draw rectangles over the desired fields: \nlatitude, longitude, date, time and other \nin that order. \nClose the image window when finished")
self.main = ImageCanvas(self)
self.main.grid(row=0, column=0, sticky='nsew')
self.c = self.main.canvas
self.currentImage = {}
self.load_imgfile(path + '/images/0029.png')
self.c.bind('<ButtonPress-1>', self.click_down)
self.c.bind('<B1-Motion>', self.click_drag)
self.c.bind('<ButtonRelease-1>', self.rectangles)
def load_imgfile(self, filename):
self.img = Image.open(filename)
self.currentImage['data'] = self.img
self.photo = ImageTk.PhotoImage(self.img)
self.c.xview_moveto(0)
self.c.yview_moveto(0)
self.c.create_image(0, 0, image=self.photo, anchor='nw', tags='img')
self.c.config(scrollregion=self.c.bbox('all'))
self.currentImage['photo'] = self.photo
self.title("Draw rectangles")
def click_down(self, event):
self.anchor = (event.widget.canvasx(event.x),
event.widget.canvasy(event.y))
self.item = None
def click_drag(self, event):
selected_box = self.anchor + (event.widget.canvasx(event.x), event.widget.canvasy(event.y))
if self.item is None:
self.item = event.widget.create_rectangle(selected_box, outline="red3", width=2.1)
else:
event.widget.coords(self.item, *selected_box)
def rectangles(self, event):
if self.item:
self.click_drag(event)
box = tuple((int(round(v)) for v in event.widget.coords(self.item)))
roi = self.currentImage['data'].crop(box) # region of interest
lat = roi.size[0], ':', roi.size[1], ':', box[0], ':', box[1]
lat = str(lat)
lat = lat.replace(',', '')
lat = lat.replace("'", '')
lat = lat.replace(' ', '')
global crop_regions
crop_regions.append(lat)
app = ImgTk()
app.mainloop()
错误讯息:
Traceback (most recent call last):
File "D:/Scripts/DaileyScripts/Python/OCR/PIL_test.py", line 81, in <module>
app = ImgTk()
File "D:/Scripts/DaileyScripts/Python/OCR/PIL_test.py", line 37, in __init__
self.load_imgfile(path + '/images/0029.png')
File "D:/Scripts/DaileyScripts/Python/OCR/PIL_test.py", line 47, in load_imgfile
self.photo = ImageTk.PhotoImage(self.img)
File "build\bdist.win-amd64\egg\PIL\ImageTk.py", line 115, in __init__
self.paste(image)
File "build\bdist.win-amd64\egg\PIL\ImageTk.py", line 165, in paste
im.load()
File "build\bdist.win-amd64\egg\PIL\ImageFile.py", line 200, in load
d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
File "build\bdist.win-amd64\egg\PIL\Image.py", line 417, in _getdecoder
raise IOError("decoder %s not available" % decoder_name)
IOError: decoder zip not available
模块似乎不起作用,即我可以毫无错误地调用它们。
答案 0 :(得分:0)
zlib在NDK中为already there。
我遇到了同样的问题。我有一个音符8(三星)。只需转到GitHub并从那里安装zlib,之后您就可以安装枕头了。