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.
Related
I am trying to create an image made up of coloured squares. I only need each square to be one pixel large, as it is just a single block colour. However, when I use this code, the image generated is extremely blurry. Is there anyway to make the boarders sharp?
def fancycolnw2(seq,m):
data=numbwall(seq,m)
#print(data)
for i in range(len(data)):
for j in range(len(data[i])):
if data[i][j]==' ':
data[i][j]=-1
im = Image.new('RGBA', (len(data[0]),len(data))) # create the Image of size 1 pixel
#print(data)
for i in range(len(data)-1):
for j in range(len(data[i])-1):
#print(i,j)
if data[i][j]==-1:
im.putpixel((j,i), ImageColor.getcolor('black', 'RGBA'))
if data[i][j]==0:
#print('howdy')
im.putpixel((j,i), ImageColor.getcolor('red', 'RGBA'))
if data[i][j]==1:
im.putpixel((j,i), ImageColor.getcolor('blue', 'RGBA'))
if data[i][j]==2:
im.putpixel((j,i), ImageColor.getcolor('grey', 'RGBA'))
im.show()
im.save('simplePixel.png') # or any image format
The result I get looks like this:
Image
It is the correct image, I just wish the boundaries between pixels were sharp. Any help would be greatly appreciated!
The image is perfectly sharp, but rather small. I suspect that you are "zooming in" to view it clearer, and that whatever program you are zooming with is filtering the image, because with most images this looks better. You need to find a viewing program that uses "nearest neighbour" resampling when zooming in, or generate a larger image to start with, for example by setting a 4-by-4 pixel block rather than individual pixels.
(Also, the code says "# or any other image format". Don’t use JPEG for this, as the lossy compression will likely wreck your image.)
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?
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 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 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.