Collage Making Using Python
1 min readSep 26, 2021
Description:-
- Create image using Python.
- Take two images crop some part of both image and swap it.
- Take two images and combine it to form single image (collage).
The code for creating image using python is as follows:-
import cv2
import numpy as np
photo=np.zeros([500,1000,3],dtype=np.uint8)
photo2=cv2.circle(photo,(350,150), 50,[255,0,0],3 )photo2=cv2.circle(photo,(450,150), 50,[255,0,0],3 )
photo2=cv2.circle(photo,(550,150), 50,[255,0,0],3 )photo2=cv2.circle(photo,(400,240), 50,[0,0,255],3 )
photo2=cv2.circle(photo,(500,240), 50,[0,0,255],3 )photo2=cv2.circle(photo,(450,333), 50,[0,255,0],2 )cv2.imshow("image",photo2)
cv2.waitKey()
cv2.destroyAllWindows()
Next, we have to take 2 images and then crop some part of both image and swap it.
import cv2
import numpy
In [2]:
photo1 = cv2.imread('redhat.png')
photo2 = cv2.imread('linux.jpg')
In [3]:
cv2.imshow('photo1', photo1)
cv2.imshow('photo2', photo2)
cv2.waitKey()
cv2.destroyAllWindows()
In [4]:
cphoto1 = photo1 [:,:150]
cphoto2 = photo2 [:,:160]
cv2.imshow('cphoto1', cphoto1)
cv2.waitKey()
cv2.destroyAllWindows()
cv2.imshow('cphoto2', cphoto2)
cv2.waitKey()
cv2.destroyAllWindows()
photo1 [0:109,0:150] = cphoto2 [0:109,0:150]
cv2.imshow('swapped photo 1', photo1)
cv2.waitKey()
cv2.destroyAllWindows()
photo3 = cv2.imread('redhat.png')
photo4 = cv2.imread('linux.jpg')
cphoto3 = photo3 [:,:150]
cphoto4 = photo4 [:,:160]
cv2.imshow('cphoto3', cphoto3)
cv2.waitKey()
cv2.destroyAllWindows()
cv2.imshow('cphoto4', cphoto4)
cv2.waitKey()
cv2.destroyAllWindows()
photo4 [0:109,0:150] = cphoto3 [0:,0:]
cv2.imshow('swapped photo 1', photo4)
cv2.waitKey()
cv2.destroyAllWindows()
cv2.imshow('swapped photo 4', cphoto4)
cv2.waitKey()
cv2.destroyAllWindows()
Finally, take two images and combine it to form single image.
import cv2
import numpy
image1 = cv2.imread('elonmusk.jpg')
image2 = cv2.imread('doge.jpg')
image1 = image1 [:, 0:262]
cv2.imshow('image1', image1)
cv2.waitKey()
cv2.destroyAllWindows()
image2 = image2 [0:180,:]
cv2.imshow('image2', image2)
cv2.waitKey()
cv2.destroyAllWindows()
image = cv2.hconcat([image1,image2])
cv2.imshow('collage', image)
cv2.waitKey()
cv2.destroyAllWindows()
Thank you :)