Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save IAmSuyogJadhav/305bfd9a0605a4c096383408bee7fd5c to your computer and use it in GitHub Desktop.

Select an option

Save IAmSuyogJadhav/305bfd9a0605a4c096383408bee7fd5c to your computer and use it in GitHub Desktop.
Add transparency to rectangles, circles, polgons, text or any shape drawn in OpenCV.
import cv2
image = cv2.imread('test.jpg')
overlay = image.copy()
x, y, w, h = 10, 10, 10, 10 # Rectangle parameters
cv2.rectangle(overlay, (x, y), (x+w, y+h), (0, 200, 0), -1) # A filled rectangle
alpha = 0.4 # Transparency factor.
# Following line overlays transparent rectangle over the image
image_new = cv2.addWeighted(overlay, alpha, image, 1 - alpha, 0)
@shivaraj1994

Copy link
Copy Markdown

Thanks a lot man

@michelpf

Copy link
Copy Markdown

Great!

@ahmadabdulnasir

Copy link
Copy Markdown

Thanks alot

@hamza-ghadyali

Copy link
Copy Markdown

thanks

@kpandu

kpandu commented Apr 20, 2020

Copy link
Copy Markdown

Thanks

@JBisc

JBisc commented Apr 30, 2020

Copy link
Copy Markdown

Thanks!

@ArashCh

ArashCh commented Aug 26, 2020

Copy link
Copy Markdown

Nice work...Thanks!

@davidgikas

Copy link
Copy Markdown

How can two rectangles have different transparency?

@IAmSuyogJadhav

IAmSuyogJadhav commented Aug 27, 2020

Copy link
Copy Markdown
Author

How can two rectangles have different transparency?

@davidgikas,
One way I think that can be done is to just go on doing this same process iteratively.
Add the first rectangle using the above script, and then just create the second rectangle and add to image_new with your desired alpha using cv2.addWeighted.

@davidgikas

Copy link
Copy Markdown

Thank you, i did this in a for statement and it worked!

@lidorshimoni

Copy link
Copy Markdown

Thanks a lot!
is there a more efficient way of doing this? since copying a large image is computationally heavy when I want to draw it many times a second

@manaspalaparthi

Copy link
Copy Markdown

i think open cv should implement this inside rectangle function as a parameter of transparency

@IAmSuyogJadhav

Copy link
Copy Markdown
Author

i think open cv should implement this inside rectangle function as a parameter of transparency

Yup. Completely agree. This should be a straightforward thing yet we have to do it in a roundabout way currently.

@omegaBionic

Copy link
Copy Markdown

Thanks :)

@TalhaUsuf

Copy link
Copy Markdown

Man ! at last i found a simple solution. Lots of thanks. Allah bless you

@anibalsolon

Copy link
Copy Markdown

Cheers!

@baotruyenthach

Copy link
Copy Markdown

This method works!

@Anshir08

Copy link
Copy Markdown

Nice & Thanks

@jonathan-cartica

Copy link
Copy Markdown

very good very nice!

@idangrady

Copy link
Copy Markdown

Appreciated!!

@joabnd7

joabnd7 commented Dec 18, 2022

Copy link
Copy Markdown

Legend!

@Jay4BioPZ

Copy link
Copy Markdown

You are a hero :)

@intisy

intisy commented Feb 11, 2024

Copy link
Copy Markdown

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment