Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python通过文件系统调整和存储图像文件夹的大小_Python_Postgresql - Fatal编程技术网

使用python通过文件系统调整和存储图像文件夹的大小

使用python通过文件系统调整和存储图像文件夹的大小,python,postgresql,Python,Postgresql,我想调整文件夹中的图像大小,并使用postgres作为数据库和jupyter笔记本将它们存储到带有文件系统的数据库中。我正在使用调整大小功能调整图像大小,然后保存到数据库中,但调整大小功能似乎不起作用,无法理解我的错误 subject= input("Enter Subject Name:") cursor.execute("DROP TABLE IF EXISTS %s"%(subject)) cursor.execute( ""&q

我想调整文件夹中的图像大小,并使用postgres作为数据库和jupyter笔记本将它们存储到带有文件系统的数据库中。我正在使用调整大小功能调整图像大小,然后保存到数据库中,但调整大小功能似乎不起作用,无法理解我的错误

subject= input("Enter Subject Name:")
cursor.execute("DROP TABLE IF EXISTS %s"%(subject))
cursor.execute( """CREATE TABLE %s (ID SERIAL PRIMARY KEY, PHOTO BYTEA NOT NULL)"""%(subject))
conn.commit()

userfilepath=input("enter file path:")
dirs = os.listdir( userfilepath )


def resize():
    for item in dirs:
        if os.path.isfile(userfilepath+item):
            im = Image.open(userfilepath+item)
            f, e = os.userfilepath.splitext(userfilepath+item)
            imResize = im.resize((200,200), Image.ANTIALIAS)
            imResize.save(f + ' resized.jpg', 'JPEG', quality=90)



import cv2 
import os, sys
from PIL import Image
import io
import glob 
img_dir = userfilepath  # Enter Directory of all images  
data_path = os.path.join(img_dir,'*g') 
files = glob.glob(data_path) 
data = [] 
for f1 in files: 
    # img = cv2.imread(f1) 
    # data.append(img) 

     with open(f1,"rb") as file:
             resize()
             BinaryData=file.read()
             cursor.execute("INSERT INTO {tab} (photo)  
             VALUES({})".format(psycopg2.Binary(BinaryData, ) , tab=subject  ) )
             conn.commit()
             #Insert_blob(img_dir)

请随意更改顶部的变量,因为您似乎希望从用户输入中获取它们。为了这个例子,我选择了硬编码

看看下面的代码

import os

userfilepath = "files"
dirs = os.listdir("files")


def do_open(_file):
    print(f"Opening {_file}")


def do_resize(_file):
    print(f"resizing: {_file}")


def resize():
    for item in dirs:
        f = userfilepath + item
        print(f)
        if os.path.isfile(f):
            im = do_open(userfilepath + item)
            

resize()
# output 
# filesf1.txt
# filesf2.txt


def resize_well():
    for item in dirs:
        f = os.path.join(userfilepath, item)
        print(f)
        if os.path.isfile(f):
            im = do_open(f)



resize_well()
# output
# files\f1.txt
# Opening files\f1.txt
# files\f2.txt
# Opening files\f2.txt
resize\u well()
os.path.join()
中,创建一个正确的路径,在linux和windows上,使用字符串连接分别忽略了
/
\
分隔符


您的代码没有传递
if
语句,因为
userfilepath+item
不存在,但可能
userfilepath/item
存在。

将出现的错误放在问题帖中会有帮助!。。。
os.path.isfile(userfilepath+item)
是否返回带有适当斜杠的合理路径?为什么不使用类似于
os.path.join()
的方法来确保实际上我没有收到任何错误,也没有收到调整大小的图像。通过使用os.path.join(userfilepath+item),我收到的错误是FileNotFoundError:[Errno 2]没有这样的文件或目录:“C:\\Users\\my_profile\\Desktop\\unrized1.jpg”没有问题!快乐的编码!)