I want to resize images in the folder and store them into database with filesystem using postgres as database and jupyter notebook. I am using resize function for resizing my images and then saving then into database but resize function seems not working and unable to understand my mistake.
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)
Feel free to change the variables at the top since you seem to want to get them from user input. I've chosen to hardcode them for the purpose of this example.
Have a look at the code below
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
In resize_well() os.path.join() creates a proper path, where as using string concatenation misses out the / or \ delimiter on linux and windows respectively.
Your code isn't passing the if statement because userfilepath+item doesn't exist, but probably userfilepath/item does.
Related
I am converting the hexadecimal files to images. The input files are converted to byte string using binascii library. The problem arises when the byte string is written to form an image. The output of all the hexadecimal files is same. I will be grateful if someone provides me a solution.
Here is my code:
import binascii
import cv2
import os
from tkinter import *
from tkinter import filedialog
#Hide the root window that comes by default
root=Tk()
root.withdraw()
#Browse and select txt files
dir=[]
dir=filedialog.askopenfilenames(
initialdir="C:\Binaries\Hexadecimal_Text_Files",
title="Open Text file",
filetypes=(("Text Files", "*.txt"),)
)
#Reading data in txt files and decoding hexadecimal characters
for x in dir:
tf=open(x)#Open file
data=tf.read()#Read data in file
data=data.replace(' ','')#Remove whitespaces
data=data.replace('\n','')#Remove breaks in lines
data=binascii.a2b_hex(data)
tf.close()
#Extract txt filename without extension
pathname, extension = os.path.splitext(f"{x}")#Split path into filename and extenion
filename = pathname.split('/')#Get filename without txt extension
filepath=f"C:\Binaries\Images\{filename[-1]}.png"#Defining name of image file same as txt file
#Write data into image
with open(filepath, 'wb') as image_file:
img=image_file.write(data)
#Resizing Image
img=cv2.resize(img,(500,500))
cv2.imwrite(filepath,img)
Output:
I made my own version because I could not get yours to work, but if you want to make yours work, at least one problem with I found is with this line:
img=cv2.resize(img,(500,500))
by printing all the variables after the supposed "conversion", I found that your variable img in the previous line is not an image but the result of image_file.write(data) which returns the number of bytes written to the file and not the image itself, which is probably why it always prints the same image.
Here is my version
root=Tk()
root.withdraw()
file_path = filedialog.askopenfilename(
initialdir = "C:\Binaries\Images",
title = "Select Hexadecimal Text File",
filetypes = (("Text Files", "*.txt"),)
)
with open(file_path, "r") as hex_file:
hex_data = hex_file.read().replace("\n", "")
#replaces white spaces and new lines from file
binary_data = binascii.a2b_hex(hex_data)
#converts the hexadecimal data to binary
pathname, extension = os.path.splitext(file_path)
image_path = pathname + ".png"
#image path and format
with open(image_path, "wb") as image_file:
image_file.write(binary_data)
#writing the binary data to image file
img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
#if txt file is empty
if img is None:
print("Error: Image not loaded!")
else:
cv2.imshow("image", img)
#waits for key input and closes when pressing any key
cv2.waitKey(0)
cv2.destroyAllWindows()
I have converted the hexadecimal files into images by using numpy array and Pillow. Now I am getting different images.
import numpy as np
import binascii
import os
from PIL import Image as im
from tkinter import *
from tkinter import filedialog
# Hide the root window that comes by default
root = Tk()
root.withdraw()
# Browse and select txt files
dir = []
dir = filedialog.askopenfilenames(
initialdir="C:\Binaries\Folder_3",
title="Open Text file",
filetypes=(("Text Files", "*.txt"),)
)
# Reading data in txt files and decoding hexadecimal characters
for temp in dir:
tf = open(temp) # Open file
data = tf.read() # Read data in file
data= data.replace('\'','') #Remove label
data = data.replace(' ', '') # Remove whitespaces
data = data.replace('\n', '') # Remove breaks in lines
data = binascii.a2b_hex(data)
tf.close()
#Converting bytes array to numpy array
a = np.frombuffer(data, dtype='uint8')
#print(a) //Display array
#Finding optimal factor pair for size of image
x = len(a)
val1=0
val2=0
for i in range(1, int(pow(x, 1 / 2))+1):
if x % i == 0:
val1=i
val2=int(x / i)
#Converting 1-D to 2-D numpy array
a = np.reshape(a, (val1, val2))
#print(a) #Display 2-D array
#Writing array to image
data = im.fromarray(a)
# Split path into filename and extenion
pathname, extension = os.path.splitext(f"{temp}")
filename = pathname.split('/') # Get filename without txt extension
# Defining name of image file same as txt file
filepath = f"C:\Binaries\Images_3\{filename[-1]}.png"
#Resize image
data=data.resize((500,500))
#Saving image into path
data.save(filepath)
I have the following code:
import face_recognition
from PIL import Image, ImageDraw
from tkinter import Tk
from tkinter.filedialog import askopenfilename
from shutil import copyfile
#Ask user for file name
Tk().withdraw()
filename = askopenfilename()
#Add known images
image_of_person = face_recognition.load_image_file(filename)
person_face_encoding = face_recognition.face_encodings(image_of_person)[0]
for i in range (1, 8):
#Construct the picture name and print it
file_name = str(i).zfill(5) + ".jpg"
print(file_name)
#Load the file
newPic = face_recognition.load_image_file(file_name)
#Search every detected face
for face_encoding in face_recognition.face_encodings(newPic):
results = face_recognition.compare_faces([person_face_encoding], face_encoding, 0.5)
#If match, show it
if results[0] == True:
copyFile(file_name, "./img/saved" + file_name)
The intention is to use the known image (image_of_person) and search a folder of images ('./img/unknown') for a match, then show the matched photo.
I receive the error:
No such file or directory: '00001.jpg'
On the line
newPic = face_recognition.load_image_file(file_name)
How do I point the recognition to the sample of images folder?
Note: for i in range (1, 8): - 8 Images are in the sample folder.
I think your problem is you're not giving the right path when trying to load the images.
Change
file_name = str(i).zfill(5) + ".jpg"
to
file_name = f"./img/unknown/{str(i).zfill(5)}.jpg"
Note: If you're using python2, then
file_name = "./img/unknown/{}.jpg".format(str(i).zfill(5)
Another tip, if you want your code to be generic, no matter how many images there are, you can do
for i in range(1, len(os.listdir("./img/unknown"))).
Or, even better, you can simply do
for img in os.listdir("img/unknown"):
file_name = os.path.join("img/unknown", img)
... continue with the rest of the flow ...
I'm in an introductory neural networking class so mind my ignorance. Also my first SO post.
I'm trying to resize some very highly resolved images within a dataset into 80x80p grayscale images in a new dataset. However, when I do this, I'd like to keep the filenames of each new image the same as the original image. The only way I know how to resave images into a new file is through a str(count) which isn't what I want. The filenames are important in creating a .csv file for my dataset later.
The only SO post I can find that is related is this:
Use original file name to save image
But the code suggested there didn't work - wasn't sure if I was going about it the wrong way.
import os
from PIL import Image
import imghdr
count=0
path1 = "/Users/..."
path2 = "/Users/..."
listing = os.listdir(path1)
for file in listing:
type = imghdr.what((path1 + file))
if type == "jpeg":
img = Image.open("/Users/..." +file).convert('LA')
img_resized = img.resize((80,80))
img_resized.save(path2 + str(count) + '.png')
count +=1
pass
pass
Reuse the original filename that you get from the for loop i.e. file
and, split it into filename and extension using os.path.splitext() like below:
import os
from PIL import Image
import imghdr
count=0
path1 = "/Users/..."
path2 = "/Users/..."
listing = os.listdir(path1)
for file in listing:
type = imghdr.what((path1 + file))
if type == "jpeg":
img = Image.open("/Users/..." +file).convert('LA')
img_resized = img.resize((80,80))
# splitting the original filename to remove extension
img_filename = os.path.splitext(file)[0]
img_resized.save(path2 + img_filename + '.png')
count +=1
pass
Another option, we can use python str's built-in split method to split the original filename by . and discard the extension.
import os
from PIL import Image
import imghdr
count=0
path1 = "/Users/..."
path2 = "/Users/..."
listing = os.listdir(path1)
for file in listing:
type = imghdr.what((path1 + file))
if type == "jpeg":
img = Image.open("/Users/..." +file).convert('LA')
img_resized = img.resize((80,80))
# splitting the original filename to remove extension
img_filename = file.split(".")[0]
img_resized.save(path2 + img_filename + '.png')
count +=1
pass
So, if an image has a name such as some_image.jpeg then, the img_filename will have a value some_image as we splitted by . and discarded .jpeg part of it.
NOTE: This option assumes the original_filename will not contain any . other than the extension.
I assume that image name is on path1. If so you can grap image name from there in this way:
x=path1.rsplit('/',1)[1]
We are splitting path1 on last slash and taking image name string via indexing.
after having concatenated 10 strips of the same image, I want to convert them into reflectance and therefore divide them by 10,000. Nevertheless I have two types of files in my folders, except I want to apply my code only to my.img file and not to the.hdr...
Do you know how I can proceed to make this selection with os.listdir?
my code is as follows :
import os
import spectral as sp
import spectral.io.envi as envi
src_directory = "/d/afavro/Bureau/3_stack/"
dossier = os.listdir (src_directory)
print(dossier)
for fichier in dossier:
print (fichier)
ssrc_directory = "/d/afavro/Bureau/3_stack/" + fichier
rasters = os.listdir (ssrc_directory)
print(rasters)
OUTPUT_FOLDER = "/d/afavro/Bureau/4_reflectance/" + 'reflectance_' + fichier
print(OUTPUT_FOLDER)
if not os.path.exists(OUTPUT_FOLDER):
os.makedirs(OUTPUT_FOLDER)
for image in rasters:
print (image)
img = sp.open_image(image)
print("%s opened successfully" %os.path.basename(image))
im_HS = img[:,:,:]/10000
header = envi.read_envi_header('/d/afavro/Bureau/3_stack/'+ image)
#Save index image
sp.envi.save_image(OUTPUT_FOLDER + '/reflectance_' + image, im_HS, metadate = header, force = True, interleave = 'bsq')
I think that making a yew loop would be a good idea but I don't know how to do it...
Ideas ?
Find the extension of the file using os.path.splitext
for f in os.listdir('<path>'):
name, ext = os.path.splitext(f)
if ext == '.img':
#do stuff
Why don't you use glob?
from glob import glob
for f in glob('/your/path/*.img'):
pass # add your code here
Hello You can use use Pathlib as an object oriented Path management library
and do something like
from Pathlib2 import Path
pattern_1 = "type1"
pattern_2 = "type2"
list_pattern_1_files = list(Path(<YOUR_PATH>).glob(f'**/*.{pattern_1}'))
list_pattern_2_files = list(Path(<YOUR_PATH>).glob(f'**/*.{pattern_2}'))
recently I try to find the right way to read LSUN dataset which is in the form of lmdb. However, I do not find any useful information. I want to know how to read image data from lmdb and what the advantage is in that way. Thank you!
Finally, I use the following code to extract LUSN images from lmbd file.
import os
import lmdb
from PIL import Image
import tempfile
def _export_mdb_images(db_path, out_dir=None, flat=True, limit=-1, size=256):
out_dir = out_dir
env = lmdb.open(
db_path, map_size=1099511627776,
max_readers=1000, readonly=True
)
count = 0
with env.begin(write=False) as txn:
cursor = txn.cursor()
for key, val in cursor:
key = str(key, 'utf-8')
# decide image out directory
if not flat:
image_out_dir = os.path.join(out_dir, '/'.join(key[:6]))
else:
image_out_dir = out_dir
# create the directory if an image out directory doesn't exist
if not os.path.exists(image_out_dir):
os.makedirs(image_out_dir)
with tempfile.NamedTemporaryFile('wb') as temp:
temp.write(val)
temp.flush()
temp.seek(0)
image_out_path = os.path.join(image_out_dir, key + '.jpg')
Image.open(temp.name).resize((size, size)).save(image_out_path)
count += 1
if count == limit:
break
if count % 1000 == 0:
print('Finished', count, 'images')
print("start")
db_path = "path to lmbd"
out_dir = os.path.join(db_path, "data")
_export_mdb_images(db_path, out_dir)