Having fun with images in python

Hello I, will be using jupyter notebook python to have some fun with images

I will be doing 3 things

  1. Creating some circles using python on black background.
  2. Cropping an image and inserting that cropped part into another image.

3.Merging 2 images

So, part 1

I will import library naming cv 2 and NumPy.

import cv2
import numpy as np

Now creating an image with black background of pixels 500,500 and 3 is to make it colourfull.

photo = np.zeros((500,500,3))

Now creating some circles in that image and saving it as nphoto.

nphoto = cv2.circle(photo,(100,100),50,(255,0,0),5)
nphoto = cv2.circle(photo,(150,150),50,(0,255,0),5)
nphoto = cv2.circle(photo,(200,200),50,(0,0,255),5)
nphoto = cv2.circle(photo,(250,250),50,(0,255,205),5)
nphoto = cv2.circle(photo,(300,300),50,(145,255,0),5)

(photo,(x axis center,y axis center),radius,(colour scheme),pixel width)

To show your image you need to type

cv2.imshow("Image",photo)
cv2.waitKey()
cv2.destroyAllWindows()

So now for cropping and pasting to another image.

Showing both images that I am taking.

pic1=cv2.imread("one.jpg")
cv2.imshow("Image1",pic1)
cv2.waitKey()
cv2.destroyAllWindows()
pic2=cv2.imread("two.jpg")
cv2.imshow("Image2",pic2)
cv2.waitKey()
cv2.destroyAllWindows()

Cropping pic 1 and pic 2 and showing cropped images.

c_pic1=pic1[150:500, 50:500]
cv2.imwrite("c_pic1.jpg",c_pic1)
cv2.imshow("Cropped",c_pic1)
cv2.waitKey()
cv2.destroyAllWindows()
c_pic2=pic2[150:500, 50:500]
cv2.imwrite("c_pic2.jpg",c_pic2)
cv2.imshow("Cropped",c_pic2)
cv2.waitKey()
cv2.destroyAllWindows()

Now inserting image 1 inside image 2 and showing the image.

c_pic1=pic1[150:500, 50:500]
cv2.imwrite("c_pic1.jpg",c_pic1)
cv2.imshow("Cropped",c_pic1)
cv2.waitKey()
cv2.destroyAllWindows()

Done

Now we will be merging 2 images

So basically what I did was re-read image 1 and image 2 because I had changed these images when I showed image 1 inside image 2 and then used a function called hconcat(horizontally concatenate) and last few lines are to show the image

image1=cv2.imread("one.jpg")image1.shapeimage2=cv2.imread("two.jpg")ctwo = image2[0:640,0:640]ctwo.shapecollage=cv2.hconcat([image1,ctwo])cv2.imshow("collage",collage)
cv2.waitKey()
cv2.destroyAllWindows()

The image

Thank you

By these few lines of code, I am sure you can learn and perform a few simple tasks in images too.

This is a basic machine learning linear regression model made on linux OS inside an docker container