Помогите пожалуйста, недавно начал изучать библиотеку PIL в python Не могу понять как из соединить 2 картинки в одну
2 ответа
from PIL import Image
#выбираем файлы, которые будем объединять.
#В моем случае google_images название подпапки, в которой хранятся изображения.
#Двойной слеш для устранения проблемы кодировки.
img_file_1 = 'google_images\\51NV6IL6q7L._AC_SL1080_.jpg'
img_file_2 = 'google_images\\811RGQWpH5L._SL1500_.jpg'
#открываем файлы
im_1 = Image.open(img_file_1)
im_2 = Image.open(img_file_2)
#картинки разного размера, поэтому для соединения их нужно преобразовать в единых размер.
#Можно выбрать свой в зависимости от ориентирования "склеивания".
#Я покажу пример склеивания по горизонтали.
im_1 = im_1.resize((400, 400))
im_1.size
im_2 = im_2.resize((400, 400))
im_2.size
#создаём пустую картинку
new_image = Image.new('RGB',(2*img_1.size[0], img_1.size[1]), (250,250,250))
#вставляем наши изображения
new_image.paste(img_1,(0,0))
new_image.paste(img_2,(mirrored_img_1.size[0],0))
#сохраняем новое объединенное изображение в нужном формате
new_image.save("some_image.jpg","JPEG")
#смотрим, что получилось
new_image.show()
The code you provided is an example of how to combine two images using the PIL (Python Imaging Library) module. However, there are a few errors and inconsistencies in the code. Here's the corrected version:
from PIL import Image
# Select the image files to be merged
img_file_1 = 'google_images/51NV6IL6q7L._AC_SL1080_.jpg'
img_file_2 = 'google_images/811RGQWpH5L._SL1500_.jpg'
# Open the image files
im_1 = Image.open(img_file_1)
im_2 = Image.open(img_file_2)
# Resize the images to the same size
im_1 = im_1.resize((400, 400))
im_2 = im_2.resize((400, 400))
# Create a new image with double the width of the individual images
new_image = Image.new('RGB', (2 * im_1.size[0], im_1.size[1]), (250, 250, 250))
# Paste the images onto the new image
new_image.paste(im_1, (0, 0))
new_image.paste(im_2, (im_1.size[0], 0))
# Save the merged image in the desired format
new_image.save("some_image.jpg", "JPEG")
# Display the merged image
new_image.show()
In this corrected code:
- The backslashes (
\
) in the file paths have been replaced with forward slashes (/
) to ensure compatibility across different operating systems. - The image resizing code has been fixed to use the correct variable names (
im_1
andim_2
instead ofimg_1
andimg_2
). - The
mirrored_img_1
variable has been removed since it was not defined or necessary. - The
new_image.show()
command displays the merged image after saving.
Make sure you have the PIL module installed (pip install pillow
) before running the code.
-
21. переведите, пожалуйста, текст ответа (нажав править ниже текста ответа) на русский язык 2. Почему публикация ответов созданных GPT и ChatGPT недопустима? 23 мая в 16:41