如何为多个png图片编写调整大小的代码,可以免除其中一张图片?



我编写了一段代码,从a文件中提取所有图像,然后我想在下一个函数中从调整大小中排除特定提取的图片之一。我该怎么做呢?

下面是我的代码:
def extract_imgfrmpdf():
# open the file
pdf_file = fitz.open(pdf_path)
# # iterate over pdf pages
for page_index in range(len(pdf_file)):
# get the page itself
page = pdf_file[page_index]
image_list = page.get_images()
# printing number of images found in this page
if image_list:
print(f"[+] Found a total of {len(image_list)} images in page {page_index}")
else:
print("[!] No images found on page", page_index)
for image_index, img in enumerate(page.get_images(), start=1):
# get the XREF of the image
xref = img[0]
# extract the image bytes
base_image = pdf_file.extract_image(xref)
image_bytes = base_image["image"]
# get the image extension
image_ext = base_image["ext"]
# load it to PIL
image = Image.open(io.BytesIO(image_bytes))
# save it to local disk
out = image.save(open(image_path+ '/' + f"image{page_index + 1}_{image_index}.{image_ext}", "wb"))
return (out)
def resize():
#assign the path of the images to a variable:
f = image_path
#By using os.listdir() function you can read all the file names in a directory.
for file in os.listdir(f):
f_img = f+"/"+file
#open the image
img = Image.open(f_img)
#resize the image
img = img.resize((253, 250))
#saved the image
out2 = img.save(f_img)
return(out2)

我假设您已经使大小调整工作,并且您只是试图跳过特定页面或这一行的其他内容。

f = image_path
dir_list = os.listdir(f) 
unwated_file_name = "pagexx" # the name of page you don't want to be resized
for i in range(len(dir_list)):
if dir_list[i] == unwated_file_name:
del_index = i
else:
pass
dir_list.pop(del_index)
print(dir_list) #can now see what pages that will be resized

相关内容

最新更新