chat-with-chatgpt
chat-with-chatgpt copied to clipboard
python
python写一个脚本,可以把图片缩放到指定尺寸,空白地方用黑色填充
Hello! I am your assistant on GitHub, built by the Second State team. I am ready to assist you with your question!
To resize an image to a specific dimension and fill the empty space with black color, you can use the Python Imaging Library (PIL). Here's an example script that accomplishes this:
from PIL import Image
def resize_image(input_image_path, output_image_path, size):
original_image = Image.open(input_image_path)
width, height = original_image.size
# Calculate the new size while maintaining the aspect ratio
if width > height:
new_width = size
new_height = int(new_width * height / width)
else:
new_height = size
new_width = int(new_height * width / height)
# Create a new image with the desired size and fill it with black color
resized_image = Image.new("RGB", (size, size), (0, 0, 0))
# Paste the original image into the resized image, while centering it
resized_image.paste(original_image.resize((new_width, new_height)), ((size - new_width) // 2, (size - new_height) // 2))
# Save the resized image
resized_image.save(output_image_path)
# Usage example
input_path = "input.jpg"
output_path = "output.jpg"
desired_size = 800
resize_image(input_path, output_path, desired_size)
You need to have the PIL package installed to run this script. You can install it by running pip install pillow. Replace "input.jpg" and "output.jpg" with the paths to your input and desired output image, respectively. The desired_size variable should be set to the desired dimension (in pixels) for the output image.
Please let me know if you need further assistance!
怎么跟原图保持一样的背景
如果您想要保持原始图像的背景,并且仅调整图像到指定尺寸,而不是用黑色填充空白处,您可以按照以下代码调整示例脚本:
from PIL import Image
def resize_image(input_image_path, output_image_path, size):
original_image = Image.open(input_image_path)
width, height = original_image.size
# Calculate the new size while maintaining the aspect ratio
if width > height:
new_width = size
new_height = int(new_width * height / width)
else:
new_height = size
new_width = int(new_height * width / height)
# Resize the image while preserving the original background
resized_image = original_image.resize((new_width, new_height), Image.ANTIALIAS)
# Create a new image with the desired size and paste the resized image onto it
final_image = Image.new("RGB", (size, size))
x = (size - new_width) // 2
y = (size - new_height) // 2
final_image.paste(resized_image, (x, y))
# Save the resized image
final_image.save(output_image_path)
# Usage example
input_path = "input.jpg"
output_path = "output.jpg"
desired_size = 800
resize_image(input_path, output_path, desired_size)
这段代码与之前的代码相似,但是 resized_image 现在是根据 new_width 和 new_height 调整大小的原始图像,并使用 Image.ANTIALIAS 表示在调整大小时使用抗锯齿算法。
然后,我们创建一个新的 final_image 对象,并在其中的正确位置使用 paste 方法将 resized_image 粘贴到该对象上。
保存的过程与之前的代码相同。
请注意,为了正确运行此脚本,您需要在运行之前安装 PIL 库(如果尚未安装)。可以通过运行 pip install pillow 来安装它。
如果您有任何进一步的问题,请随时提问!
it not work
I apologize for the inconvenience. It seems that there might be an issue with the code I provided.
To resize an image while preserving the original background, you need to create a new image with the desired size and then paste the original image onto it using a mask. Here's an updated version of the script:
from PIL import Image
def resize_image(input_image_path, output_image_path, size):
original_image = Image.open(input_image_path)
width, height = original_image.size
# Calculate the new size while maintaining the aspect ratio
if width > height:
new_width = size
new_height = int(new_width * height / width)
else:
new_height = size
new_width = int(new_height * width / height)
# Calculate the offset to center the original image
x_offset = (size - new_width) // 2
y_offset = (size - new_height) // 2
# Create a new image with the desired size and a white background
final_image = Image.new("RGB", (size, size), (255, 255, 255))
# Resize the original image while preserving the background
resized_image = original_image.resize((new_width, new_height), Image.ANTIALIAS)
# Create a mask with the resized image to only paste the non-transparent regions
mask = Image.new('L', (width, height))
mask.paste(resized_image.convert('L'), (x_offset, y_offset))
# Paste the resized image onto the final image using the mask
final_image.paste(resized_image, (x_offset, y_offset), mask=mask)
# Save the resized image with the preserved background
final_image.save(output_image_path)
# Usage example
input_path = "input.jpg"
output_path = "output.jpg"
desired_size = 800
resize_image(input_path, output_path, desired_size)
Please make sure you have the PIL package installed by running pip install pillow. Replace "input.jpg" and "output.jpg" with the paths to your input and desired output images, respectively. Set the desired_size variable to the desired dimension (in pixels) for the output image.
I hope this updated code works for you. Let me know if you have any further questions or issues!