I try to convert all the pdf of a directory into images, but I have an issues because it only convert one pdf not all of them.
import matplotlib
import pytesseract
import os
import argparse
import cv2
from PIL import Image
import PyPDF2
from wand.image import Image as wi
for filename in os.listdir(src_path):
count = count + 1
# apply tesseract OCR and write to text file in specified target directory
target_path = args.trg_dir
# check if a file is a directory
if os.path.isdir(filename):
pass
else:
# check if a file is a pdf
try:
PyPDF2.PdfFileReader(open(os.path.join(src_path, filename), "rb"))
except PyPDF2.utils.PdfReadError:
else:
pdf = wi(filename=filename, resolution=300)
pdfimage = pdf.convert("jpeg")
i=1
for img in pdfimage.sequence:
page = wi(image=img)
page.save(filename=str(i)+".jpg")
i +=1
IIUC, try:
files = [file for file in os.listdir(src_path) if file.endswith(".pdf")]
for file in files:
with wi(file, resolution=300) as img_pdf:
for page, img in enumerate(img_pdf.sequence):
wi(img).save(f"{file}_{page}.jpg")
Related
I'm an newbie and I'm trying to create a multipage pdf with img2pdf (recursive), but only the last picture is saved in a pdf file.
from pathlib import Path
import os
import img2pdf
main_dir = Path(Path.cwd(),'MAIN')
for subfolder in main_dir.iterdir():
if subfolder.is_file():
continue
for filename in subfolder.iterdir():
#pdf as foldername
pdf_name = os.path.basename(subfolder)
#write image-file to pdf file
with open(pdf_name+".pdf", "wb") as f:
f.write(img2pdf.convert(str(filename)))
When I test it with print(filename) all images are going through the loop.
Maybe someone can tell me where my misconception is.
img2pdf module can directly take a list of image file names as an argument and converts them into pdf. Take a look at the documentation here
from pathlib import Path
import os
import img2pdf
main_dir = Path(Path.cwd(),'your/path/to/images/folder')
for subfolder in main_dir.iterdir():
imgs = []
if subfolder.is_file():
continue
for filename in subfolder.iterdir():
imgs.append(os.path.join(subfolder, filename))
pdf_name = os.path.basename(subfolder)
with open(pdf_name+".pdf", "wb") as f:
f.write(img2pdf.convert(imgs))
Can anyone show me how i can convert all png images in my local folder to jpg images? I've tried using the following code
path1 = r'C:\Users\david.han\Desktop\COPY_images_files'
path2 = r'C:\Users\david.han\Desktop\JPG converter'
files_to_convert = (f for f in os.listdir(path1) if f.endswith(".png"))
for filename in files_to_convert:
im = Image.open(os.path.join(path1, filename))
root, _ = os.path.splitext(filename)
jpg_file = os.path.join(path2, f'{root}.jpg')
im.save(jpg_file)
I keep getting this error "OSError: cannot write mode P as JPEG"
I've decided to keep JPG but in case anyone wants to know how to change png to jpg
enter code here
from PIL import Image
import os
path = r'C:\Users\david.han\Desktop\COPY_images_files'
for file in os.listdir(path):
if file.endswith(".jpg"):
img = Image.open(file)
file_name, file_ext = os.path.splitext(file)
img.save('/png/{}.png'.format(file_name))
Correction to David's code:
from PIL import Image
import os
path = r'enter_path_to_images'
for file in os.listdir(path):
if not file.endswith(".jpg"):
img = Image.open("{path}/{file}")
file_name, file_ext = os.path.splitext(file)
img.save('/png/{}.png'.format(file_name))
In this case, we also need to add the correct path not to get any errors.
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")
So I have been trying to iterate through a folder containing pictures, convert the pictures to a new format and save the new pictures into a new folder.
import sys
import os
from PIL import Image, ImageFilter
first_folder = sys.argv[1]
result = sys.argv[2]
if not os.path.exists(result):
os.makedirs(result)
for filename in os.listdir(first_folder):
img = Image.open(f'{first_folder}{filename}')
img.filter(ImageFilter.SHARPEN)
split =os.path.splitext(filename)[0]
img.save(f'{result}{split}.png', 'png')
print('Pictures Converted')
print ('all pictures in png format in the new folder')
when I run "python converter.py old_folder new_folder" I get this error message below
File "converter.py", line 18, in <module>
img = Image.open(f'{first_folder} {filename}')
File "C:\Users\scorpio\AppData\Local\Programs\Python\Python38\lib\site-packages\PIL\Image.py", line 2809, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'old_folderpic2.jpg'
you're missing folder separator, try this:
import sys
import os
from PIL import Image, ImageFilter
first_folder = sys.argv[1]
result = sys.argv[2]
if not os.path.exists(result):
os.makedirs(result)
for filename in os.listdir(first_folder):
img = Image.open(f'{first_folder}\{filename}')
img.filter(ImageFilter.SHARPEN)
split =os.path.splitext(filename)[0]
img.save(f'{result}\{split}.png', 'png')
print('Pictures Converted')
print ('all pictures in png format in the new folder')
I am generating a gif with images2gif from some .png pictures. I am using the following script:
__author__ = 'Robert'
from images2gif import writeGif
from PIL import Image
import os
file_names = sorted((fn for fn in os.listdir('/home/manager/Desktop/sf_linux_shared/project/prueba') if fn.endswith('.png')))
#['animationframa.png', 'animationframb.png', ...] "
images = [Image.open(fn) for fn in file_names]
size = (150,150)
for im in images:
im.thumbnail(size, Image.ANTIALIAS)
print writeGif.__doc__
filename = "my_gif.GIF"
writeGif(filename, images, duration=0.2)
however I have the following error:
IOError: [Errno 2] No such file or directory: 'cosa.png'
cosa.png is one of the pictures I want to create the gif with. The problem seems to be in:
images = [Image.open(fn) for fn in file_names]
but I cannot detect it
open(filename) looks in the current working directory if filename is not an
absolute path. The current working directory is the directory from which the script is run.
Either use os.chdir to change the working directory, or make all your filenames absolute:
WORKDIR = '/home/manager/Desktop/sf_linux_shared/project/prueba'
file_names = sorted((os.path.join(WORDIR, fn)
for fn in os.listdir(WORKDIR) if fn.endswith('.png')))