Coin163

首页 > AngularJS - 简介 - Nero

AngularJS - 简介 - Nero

相关标签: angular.js angularjs

up vote 0 down vote favorite Can I open an image inside a zip with PIL/Pillow without extracting it to disk first? python python-imaging-library zipfile pillow share | improve this question asked Oct 16 '15 at 8:57 Tor Klingberg 1,319 4 14 34 add a comment

|

2 Answers 2 active oldest votes up vote 2 down vote accepted The recent Pillow releases do not require .seek(): #!/usr/bin/env pythonimport sysfrom zipfile import ZipFilefrom PIL import Image # $ pip install pillowfilename = sys.argv[1]with ZipFile(filename) as archive:

for entry in archive.infolist():

with archive.open(entry) as file:

img = Image.open(file)

print(img.size, img.mode, len(img.getdata())) share | improve this answer answered Oct 16 '15 at 9:52 J.F. Sebastian 202k 48 396 589

Thanks, do you know since which version of Pillow? –

Tor Klingberg Oct 16 '15 at 10:40

@TorKlingberg: it fails on 2.7. It works on 2.9+. You could try to find a specific issue on github. –

J.F. Sebastian Oct 16 '15 at 10:41

The current documentation for Image.open still says "The file object must implement read(), seek(), and tell() methods" –

Tor Klingberg Oct 16 '15 at 10:43 1

@TorKlingberg: "must" is probably a bug in the docs. Here're release notes for 2.8 that explicitly says that Image wraps the non-seekable files itself –

J.F. Sebastian Oct 16 '15 at 10:46

That's good. Ubuntu 14.04 only has Pillow 2.3.0. I will look into setting up a virtualenv. –

Tor Klingberg Oct 16 '15 at 10:54 add a comment

|

up vote 2 down vote Pythons zipfile does provide a ZipFile.open() that returns a file object for a file inside the zip, and Pillow's Image.open() can take file object to open from. Unfortunately the zipfile object does not provide the seek() method that Image.open() needs. Instead read the image file into a string in RAM (if it is not too big), and use StringIO to get a file object for Image.open(): from zipfile import ZipFilefrom PIL import Imagefrom StringIO import StringIOarchive = ZipFile("file.zip", 'r')image_data = archive.read("image.png")fh = StringIO(image_data)img = Image.open(fh) share | improve this answer answered Oct 16 '15 at 8:57 Tor Klingberg 1,319 4 14 34 1

It may be better to use io.BytesIO instead of StringIO, though both seem to work in this case. –

Tor Klingberg Oct 16 '15 at 11:00 add a comment

|

Your Answer

draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged python python-imaging-library zipfile pillow or ask your own question.

原文

up vote 0 down vote favorite Can I open an image inside a zip with PIL/Pillow without extracting it to disk first python python-imaging-library zipfile pillow share | improve this

------分隔线----------------------------
相关推荐