[英]how can I write a resizing code for multiple png pictures that can exempt one of the pichtures?
I wrote a code that extract all images from the a file then I want to exclude one of the specific extracted pictures from resizing in the next function. How should I do that?我写了一段代码,从 a 文件中提取所有图像,然后我想在接下来的 function 中从调整大小中排除其中一张特定的提取图片。我应该怎么做?
Here is my code:这是我的代码:
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)
I assume you have gotten the resizing to work, and you are only trying to skip a specific page or something along this line.我假设您已经调整了大小,并且您只是想跳过特定页面或沿着这条线的某些内容。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.