Python如何将图像保存到不同的目录?

问题描述

import os
import glob
from PIL import Image

files = glob.glob('/Users/mac/PycharmProjects/crop001/1/*.jpg')

for f in files:
    img = Image.open(f)
    img_resize = img.resize((int(img.width / 2),int(img.height / 2)))
    title,ext = os.path.splitext(f)
    img_resize.save(title + '_half' + ext)

我想将新图片保存到

"/Users/mac/PycharmProjects/crop001/1/11/*.jpg" 

不是

"/Users/mac/PycharmProjects/crop001/1/*.jpg"

任何帮助将不胜感激!

解决方法

您可以通过更改此处的 //! A dummy XError handler which just skips errors static int xErrorDummyHandler (Display*,XErrorEvent* ) { return 0; } ... Window aWindow = ...; Display* aDisp = ...; GLXFBConfig anFBConfig = ...; bool toDebugContext = false; GLXContext aGContext = NULL const char* aGlxExts = glXQueryExtensionsString (aDisp,aVisInfo.screen); if (!checkGlExtension (aGlxExts,"GLX_ARB_create_context_profile")) { std::cerr << "GLX_ARB_create_context_profile is NOT supported\n"; return; } // Replace default XError handler to ignore errors. // Warning - this is global for all threads! typedef int (*xerrorhandler_t)(Display*,XErrorEvent* ); xerrorhandler_t anOldHandler = XSetErrorHandler(xErrorDummyHandler); typedef GLXContext (*glXCreateContextAttribsARB_t)(Display* dpy,GLXFBConfig config,GLXContext share_context,Bool direct,const int* attrib_list); glXCreateContextAttribsARB_t aCreateCtxProc = (glXCreateContextAttribsARB_t )glXGetProcAddress((const GLubyte* )"glXCreateContextAttribsARB"); int aCoreCtxAttribs[] = { GLX_CONTEXT_MAJOR_VERSION_ARB,3,GLX_CONTEXT_MINOR_VERSION_ARB,2,GLX_CONTEXT_PROFILE_MASK_ARB,GLX_CONTEXT_CORE_PROFILE_BIT_ARB,GLX_CONTEXT_FLAGS_ARB,toDebugContext ? GLX_CONTEXT_DEBUG_BIT_ARB : 0,0 }; // try to create a Core Profile of highest OpenGL version (up to 4.6) for (int aLowVer4 = 6; aLowVer4 >= 0 && aGContext == NULL; --aLowVer4) { aCoreCtxAttribs[1] = 4; aCoreCtxAttribs[3] = aLowVer4; aGContext = aCreateCtxProc (aDisp,anFBConfig,NULL,True,aCoreCtxAttribs); } for (int aLowVer3 = 3; aLowVer3 >= 2 && aGContext == NULL; --aLowVer3) { aCoreCtxAttribs[1] = 3; aCoreCtxAttribs[3] = aLowVer3; aGContext = aCreateCtxProc (aDisp,aCoreCtxAttribs); } bool isCoreProfile = aGContext != NULL; if (!isCoreProfile) { std::cerr << "glXCreateContextAttribsARB() failed to create Core Profile\n"; } // try to create Compatible Profile if (aGContext == NULL) { int aCtxAttribs[] = { GLX_CONTEXT_FLAGS_ARB,0 }; aGContext = aCreateCtxProc (aDisp,aCtxAttribs); } XSetErrorHandler (anOldHandler); // fallback to glXCreateContext() as last resort if (aGContext == NULL) { aGContext = glXCreateContext (aDisp,aVis.get(),GL_TRUE); if (aGContext == NULL) { std::cerr << "glXCreateContext() failed\n"; } } 参数将处理后的图像保存到您的首选目录 (/Users/mac/PycharmProjects/crop001/1/11/*.jpg)。

假设您仍想保存文件名中带有 img_resize.save() 后缀的处理过的图像。所以最后的代码在这里:

_half

我在这里使用的所有函数文档都可以在这里找到: