{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import zipfile,os\n", "from PIL import Image\n", "\n", "#设置工作目录,其中放小闲下载的错题zip文件\n", "working_path = r\"临时文件/错题处理\"\n", "os.chdir(working_path)\n", "\n", "for zips in [f for f in os.listdir() if \".zip\" in f]:\n", " print(\"正在处理:\",zips)\n", " zf = zipfile.ZipFile(zips)\n", " picture_list = [f.filename for f in zf.filelist if \".png\" in f.filename]\n", " total_width = 0\n", " total_height = 0\n", " for p in picture_list:\n", " picture_file = zf.extract(p)\n", " current_pic = Image.open(picture_file)\n", " if total_width < current_pic.width:\n", " total_width = current_pic.width\n", " total_height += current_pic.height\n", " current_pic.close()\n", " canvas = Image.new(\"RGB\",(total_width,total_height),color=(255,255,255))\n", " position = 0\n", " for p in picture_list:\n", " current_pic = Image.open(p)\n", " canvas.paste(current_pic,(0,position))\n", " position += current_pic.height\n", " current_pic.close()\n", " os.remove(p)\n", " zf.close()\n", " canvas.save(zips[:-4]+\".png\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.7 ('base')", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "e4cce46d6be9934fbd27f9ca0432556941ea5bdf741d4f4d64c6cd7f8dfa8fba" } } }, "nbformat": 4, "nbformat_minor": 2 }