importosimporttempfilefromfpdfimportFPDFfrompdf2imageimportconvert_from_pathdefpdf_to_image_pdf(pdf_path,output_pdf_path):# Convert pages to imagesimages=convert_from_path(pdf_path)# Create a new PDFpdf=FPDF(unit="pt",format="A4")forimageinimages:# Convert image size to A4a4_width_pt=595.28# A4 width in pointsa4_height_pt=841.89# A4 height in pointsaspect_ratio=image.width/image.heightifaspect_ratio>1:img_width_pt=a4_width_ptimg_height_pt=a4_width_pt/aspect_ratioelse:img_height_pt=a4_height_ptimg_width_pt=a4_height_pt*aspect_ratio# Add a page to PDFpdf.add_page()# Save image to a temporary filewithtempfile.NamedTemporaryFile(suffix=".jpg",delete=False)asf:temp_filename=f.nameimage.save(temp_filename,format='JPEG')# Add image to PDFpdf.image(temp_filename,x=0,y=0,w=img_width_pt,h=img_height_pt)# Remove temporary fileos.remove(temp_filename)# Save the PDFpdf.output(output_pdf_path)pdf_path="/Users/jiyouhai/Desktop/概率论习题及答案.pdf"output_pdf_path="/Users/jiyouhai/Desktop/概率论习题及答案_output.pdf"pdf_to_image_pdf(pdf_path,output_pdf_path)
importosimportre# <- Add this linefromPILimportImageimportglobdefatoi(text):returnint(text)iftext.isdigit()elsetextdefnatural_keys(text):''' alist.sort(key=natural_keys) sorts in human order http://nedbatchelder.com/blog/200712/human_sorting.html '''return[atoi(c)forcinre.split(r'(\d+)',text)]jpeg_folder='/Users/jiyouhai/Desktop/未命名文件夹/'jpeg_files=glob.glob(jpeg_folder+'*.jpeg')jpeg_files.sort(key=natural_keys)# Use natural_keys function for sortingimg_list=[]forfilenameinjpeg_files:img_list.append(Image.open(filename))img_list[0].save("/Users/jiyouhai/Desktop/answer5.pdf","PDF",resolution=100.0,save_all=True,append_images=img_list[1:])
importfitzimportiofromPILimportImagedoc=fitz.open('/Users/jiyouhai/Desktop/曾红刚复变函数答案助教版.pdf')page=doc[0]# Assuming the image is on the first pageimg_index=0# Assuming the image is the first imageimg=page.get_images(full=True)[img_index]xref=img[0]base=fitz.Pixmap(doc,xref)ifbase.n>4:# CMYK: convert to RGB firstpix=fitz.Pixmap(fitz.csRGB,base)else:pix=base# Save it in memoryimgdata=io.BytesIO(pix.tobytes())img_obj=Image.open(imgdata)# Print out some informationprint(img_obj.format)print(img_obj.mode)
importfitzimportioimportnumpyasnpfromPILimportImagefromreportlab.pdfgenimportcanvasfromreportlab.lib.pagesizesimportA3,A4,A5,landscapeimportosimporttempfiledefcrop_pdf(input_file_path,output_file_path):doc=fitz.open(input_file_path)pagesize=A4# Create a new canvas for the output PDFc=canvas.Canvas(output_file_path,pagesize=pagesize)# Create a list to hold the temporary image filestemp_files=[]foriinrange(len(doc)):page=doc[i]forimg_index,imginenumerate(page.get_images(full=True)):xref=img[0]base=fitz.Pixmap(doc,xref)ifbase.n==1:# Grayscalemode="L"elifbase.n>4:# CMYK: convert to RGBbase=fitz.Pixmap(fitz.csRGB,base)mode="RGB"else:continuepil_img=Image.frombytes(mode,[base.width,base.height],base.samples)np_array=np.array(pil_img)non_white_pixels=np.where(np_array!=255)# For grayscale imageifnon_white_pixels[0].size==0:# Check if the image is not emptyprint(f"Skipping empty image on page {i}, image {img_index}")continuey1,y2=non_white_pixels[0].min(),non_white_pixels[0].max()x1,x2=non_white_pixels[1].min(),non_white_pixels[1].max()cropped_img=pil_img.crop((x1,y1,x2,y2))# Create a temporary file to save the cropped imagewithtempfile.NamedTemporaryFile(delete=False,suffix=".jpg")asf:cropped_img.save(f,format='JPEG')temp_files.append(f.name)# Draw the image on the canvasc.drawImage(f.name,0,0,pagesize[0],pagesize[1])# Go to the next pagec.showPage()# Save the PDFc.save()# Delete the temporary image filesforfileintemp_files:os.remove(file)input_file_path='/Users/jiyouhai/Desktop/常微分方程作业答案.pdf'output_file_path='/Users/jiyouhai/Desktop/常微分方程作业答案_cropped.pdf'crop_pdf(input_file_path,output_file_path)