I'm trying to use imagemagick to take several JPGs and lay them out on a pdf
I'm at the beginning of the project and I do not understand how to open the multiple images and do this.
My research indicated to me that this is possible on the command line by simply passing the convert() function multiple files. experience is telling me that this isn't how it's done with Wand, but I can't figure out how!
Any advice is appreciated!
my example code is below.
before you excute this code, insert the dir path.
import os
from wand.image import Image
from wand.display import display
path = "____absolute_dir_path____ (ex. /home/kim/work/)"
dirList=os.listdir(path)
for fname in dirList:
print fname
with Image(filename=path+fname) as img:
print img.size
Related
I am trying to convert a PDF into JPEG using Python. Below are the steps I have taken as well as the code but, firstly, here are:
Expected results: Have 1 JPEG file per page in the PDF file added into my "Output" folder.
Actual results: The code appears to run indefinitely without any JPEGS being added to the "Output" folder.
Steps taken:
Installed pdf2image via CMD (pip install pdf2image)
Installed Poppler.
Note on Poppler:
It is required to add it to PATH and I had done this in the environment variables but I kept getting the error pdf2image.exceptions.PDFInfoNotInstalledError: Unable to get page count. Is Poppler installed and in PATH?. So as a workaround, I have added the path in the code directly and I am not receiving this error any longer.
from pdf2image import convert_from_path
path = "D:/Users/<USERNAME>/Desktop/Python/DeratingTool/"
pdfname = path+"<PDFNAME>.pdf"
images = convert_from_path(pdfname, 500,poppler_path=r'C:\Program Files\Release-22.04.0-0\poppler-22.04.0\Library\bin')
output_folder_path = "D:/Users/<USERNAME>/Desktop/Python/DeratingTool/Output"
i = 1
for image in images:
image.save(output_folder_path + str(i) + "jpg", "JPEG")
i = i+1
Any ideas why this doesn't seem to be able to finish would be most welcome.
Thank you.
I actually found all of the information I needed for the desired result right in the definitions (Thank you, #RJAdriaansen for pointing me back there). The default format is set to "PPM" and can be changed to "jpeg" Below is the functioning code for my purposes:
from pdf2image import convert_from_path
path = "D:/Users/<USERNAME>/Desktop/Python/DeratingTool/"
pdfname = path+"<FILENAME>.pdf"
images = convert_from_path(
pdfname,
dpi=500,
poppler_path=r'C:\Program Files\Release-22.04.0-0\poppler-22.04.0\Library\bin',
output_folder="D:/Users/<USERNAME>/Desktop/Python/DeratingTool/Output",
fmt="jpeg",
jpegopt=None)
Thank you
from pdf2image import convert_from_path
images = convert_from_path('path.pdf',poppler_path=r"E:/software/poppler-0.67.0/bin")
for i in range(len(images)):
images[i].save('image_name'+ str(i) +'.jpg', 'JPEG')
But now I want to convert more than 100 pdf files into images.
Is there any way?
Thanks in advance.
You can use glob to 'glob' the file names into a list: Python glob is here https://docs.python.org/3/library/glob.html - but it's a general expression for using wildcard expansion in the (*nix) filesystem [https://en.wikipedia.org/wiki/Glob_(programming)]. I assume it works under windows :)
Then you just loop over the files. Hey presto!
import glob
from pdf2image import convert_from_path
poppler_path = r"E:/software/poppler-0.67.0/bin"
pdf_filenames = glob.glob('/path/to/image_dir/*.pdf')
for pdf_filename in pdf_filenames:
images = convert_from_path(pdf_filename, poppler_path=poppler_path)
for i in range(len(images)):
images[i].save(f"{pdf_filename}{i}.jpg", 'JPEG')
!TIP: f"{pdf_filename}{i}.jpg" is a python f-string which gives a the reader a better idea of what the string will look like eventually. You might want to zero pad the integers there, because at some point you might want to 'glob' those or some such. There are lots of ways to achieve that - see How to pad zeroes to a string? for example.
You will possibly need to use the os module.
First step:
Use the os.listdir function like this
os.listdir(path to folder containing pdf files)
to get a list of paths within that folder.
To be more specific the os.isfile() to check if the current path is a file or a folder .
Perform the conversion if the path lead to a file like this.
images = convert_from_path('path.pdf',poppler_path=r"E:/software/poppler-0.67.0/bin")
for i in range(len(images)):
images[i].save('image_name'+ str(i) +'.jpg', 'JPEG')
Otherwise use recursion to traverse the folder even more.
Here's a link to a repo where I recursively resized images in a folder . It could be useful to digest this idea.
Link to a recursive resizing of images in a given path.
I am attempting to copy and apply a horizontal transformation to all images from one of my directories (directory_a), and then write them to another (directory_b) using cv2. All images are in png format. This is my first time using open cv/cv2.
When I use the code I currently have (see below), the transformation is applied but the images are saved to the same directory. Does anyone know how to properly do this?
from glob import glob
import cv2
#Flip Image horizontally and add Image to directory_b Folder
directory_a = '/content/drive/MyDrive/no_copies2/train/directory_a_images'
directory_b = '/content/drive/MyDrive/no_copies2/train/directory_b_images'
input_files = glob(os.path.join(directory_a, "*.png"))
for file in input_files:
img = cv2.imread(file)
horizontal_img = cv2.flip(img, 1)
cv2.imwrite(os.path.join(directory_b, file + '_flip_v' + '.png'), horizontal_img)
file variable is holding the path to the whole path, try:
cv2.imwrite(os.path.join(directory_b, os.path.basename(file).split(".")[0] + '_flip_v' + '.png'),
horizontal_img)
It might have to do with Google Colab. Make sure you have all directories created before running the code. I previously tried doing something similar and not setting up the directories properly in the drive came with some issues.
Currently I am using Python Pillow Library to edit images. Since I am dealing with large data-sets and need to edit some images with only specific name endings (say only image names that end with cropped or images of specific file type like png or bmp), is there a way to write code in such a way that allows me to open and edit these images? If so please give me hints or suggestions! Thanks!
Also Pillow version is 5.0.0 and Python version is 3.6.
If your question is to only know if is there a way to write code to only allow you to edit image files with specific file type and specific end names. Then the answer is YES. You can do it with python.
A Sample Code:
import os
from PIL import Image #Pillow
directory = os.fsencode("images_folder")
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(".png") or filename.endswith(".bmp") or "cropped" in filename:
# Do the editing using pillow
# img = Image.open(filename)
continue
Certainly you can do this in python, but the specific way of doing this obviously depends on the specifics of the problem. Are all your images stored in one directory or many? Will you be running the script from the same directory as the images or from some other directory? Etc.
To get you started, take a look at the os module here.
In this module, there is a listdir method that returns a list of all files inside a directory. You can iterate through that list and find all the filenames that ends with a specific set of characters by using the built in endswith method on strings. For example:
import os
fileslist = [f for f in os.listdir(path) if f.endswith('.jpg')]
Now that you have a filelist of all the files in a directory that ends with some certain characters, you can then use pillow to open the images from that list.
This question seems really easy, but I could not figure that out. I want to save a jpg file. I used plt.savefig(fileName). I also create a new folder using import os and then os.mkdir('D:\Users\data'). Now, I want to put this figure into this created folder. Thanks in advance ...
Just pass the full path to savefig.
folderName = 'D:/Users/data'
os.makedirs(folderName)
plt.savefig(os.path.join(folderName, fileName))