Midjourney 4合1大圖自動分割成4x4小圖
2023/08/13 07:29
瀏覽29
迴響0
推薦0
引用0

Midjourney在 Discord 中生成的 4合1 圖片,如果你對其中一張圖片滿意,你可以再點選一次放大按鈕。但是不難發現,如果你將 4合1 圖片下載下來後,在 Windows 電腦上它的大小為 1024x1024 畫素,在 Mac 電腦上它的大小為 2048x2048 畫素,細心的你肯定會發現,原來這張4合1的圖片都是分別由一樣畫素大小拼在一起的大圖。
但大家都知道,生成一次圖片會扣除一次 Fast Time,如果你再放大一次,還會再扣除一次 Fast Time。通常每次扣除的時間是 0.02 小時,這意味著如果你想要獲取到最終想要的那張大圖,你需要 0.02+0.02=0.04 小時。但是 Midjourney 並不是為你放大圖片,其實圖片在你生成的時候就已經是那麼大了。你選中哪個數字的圖片,它也只是把那張圖片供你下載而已。

如果是 16:9、4:3 等其他比例出的圖片,它們也都是由 4x4 的小圖組合成一張大圖的。
所以我們無非就是要想辦法把 4合1 的圖片下載下來,然後怎麼把它平均切割出來就可以了。這個需求如果用Python處理是非常容易實現的,所以想像以後你只要保存4合1的圖片即可,不需要再讓Midjourney幫你放大了。如果你嫌棄輸出的圖不夠大,那就是將輸出的圖再去AI放大即可(後續再說)。這樣做的目的,你懂的!
好了,廢話不多說,以下是波哥寫的Python程式碼可針任意比例的 4合1 圖片切割成4等分的圖。創作不易,轉載請注明出處。謝謝合作!
"""
Author: booooker.com
Date: 2023-07-31
Description: This is a Python script that split Midjourney's 4in1 photo.
"""
import os
import sys
from PIL import Image
def split_all_images_in_folder(folder_path, num_cols=2, num_rows=2):
# Create the output folder if it does not exist
output_folder = os.path.join(folder_path, "output")
if not os.path.exists(output_folder):
os.mkdir(output_folder)
# Get a list of all image files in the specified folder
image_files = [os.path.join(folder_path, file) for file in os.listdir(folder_path) if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp', '.tiff', '.ico', '.svg'))]
# Iterate through each image file and split it into multiple parts
for image_file in image_files:
# Open the image and get its size
image = Image.open(image_file)
width, height = image.size
# Calculate the size of each cropped image based on the number of columns and rows
cropped_width = width // num_cols
cropped_height = height // num_rows
# Crop the image into multiple parts
cropped_images = []
for row in range(num_rows):
for col in range(num_cols):
left = col * cropped_width
right = (col + 1) * cropped_width
top = row * cropped_height
bottom = (row + 1) * cropped_height
cropped_images.append(image.crop((left, top, right, bottom)))
# Get the directory path and base filename of the input image
directory, filename = os.path.split(image_file)
# Get the filename without the extension
filename_without_extension, extension = os.path.splitext(filename)
# Save the cropped images with the same filename as the original image in the output folder
for i, cropped_image in enumerate(cropped_images):
# Construct the output file path by appending _i+1. to the input filename
output_file_path = os.path.join(output_folder, "_.".format(filename_without_extension, i + 1, extension[1:]))
cropped_image.save(output_file_path, format=extension[1:])
print("Image split into parts!".format(image_file, len(cropped_images)))
if __name__ == '__main__':
folder_path = "your folder path goes here"
if not os.path.isdir(folder_path):
print("Invalid folder path.")
else:
num_cols = 2
num_rows = 2
split_all_images_in_folder(folder_path, num_cols, num_rows)
以上程式碼複製下來使用的時候,記得要把folder_path = "your folder path goes here"改成你存放 4in1 圖片的路徑。執行指令碼後,它會自動識別資料夾中的所有圖片並將4等分後存放到同目錄output資料夾下。
以上代码还可以新增如定期检测资料夹下是否有新增图片,如有,4等分,或者分割完成后的原图保存在同目录Finished资料夹下防止二次处理,这样的代码由于需要较深入,就不在这里说明了。
原貼鏈接:https://www.booooker.com/502.html?feed_id=448&_unique_id=64d99179b36e2&utm_source=&utm_medium=ibook&utm_campaign=booooker
你可能會有興趣的文章:
限會員,要發表迴響,請先登入

