Hướng dẫn read image from zip file python - đọc hình ảnh từ tệp zip python

Tôi đã thấy các biến thể của câu hỏi này, nhưng không phải trong bối cảnh chính xác này. Những gì tôi có là một tập tin gọi là 100 test.zip chứa 100 .jpg hình ảnh. Tôi muốn mở tệp này trong bộ nhớ và xử lý từng tệp thực hiện các hoạt động PIL. Phần còn lại của mã đã được viết, tôi chỉ muốn tập trung vào từ tệp zip đến hình ảnh PIL đầu tiên. Đây là những gì mã trông giống như bây giờ từ các đề xuất tôi đã thu thập được từ việc đọc các câu hỏi khác, nhưng nó không hoạt động. Các bạn có thể xem xét và giúp đỡ không?

import zipfile
from StringIO import StringIO
from PIL import Image

imgzip = open['100-Test.zip', 'rb']
z = zipfile.ZipFile[imgzip]
data = z.read[z.namelist[][0]]
dataEnc = StringIO[data]
img = Image.open[dataEnc]

print img

Nhưng tôi đang gặp lỗi này khi tôi chạy nó:

 IOError: cannot identify image file 

Giải pháp thay thế: Tôi đã thấy các nguồn khác nói rằng sử dụng điều này thay thế:

image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]

Nhưng vấn đề là tôi không mở một tệp, nó đã có trong bộ nhớ bên trong biến dữ liệu. Tôi cũng đã thử sử dụng

 IOError: cannot identify image file 
5 nhưng đã gặp lỗi này:

TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]

Bài viết này giải thích cách người ta có thể thực hiện các hoạt động khác nhau trên tệp zip bằng chương trình Python đơn giản.

Tệp zip là gì?

ZIP là một định dạng tệp lưu trữ hỗ trợ nén dữ liệu không mất. Bằng cách nén không mất, chúng tôi có nghĩa là thuật toán nén cho phép dữ liệu gốc được xây dựng lại hoàn hảo từ dữ liệu được nén. Vì vậy, một tệp zip là một tệp duy nhất chứa một hoặc nhiều tệp nén, cung cấp một cách lý tưởng để tạo các tệp lớn nhỏ hơn và giữ các tệp liên quan lại với nhau.

Tại sao chúng ta cần các tệp zip?

  • Để giảm yêu cầu lưu trữ.
  • Để cải thiện tốc độ truyền qua các kết nối tiêu chuẩn.

Để làm việc trên các tệp zip bằng Python, chúng tôi sẽ sử dụng mô -đun Python sẵn có gọi là ZipFile.

1. Trích xuất tệp zip

 IOError: cannot identify image file 
6
 IOError: cannot identify image file 
7
 IOError: cannot identify image file 
8
 IOError: cannot identify image file 
9

image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
0
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
1
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
2

image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
3
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
4
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
5
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
6
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
7

image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
6
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
0

image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
2
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
3
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
4
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
5

image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
6
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
8

image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
2
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
3
from zipfile import ZipFile
2
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
5

Chương trình trên trích xuất một tệp zip có tên là My_Python_files.zip, trong cùng thư mục như tập lệnh Python này. Đầu ra của chương trình trên có thể trông như thế này:
The output of above program may look like this:

Hãy để chúng tôi cố gắng hiểu mã trên theo từng mảnh:

  • from zipfile import ZipFile

    Zipfile là một lớp mô -đun zipfile để đọc và viết các tệp zip. Ở đây chúng tôi chỉ nhập lớp zipfile từ mô -đun zipfile.

  • with ZipFile[file_name, 'r'] as zip:

    Ở đây, một đối tượng zipfile được tạo bằng cách gọi Trình xây dựng ZipFile chấp nhận tên tệp và tham số chế độ.READ mode and name it as zip.

  • zip.printdir[]

    Phương thức printDir [] in một bảng nội dung cho kho lưu trữ. method prints a table of contents for the archive.

  • zip.extractall[]

    Phương thức extractall [] sẽ trích xuất tất cả các nội dung của tệp zip vào thư mục làm việc hiện tại. Bạn cũng có thể gọi phương thức trích xuất [] để trích xuất bất kỳ tệp nào bằng cách chỉ định đường dẫn của nó trong tệp zip. Ví dụ: method will extract all the contents of the zip file to the current working directory. You can also call extract[] method to extract any file by specifying its path in the zip file.
    For example:

    zip.extract['python_files/python_wiki.txt']

    Điều này sẽ chỉ trích xuất các tệp được chỉ định.

    Nếu bạn muốn đọc một số tệp cụ thể, bạn có thể đi như thế này:

    data = zip.read[name_of_file_to_read]

2. Viết vào tệp zip

Xem xét một thư mục [thư mục] với định dạng như vậy:

Ở đây, chúng ta sẽ cần phải thu thập dữ liệu toàn bộ thư mục và các hướng phụ của nó để có được danh sách tất cả các đường dẫn tệp trước khi ghi chúng vào tệp zip.
The following program does this by crawling the directory to be zipped:

 IOError: cannot identify image file 
6
 IOError: cannot identify image file 
7
 IOError: cannot identify image file 
8
 IOError: cannot identify image file 
9

image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
0
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
1
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
2

Chương trình trên trích xuất một tệp zip có tên là My_Python_files.zip, trong cùng thư mục như tập lệnh Python này. Đầu ra của chương trình trên có thể trông như thế này:

Hãy để chúng tôi cố gắng hiểu mã trên theo từng mảnh:

Zipfile là một lớp mô -đun zipfile để đọc và viết các tệp zip. Ở đây chúng tôi chỉ nhập lớp zipfile từ mô -đun zipfile.

Ở đây, một đối tượng zipfile được tạo bằng cách gọi Trình xây dựng ZipFile chấp nhận tên tệp và tham số chế độ.

Phương thức printDir [] in một bảng nội dung cho kho lưu trữ.

zip.printdir[]
6
zip.extractall[]
1

Phương thức extractall [] sẽ trích xuất tất cả các nội dung của tệp zip vào thư mục làm việc hiện tại. Bạn cũng có thể gọi phương thức trích xuất [] để trích xuất bất kỳ tệp nào bằng cách chỉ định đường dẫn của nó trong tệp zip. Ví dụ:

Điều này sẽ chỉ trích xuất các tệp được chỉ định.

Nếu bạn muốn đọc một số tệp cụ thể, bạn có thể đi như thế này:

image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8
with ZipFile[file_name, 'r'] as zip:
3
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
1
zip.extract['python_files/python_wiki.txt']
4

image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
2
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
3
zip.extract['python_files/python_wiki.txt']
8
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
5

image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8
with ZipFile[file_name, 'r'] as zip:
7
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
0
with ZipFile[file_name, 'r'] as zip:
9
data = zip.read[name_of_file_to_read]
4

zip.printdir[]
1
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
2
data = zip.read[name_of_file_to_read]
7

Các

zip.printdir[]
1
with ZipFile[file_name, 'r'] as zip:
7
 IOError: cannot identify image file 
08
with ZipFile[file_name, 'r'] as zip:
9
data = zip.read[name_of_file_to_read]
4

zip.printdir[]
6
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
6
 IOError: cannot identify image file 
13
 IOError: cannot identify image file 
08
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
5

image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
2
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
3
 IOError: cannot identify image file 
19
 IOError: cannot identify image file 
20

 IOError: cannot identify image file 
21
 IOError: cannot identify image file 
222.

image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8
 IOError: cannot identify image file 
28

Đầu ra của chương trình trên trông như thế này:

Hãy để chúng tôi cố gắng hiểu mã ở trên bằng cách chia thành các mảnh:

  •  IOError: cannot identify image file 
    
    0

    Trước hết, để có được tất cả các đường dẫn tệp trong thư mục của chúng tôi, chúng tôi đã tạo chức năng này sử dụng phương thức Os.Walk [] & NBSP; Trong mỗi lần lặp, tất cả các tệp có trong thư mục đó được thêm vào một danh sách có tên File_path. Trong phần cuối, chúng tôi trả về tất cả các đường dẫn tệp.os.walk[] method. In each iteration, all files present in that directory are appended to a list called file_paths.
    In the end, we return all the file paths.

  •  IOError: cannot identify image file 
    
    1

    Ở đây chúng tôi chuyển thư mục để được nén đến hàm GET_ALL_FILE_PATHS [] và có được danh sách chứa tất cả các đường dẫn tệp.get_all_file_paths[] function and obtain a list containing all file paths.

  •  IOError: cannot identify image file 
    
    2

    Ở đây, chúng tôi tạo một đối tượng zipfile trong chế độ ghi lần này.

  •  IOError: cannot identify image file 
    
    3

    Ở đây, chúng tôi viết tất cả các tệp vào tệp zip từng một bằng phương thức ghi. write method.

3. Nhận tất cả thông tin về tệp zip

 IOError: cannot identify image file 
6
 IOError: cannot identify image file 
7
 IOError: cannot identify image file 
8
 IOError: cannot identify image file 
9

 IOError: cannot identify image file 
8
 IOError: cannot identify image file 
34

image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
0
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
1
 IOError: cannot identify image file 
37

image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
3
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
4
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
5
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
6
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
7

image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8
with ZipFile[file_name, 'r'] as zip:
7
 IOError: cannot identify image file 
45
with ZipFile[file_name, 'r'] as zip:
9
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
6
 IOError: cannot identify image file 
48

zip.printdir[]
6
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
2
 IOError: cannot identify image file 
51

zip.printdir[]
6
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
2
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
3
 IOError: cannot identify image file 
55
 IOError: cannot identify image file 
56
 IOError: cannot identify image file 
57
 IOError: cannot identify image file 
58
 IOError: cannot identify image file 
59
 IOError: cannot identify image file 
60

zip.printdir[]
6
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
2
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
3
 IOError: cannot identify image file 
64
 IOError: cannot identify image file 
56
 IOError: cannot identify image file 
57__

zip.printdir[]
6
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
2
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
3
 IOError: cannot identify image file 
74
 IOError: cannot identify image file 
56
 IOError: cannot identify image file 
57
 IOError: cannot identify image file 
77

zip.printdir[]
6
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
2
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
3
 IOError: cannot identify image file 
81
 IOError: cannot identify image file 
56
 IOError: cannot identify image file 
57__

zip.printdir[]
6
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
2
TypeError: unbound method read[] must be called with StringIO instance as 
first argument [got str instance instead]
3
 IOError: cannot identify image file 
91
 IOError: cannot identify image file 
56
 IOError: cannot identify image file 
57__

Đầu ra của chương trình trên có thể trông như thế này:

 IOError: cannot identify image file 
4

Ở đây, phương thức InfoList [] tạo một thể hiện của lớp zipinfo chứa tất cả thông tin về tệp zip. Chúng ta có thể truy cập tất cả thông tin như ngày sửa đổi cuối cùng của tệp, tên tệp, hệ thống trên đó các tệp được tạo, phiên bản zip, kích thước của tệp ở dạng nén và không nén, v.v.infolist[] method creates an instance of ZipInfo class which contains all the information about the zip file.
We can access all information like last modification date of files, file names, system on which files were created, Zip version, size of files in compressed and uncompressed form, etc.

Bài viết này & nbsp; được đóng góp bởi Nikhil Kumar. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết bằng Write.GeekSforGeek.org hoặc gửi bài viết của bạn. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác.Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.

Vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác, hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên.


Làm cách nào để đọc hình ảnh từ tệp zip trong Python?

Sử dụng zipfile để có được định dạng zipextfile ..
Sử dụng Pil. Hình ảnh để chuyển đổi zipextfile thành cấu trúc dữ liệu như hình ảnh ..
Chuyển đổi Pil. hình ảnh thành mảng numpy ..

Làm cách nào để trích xuất một tệp zip trong Python?

Python3.# vào một vị trí cụ thể.Nhập mô -đun zipfile Tạo đối tượng tệp ZIP bằng lớp ZipFile.Gọi phương thức trích xuất [] trên đối tượng tệp zip và truyền tên của tệp để được trích xuất và đường dẫn nơi tệp cần được trích xuất và trích xuất tệp cụ thể có trong zip.Call the extract[] method on the zip file object and pass the name of the file to be extracted and the path where the file needed to be extracted and Extracting the specific file present in the zip.

Làm cách nào để đọc một tệp zip trong gấu trúc?

Phương thức số 1: Sử dụng nén = zip trong pandas.read_csv [] phương thức.Bằng cách gán đối số nén trong phương thức read_csv [] làm zip, thì pandas trước tiên sẽ giải nén zip và sau đó sẽ tạo dataFrame từ tệp CSV có trong tệp bị nén.Using compression=zip in pandas. read_csv[] method. By assigning the compression argument in read_csv[] method as zip, then pandas will first decompress the zip and then will create the dataframe from CSV file present in the zipped file.

Làm cách nào để trích xuất một tệp từ tệp zip?

Để giải nén một tệp hoặc thư mục duy nhất, hãy mở thư mục có khóa, sau đó kéo tệp hoặc thư mục từ thư mục bị nén đến một vị trí mới.Để giải nén tất cả các nội dung của thư mục có khóa, nhấn và giữ [hoặc nhấp chuột phải] thư mục, chọn Trích xuất tất cả, sau đó làm theo hướng dẫn.open the zipped folder, then drag the file or folder from the zipped folder to a new location. To unzip all the contents of the zipped folder, press and hold [or right-click] the folder, select Extract All, and then follow the instructions.

Bài Viết Liên Quan

Chủ Đề