{"cells":[{"metadata":{"_uuid":"806c01a47173435b2c7333978a30213f74df8f20"},"cell_type":"markdown","source":"The image data for this competition are too large to fit in memory in kernels. This kernel demonstrates how to access individual images in the zip archives without having to extract them or load the archive into memory."},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"a8a16a8f6f4db302ed361ace3693d5805452dfea"},"cell_type":"code","source":"from PIL import Image\nfrom zipfile import ZipFile","execution_count":2,"outputs":[]},{"metadata":{"_uuid":"1eaef785a48e32dd2893b30ca1832a8891ce3795"},"cell_type":"markdown","source":"First, we'll need a way to check what files are in the archive. The  zipfile module let's us look into the archive without loading the whole archive into memory or unpacking the entire archive.."},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"0f421f590e4cf7b12c66d6cc484743b8f3ca4e9a"},"cell_type":"code","source":"zip_path = '../input/train_jpg.zip'\nwith ZipFile(zip_path) as myzip:\n    files_in_zip = myzip.namelist()","execution_count":3,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"960f5222471a9e99749801d84ebb0494a2b6ee65"},"cell_type":"code","source":"files_in_zip[:5]","execution_count":12,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"efda92eba0b750f0cf55d267d43d9888251c2f6b"},"cell_type":"code","source":"len(files_in_zip)","execution_count":5,"outputs":[]},{"metadata":{"_uuid":"d976822aa163d85c0e3d34b675d4a8246d280d62"},"cell_type":"markdown","source":"Now that we have the list of files, we can access individual files by name without loading the whole archive into memory or unpacking the entire archive.."},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"f7ee08f9584b5cbef731067849c01381113d9f18"},"cell_type":"code","source":"with ZipFile(zip_path) as myzip:\n    with myzip.open(files_in_zip[3]) as myfile:\n        img = Image.open(myfile)","execution_count":8,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"59d6e3645c657b5a708b5e2337222fb6567f3f7d"},"cell_type":"code","source":"img.size","execution_count":11,"outputs":[]},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"17aa818889c461d081372e3ce92e65944d3f2c81"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.5","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}