Prompt Title: Python PIL and PDF.

Created 1 year ago
Votes 0
Views 166 times
0

Prompt Detail:

To make the script read PDF files in addition to TIF files, you will need to modify the code in a few places:

  1. Import the PyPDF2 library at the top of your script:
arduino
import PyPDF2
  1. Update the files variable to include PDF files in addition to TIF files:
css
files = [f for f in os.listdir(folder_path) if f.endswith('.tif') or f.endswith('.pdf')]
  1. Add a conditional statement for PDF files in the for loop:
python
for file in files: # construct the full path to the file file_path = os.path.join(folder_path, file) if file.endswith('.tif'): # open the TIFF file using pillow img = Image.open(file_path) # get the dimensions, resolution, and color space of the image based on the file name if file.endswith('-6R.tif'): dims = (11103, 11103) res = (150, 150) color_space = 'LAB' ... # check if the dimensions, resolution, and color space are correct error_message = "" if img.size != dims: error_message += "Dimensions are incorrect. " if img.info.get('dpi', (None, None)) != res: error_message += "Resolution is incorrect. " if img.mode != color_space: error_message += "Color space is incorrect. " if error_message != "": # create the AppleScript to flag the file script = """ tell application "Finder" set file_to_flag to (POSIX file "{}") as alias set label index of file_to_flag to 2 end tell """.format(file_path) # execute the AppleScript to flag the file subprocess.call(['osascript', '-e', script]) # increment the error count error_count += 1 # append the file name and error message to their respective lists error_files.append(file) error_messages.append(error_message) elif file.endswith('.pdf'): # open the PDF file using PyPDF2 pdf_file = open(file_path, 'rb') pdf_reader = PyPDF2.PdfFileReader(pdf_file) # get the dimensions of the first page in the PDF page = pdf_reader.getPage(0) dims = (int(page.mediaBox.getWidth()), int(page.mediaBox.getHeight())) # check if the dimensions are correct error_message = "" if dims != (2488, 1621): error_message += "Dimensions are incorrect. " if error_message != "": # create the AppleScript to flag the file script = """ tell application "Finder" set file_to_flag to (POSIX file "{}") as alias set label index of file_to_flag to 2 end tell """.format(file_path) # execute the AppleScript to flag the file subprocess.call(['osascript', '-e', script]) # increment the error count error_count += 1 # append the file name and error message to their respective lists error_files.append(file) error_messages.append(error_message)

This code will check the dimensions of the first page of a PDF file and flag the file if the dimensions are incorrect. You can modify the code to check other properties of the PDF file if necessary.

avatar
Robert
Shared 2 prompts
Created 1 year ago

Leave a Comment

Related Tag Prompts

271
0
Python Pro
1 year ago 2023-01-19 05:16:28 AIPRM
0
0
MySQL Database Creation.
1 year ago 2023-03-08 12:23:48 Pub
0
0
Cool stuff
1 year ago 2023-03-08 15:49:26 Sivayogeith
0
0
Bajar version de python
1 year ago 2023-03-09 10:48:33 Juanma
0
0
Frappe
1 year ago 2023-03-11 01:40:59 KuuKuu
0
0
0
0
python remote control server
1 year ago 2023-03-11 23:05:25 V0rt
0
0
output
1 year ago 2023-03-14 07:09:17 yuvaraj
0
0
s-parameter processing in python
1 year ago 2023-03-14 14:46:13 jhdo
0
0
Python Landsat data handling.
1 year ago 2023-03-15 13:10:40 nathan
0
0
Python para Ciencia Datos
1 year ago 2023-03-16 08:24:10 pedro
0
0
Following Rotated Logfiles.
1 year ago 2023-03-17 09:05:24 hamid
0
0
Modelo de Negócio.
1 year ago 2023-03-18 01:35:57 reiu
0
0
Django API for Todo
1 year ago 2023-03-18 17:45:33 AbdAlmjed
0
0
python爬虫
1 year ago 2023-03-21 00:23:14 DDL
0
0
Improvment_product_generator
1 year ago 2023-03-21 10:01:17 Matěj Konečný