我有一組影像(frame0.jpgto frame15.jpg),我想在黃色背景的網格中顯示:
from PIL import Image
# yellow background
img = Image.new("RGBA", (1920, 1080), (255, 216, 0, 255))
img_w, img_h = img.size
offset = 20
rows = 4
columns = 4
frame_w = img_w // rows - offset
frame_h = img_h // columns - offset
total_frames = rows * columns
x, y = 15, 15
for i in range(0, total_frames):
frame_on_the_row = Image.open("frame" str(i) ".jpg", "r")
img.paste(frame_on_the_row, (x, y))
x = frame_w 15 # its offset I choose empirically
if x > img_w - frame_w:
x = 15
y = frame_h 15
img.save("out.png")
輸出示例:

如您所見,底部和右側的黃色邊框比其他黃線略寬。我怎樣才能使它們都一樣寬?
uj5u.com熱心網友回復:
您用 20 計算了偏移量。我假設您是x, y = 15, 15通過減去 5 px 來確定的,以將右邊和底線計算為 20 px。但是,您現在將其他設定為 15 像素。
設定x, y = 16, 16將產生偶數行,所有 16 px 寬
對于通用計算,您只需考慮影像行/列數有額外的一行。
x_offset = ( offset * rows ) // ( rows 1 )
y_offset = ( offset * columns ) // ( columns 1 )
用文字表示:總偏移量除以總行數
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/405636.html
標籤:
上一篇:如何在組T-SQL中公開層次結構
下一篇:運算子不回傳預期結果
