![](/img/trans.png)
[英]Loop through all the images in a folder in a certain order according to their file names
[英]Writing Images to a PDF through a for loop, images are getting out of order
我顯示的腳本比我所做的要簡單一些,但它仍然與原始腳本存在相同的問題
我想做的是截取網頁截圖,go 到下一頁並截取截圖然后將其編譯成 PDF,我試圖避免將圖像寫入我的硬盤驅動器然后將其寫入 ZBCD1B68617759A1 ,然后從我的硬盤上刪除圖像。
現在圖像有時會出現故障,我認為這是因為無法足夠快地將圖像寫入 PDF,或者操作順序被搞砸了。 我相當新,所以我不確定。 我嘗試更改等待時間,我嘗試將所有圖像保存到字典中並在字典中循環,結果更糟的是重復相同的 4 張圖像。 我只知道我做得不對。 有什么建議么。
from PIL import ImageGrab, Image
from fpdf import FPDF
from win32api import GetSystemMetrics
import io
import time
from fpdf import FPDF
import keyboard
pdf_width = GetSystemMetrics(0)
pdf_height = GetSystemMetrics(1)
t = 10
pdf = FPDF(unit = "pt", format = [pdf_width,pdf_height])
for x in range(0, t):
img = ImageGrab.grab()
print(x)
mem_file = io.BytesIO()
img.save(mem_file, 'JPEG')
data = mem_file.getvalue()
my_img = Image.open(io.BytesIO(data))
pdf.add_page()
pdf.image(my_img,0,0)
keyboard.press_and_release('right')
time.sleep(2)
if x == (t-1):
pdf.output('test.pdf', 'F')
標記為 1-3 的圖片按順序排列 圖 4 與圖 2 相同,但不正確
經過數小時的嘗試和閱讀后的數字,我發了一個帖子,走了一會兒,然后在我回來的 15 分鍾內弄清楚了。 今天早上我嘗試使用字典但失敗了,所以我認為列表行不通。
t = int(page_count)
time.sleep(1)
pdf = FPDF(unit = "pt", format = [pdf_width,pdf_height])
for x in range(0, t):
string.append(ImageGrab.grab())
print(x)
mem_file = io.BytesIO()
string[x].save(mem_file, 'JPEG')
data = mem_file.getvalue()
string[x] = Image.open(io.BytesIO(data))
pdf.add_page()
pdf.image((string[x]),0,0)
keyboard.press_and_release('right')
ispage_Loading(driver)
time.sleep(WAIT)
if x == (t-1):
pdf.output(dir_path+'\\'+book_name+'.pdf', 'F')
winsound.PlaySound("*", winsound.SND_ALIAS)
explore(dir_path)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.