I want to draw some rectangles on the bmp images. And I have try some method:
im = np.array(Image.open('lenna_256.bmp'), dtype=np.uint8)
cv2.rectangle(im,(491,267),(529,305),(0,255,0),3)
cv2.rectangle(im,(10,510),(375,875),(0,255,0),3)
plt.imshow(im)
but the result shows that:
The rectangle I draw shows here
However, I want to draw the rectangles directly on the original image, instead of the greyscale one, how could I solve this?
Related
I have this image:
that I'm trying to paste on to this one:
I want to get rid of the black background (so I'm assuming "make it transparent"?).
I tried applying a black mask, but
the result is this:
I'm not quite understanding how to use a mask properly.
I thought creating a black mask would get rid of the black portions.
from PIL import Image
image = Image.open(r'..jpg')
image_2 = Image.open(r'...jpg')
image_copy = image.resize((300,300)).copy() #fish image
mask=Image.new('L', (300,300), color=0)
position = ((200,100))
image_2.paste(image_copy, position, mask = mask)
image_2.save('testing_1.png')
EDIT: Playing around with the code a bit more I'm able to overlay the image onto the background,
but it still has the black background surrounding it.
I would like to get RID of the black background.
I've been trying to get thisimage to automatically crop to the smallest size possible, removing the transparent bits around it. I can't just crop this image myself manually, as more things will be added on the image like this .
I've been using this code:
from PIL import Image, ImageChops
image=Image.open('headbase1.png')
image.load()
imageSize = image.size
imageBox = image.getbbox()
print(image.getbbox())
cropped=image.crop(imageBox)
cropped.save('headbase_end.png')
It does not crop out the transparency around it, and the bounding box is this (0, 0, 45, 45), which I do not think is right.
Thanks, VOT.
Edit, this does work: Automatically cropping an image with python/PIL with that image, however it refuses to work for my image. .
getbbox doesn't work on PNGs with alpha channels: image.mode == 'RGBA'
First remove the alpha channel and then obtain the bounding box. image.convert('RGB').getbbox()
I have written a python code using PIL and Tkinter where I display an image and put a red circle on the image where a user clicked. Here is the relevant code.
def paint_img(event, canvas):
x, y = event.x, event.y
image_draw.ellipse((x-10, y-10, x+10, y+10), fill='red')
canvas._image_tk = ImageTk.PhotoImage(pilImg)
canvas.itemconfigure(canvas._image_id, image=canvas._image_tk)
It seems that it works with some images, but in some cases it displays a grey circle. I used identify on the test images, for the successful one the output is
totestcolor.jpg JPEG 561x549 561x549+0+0 8-bit DirectClass 18.3kb
for the unsuccessful one, the output is:
totestcolor1.jpg JPEG 1533x1095 1533x1095+0+0 8-bit PseudoClass 256c 70.4kb
I want to know why this is happening and if there is any way to output a red circle for the unsuccessful image as well.
That's because the original image is grayscale. You can convert it to full RGB before drawing the red circle. In the PIL library, it is code like this:
pilImg = pilImg.convert("RGB") # or "RGBA" to keep transparency
I am trying to display png image with transparency on black background, moving it slowly around on surface, but a lot of frames contain white pixels. (that should not be there at all)
The logo surface is created like this:
self.logo_surface = cairo.ImageSurface.create_from_png(image)
And every frame is drawn like this:
I move it to a new position and then scale image down to 15x15px
def draw(self, ctx):
# scale image and move it to a right place
ctx.save()
ctx.translate(self.left, self.top)
ctx.scale(self.scale_xy, self.scale_xy)
ctx.set_source_surface(self.logo_surface)
#Here i tried different filters but without improvement in reducing white flashes
ctx.get_source().set_filter(cairo.FILTER_FAST)
ctx.paint()
ctx.restore()
left, top, scale_xy are float. Left and top are changed a bit every frame.
How could i prevent those flashes?
EDIT:
1)The data from the final surface is extracted using get_data.
buf = self.surface.get_data()
a = numpy.frombuffer(buf, numpy.uint8)
2)This effect does not happen when translate is given integer values:
ctx.translate(int(self.left), int(self.top))
but then the image does not move smoothly anymore on 100x100px surface
The wikimedia logo contained some white pixels between transparent and colored part. Because it is a big picture it cannot be seen unless zoomed very close. If downscaled with cairo, it sometimes happened to use those "white" pixel values - hence flashing white pixels on the image.
I am working with multiple images that I would like to stack on top of each other to create a single image. However, in working with them, I'm noticing that if the image already has transparency (alpha != 255), that part of the image appears faded. If there is no transparency, all is good.
I saved one of the images I was working with to a PNG and created a small bit of code that duplicates the problem. Essentially, I'm creating a new image with a transparent background and then pasting the image on top:
from PIL import Image
img=Image.new('RGBA', (946,627), (0,0,0,0))
overlayImage = Image.open('drawing.png')
img.paste(overlayImage, (0,0), overlayImage)
img.save('drawing-pasted.png')
When this completes, drawing-pasted.png looks like this:
But the original drawing (drawing.png) looked like this:
(Images cropped manually to show detail.) The original image circles fill color has an alpha value of 179.
Has anyone else encountered this, and what am I doing wrong?
Thanks much.
image = Image.open(file_path)
img1 = Image.open(file_path)
cords = (233.22)
image.paste(img1, cords, mask=img1)
image.save(path_where_you_want_to_save_final_image)
simply use this block of code
The background you are creating is black and fully transparent the original is blue but with an alpha of 179 so you have 2 pixels (0,0,0,0) and (0,0,255,179) assuming 100% blue - since you are pasting the image in it will be over the background so will use the alpha of the new image allowing (255-179)/255 or about 30% black. (N.B. The alpha of the background makes no difference since it is behind the new image)
You could use overlayImage.putalpha to set the alpha to 255 start from your image rather than a black background.