This Python script is Creates PDF file from List of Images.
( It have a series of folders inside a parent one. Each folder has several images)
My current problem is,
all image files contained in each folder merged by folder and save(creates) as a PDf file.
the merged file save it as a single PDf
the name of pdf files is by each folder name.
Example))
enter image description here
Root(Current) Folder
Folder1 : FileA.tif , FileB.gif
Folder2 : FileC.tif
Folder2 : FileD.tif
Result Folder
Folder1.pdf (Contains FileA.tif and FileB.gif combined into one pdf)
Folder2.pdf (Contains FileC.tif as pdf)
Folder3.pdf (Contains Filed.tif as pdf)
((MY Python script))
import os
import re
from fpdf import FPDF
import img2pdf
pdf = FPDF()
imagelist = []
TDPath = "D:\\Data\\ToDo" #Root(Current) Folder
RPath = "D:\\Data\\Result" #Result Folder
# Image to PDF
for dirpath, dirnames, filenames in os.walk(TDPath):
for filename in [f for f in filenames if re.match('.*([.]jpg|[.]png|[.]tif|[.]gif|[.]jpeg|[.]bmp|[.]jpg)', f)]:
full_path = os.path.join(dirpath, filename)
imagelist.append(full_path)
imagelist.sort()
#Image to PDF
for image in imagelist:
pdf.add_page()
pdf.image(image, 0, 0, 200)
# Save the pdf file
pdf.output(TDPath+".pdf", "F")
print("\nFound " + str(len(imagelist)) + " image files. Converting to PDF....\n")
Related
I have a folder that contains a group of files with different extension like .txt , .png , .pdf etc.
I want to organize the files and save them in different folders, for example, each file.txt in one folder and file.png in an another folder and file.pdf in an another folder etc.
import os
path = r'C:\Users\ibrahim\Desktop\test'
text_files = [f for f in os.listdir(path) if f.endswith('.txt')]
text_files
In this script how can I separately move files into different folders?
Something like this should work:
import os
import shutil
# Note we have to escape our backslashes here
path = "C:\\Users\\ibrahim\\Desktop\\test\\"
for f in os.listdir(path):
# Path to the original file
original_file_path = os.path.join(path, f)
# Only operate on files
if os.path.isfile(original_file_path):
# Get file name portion only
file_name = os.path.basename(original_file_path)
# Get the extension of the file and create a path for it
extension = f.split(".")[-1]
extension_path = os.path.join(path, extension)
# Create the path for files with the extension if it doesn't exist
if not os.path.exists(extension_path):
os.makedirs(extension_path)
# Copy the files into the new directory (copying is safer than moving, just so we can ensure it works as expected)
shutil.copyfile(original_file_path, os.path.join(extension_path, file_name))
Note that this will not be clever in anyway, .jpeg files will go into a different folder than .jpg files.
Try this,
import os
import shutil
txt_path = os.path.join(os.getcwd(), "text")
png_path = os.path.join(os.getcwd(), "png")
pdf_path = os.path.join(os.getcwd(), "pdf")
if not os.path.isdir(txt_path):
os.makedirs(txt_path)
print("text folder created")
if not os.path.isdir(png_path):
os.makedirs(png_path)
print("png folder created")
if not os.path.isdir(pdf_path):
os.makedirs(pdf_path)
print("pdf folder created")
#files = ['a.png' , 'b.pdf', 'c.txt']
files = [f for f in os.listdir(path)]
for file in files:
file_path = os.path.join(os.getcwd(), file)
if file_path.endswith('.txt')==True:
print('move file to text folder')
shutil.move(file_path, txt_path)
if file_path.endswith('.png')==True:
print('move file to png folder')
shutil.move(file_path, png_path)
if file_path.endswith('.pdf')==True:
print('move file to pdf folder')
shutil.move(file_path, pdf_path)
I have a folder contains files of type PDF, PNG, and JPEG. I'm trying to convert PDF files to images and this is the code I've tried:
from pdf2image import convert_from_path, convert_from_bytes
from pdf2image.exceptions import (
PDFInfoNotInstalledError,
PDFPageCountError,
PDFSyntaxError
)
images = convert_from_path('41117 UIs in eng.pdf')
for i, image in enumerate(images):
fname = 'image'+str(i)+'.jpg'
image.save(fname, "JPEG")
Clearly this code is only for a single pdf file and I want to transfer the code to serve several pdf files that are mixed with other file types in the same folder.
please help.
You could try something like this (this script finds pdf files in the same directory with your python program):
import os
from pdf2image import convert_from_path, convert_from_bytes
from pdf2image.exceptions import (
PDFInfoNotInstalledError,
PDFPageCountError,
PDFSyntaxError
)
# get all pdf files from directory
pdf_files = [filename for filename in os.listdir(
'.') if filename.endswith('.pdf')]
for pdf_file in pdf_files:
images = convert_from_path(pdf_file)
print(pdf_file)
for i, image in enumerate(images):
fname = pdf_file+'_image'+str(i)+'.jpg'
image.save(fname, "JPEG")
I have thousands of tiff files which are mulitpage tiff. I have already annotated the file name with metadata describing what the tiff is. I would now like to split the multipage tiff into single tiff images.
I created a new directory for the mulitpage tif, but don't know how to recursively name the new tifs in the img.save and move the files to a new folder. It works by running shutil 2x. Has to be a cleaner
from PIL import Image
import os, shutil
# make destination folder
os.makedirs("multipage_tif")
dest1 = ".\multipage_tif"
#split multipage tifs
for dirpath, dirs, files in os.walk("."):
for filename in files:
fname = os.path.join(dirpath,filename)
if fname.endswith('.tif'):
img = Image.open(filename)
fn, fext = os.path.splitext(filename)
for i in range(2):
try:
img.seek(i)
img.save(fn+'_page_%s.tif'%(i,))
except EOFError:
break
#move split files to the destination folder
for dirpath, dirs, files in os.walk("."):
for filename in files:
if filename.endswith('page_1.tif'):
shutil.move(os.path.join(dirpath,filename), os.path.join(dest1,filename))
for dirpath, dirs, files in os.walk("."):
for filename in files:
if filename.endswith('page_0.tif'):
shutil.move(os.path.join(dirpath,filename), os.path.join(dest1,filename))
I have code with looks through a folder 'Images' and then subfolders and processes all those images.
I now need to save those images to a parallel directory, i.e. a folder called 'Processed Images' (in same directory as 'Images' folder) and then to the subfolders within this folder - these subfolders are named the same as the subfolders in 'Images' - the image should save to the same name of subfolder that it came from.
I can get the images to save to 'Processed Images' but not the subfolders within it.
path = ("...\\Images")
for dirName, subdirList, fileList, in os.walk(path):
for file in fileList:
full_file_path = os.path.join(dirName, file)
if file.endswith((".jpg")):
image_file = Image.open(full_file_path)
image_file = image_file.convert('L')
image_file = PIL.ImageOps.invert(image_file)
image_file = image_file.resize((28, 28))
new_filename = file.split('.jpg')[0] + 'new.png'
path2 = ("...\\Processed Images")
image_file.save(os.path.join(path2, new_filename))
else: continue
You can use the function os.mkdir() to create a new folder. dirName returned by os.walk() gives you the current folder path, so you can just extract the part of the path that you need, append it to ...\\Processed Images and create the new folder if needed.
Be sure to use two separate folder trees for the input files and output files. Otherwise os.walk() will find the new directories with the output images and continue to iterate over them.
I think you can seriously simplify this code using pathlib. I’m not sure about the triple dots (I think they should be double) in your base paths but they may work for you.
from pathlib import Path
path = Path("...\\Images")
path2 = Path("...\\Processed Images")
path2.mkdir(exist_ok=True)
for jpg_file in p.glob('**/*.jpg'):
full_file_path = str(jpg_file)
image_file = Image.open(full_file_path)
image_file = image_file.convert('L')
image_file = PIL.ImageOps.invert(image_file)
image_file = image_file.resize((28, 28))
new_filename = jpg_file.stem + 'new.png'
image_file.save(str(path2 / new_filename))
I am trying to move specific filetypes to a new directory based on a nested dictionary.
For example, (to separate video and photo files) move media files defined in config[key1][other_file] to OTHERMEDIA for each camera models in their respective folders:
001__=d5
OTHERMEDIA #move files to here
IMAGES
[mix of mediafiles in subdirectories] #move files from here
002__=alpha9
OTHERMEDIA #move files to here
IMAGES
[mix of mediafiles in subdirectories] #move files from here
Code gives shutil.move(os.path.join(root,files), os.path.join(destinationpath,files)). What I am doing wrong?
import shutil
import os
config = {
'd5': {},
'alpha9': {},
'g7': {},
}
config['d5']['other_file'] = ('avi', 'AVI')
config['alpha9']['other_file'] = ('jpg', 'JPG')
config['g7']['other_file'] = ('mp4', 'MP4')
destinationpath = 'OTHERMEDIA'
root = os.getcwd()
for camID in config:
for dir in next(os.walk(root))[1]:
if dir.endswith(camID):
for path, dirs, files in os.walk(os.path.join(root, dir)):
for f in files:
if any([f.lower().endswith(x) for x in config[camID]["other_file"]]):
os.path.join(path, f). files, os.path.join(destinationpath,files)
You're doing os.path.join(path, files) when you should be doing os.path.join(path, f). files is the entire iterable; f is the specific file.
Also, as an aside, since you're looking at f.lower(), you don't need to compare it with the capitalized versions of the file extensions.