Frame function in Wand does't work on Mac? - python

I am trying to add a border around an image using the image.frame function in Python Wand:
image.frame(width=2, height=2)
This seems to work fine on a Raspberry Pi, but converts the image to grayscale on the Mac. Any clues?
from wand.image include *
from wand.display include *
# tryme.jpg is color jpg image
image=Image(filename="tryme.jpg")
image.frame(width=2, height=2)
display(image)
Should display a color picture with 2 pixel border around it.

This works fine for me on ImageMagick 6.9.10.57 Q16 Mac OSX Sierra using Python Wand 0.5.5. Note my syntax is different from your.
#!/bin/python3.7
from wand.image import Image
from wand.display import display
with Image(filename='lena.jpg') as img:
img.frame(width=10, height=10)
img.save(filename='lena_frame.jpg')
display(img)
ADDITION:
It actually fails with ImageMagick 7 as per the question asked above. I have reported a bug to the Python Wand developer.

Related

How i can open PNG w alpha channel, if he is white

I have a lot images like this:
Screenshot from ImageMagick
When I open it image in cv2, PIL I see only white canvas without any alpha channel
I started looking for the difference between these files, and those who open correctly and in ImageMagick saw this line Alpha: srgba(255,255,255,0) #FFFFFF00 screenshot
Valid files have Alpha: srgba(0,0,0,0) #00000000
File link: https://drive.google.com/file/d/1tN9D1vjwbnHg8urYJzHqI4zljS110oun/view?usp=sharing
Help:)
UPD. The question is closed. Files must be opened with the flag IMREAD_UNCHANGED
I should have written like this
cv2.imread('file.png', cv2.IMREAD_UNCHANGED)

Tkinter image with alpha channel works as desired in windows but not linux

I am using PIL and Numpy to add alpha channel to an image before displaying it in Tkinter GUI. I first read the image, convert it to "RGBA", convert it to array, add the alpha channel, convert back to image, display in Tkinter GUI.
Here is my code:
import numpy as NP
import PIL.Image, PIL.ImageTk
from Tkinter import *
root=Tk()
image = PIL.Image.open('A.png')
image = image.convert('RGBA')
Arr=NP.array(image)
#apply alpha of 100
Arr[..., 3] = 100
TKimg=PIL.ImageTk.PhotoImage(PIL.Image.fromarray(Arr))
Label(root, image=TKimg,background='white').pack()
root.mainloop()
This is the result I got when running the code on windows and linux.
The original image has a white background. After adding alpha in windows it is still has the white background "which I want". However, in linus the white background is destroyed.
So how to apply alpha and keep the white background in linux
Is there any method to achieve this using python
Thanks

PyGame image save colors distorted

So I have implemented the following screenshot functionality into my game just to log progress and stuff like that. This is my code:
pygame.image.save(screen, save_file)
Pretty basic. I recently upgraded to python 3.3 and have since been having the issue of distorted colors using this function. Here is what I mean:
Distorted Color:
So it looks quite nice, but it isn't how it supposed to be. This is the actual image:
Is this a known issue or is it just me? Are there any fixes to it or is it just a broken function at the moment. I am using pygame 1.9.2pre and I am assuming it is just a bug with the pre release but I was having issues using any other versions of pygame with python 3.3.
Some users have reported difficulty with saving images as pngs:
I only get .tga files even when I specify .png. Very frustrating.
If you use .PNG (uppercase), it will result in an invalid file (at least on my win32). Use .png (lowercase) instead.
PNG does not seem to work, I am able to get a preview of it in Thunar, but everywhere else It says that it is not a valid PNG.
Saving in a different format may be helpful. For example, BMP is a simple format, so it's unlikely that Pygame's implementation will be buggy.
If you really want to save as PNG, you can reverse the distortion by swapping the red channel with the green one. This is fairly easy. For example, using PIL:
from PIL import Image
im = Image.open("screenshot.png")
width, height = im.size
pix = im.load()
for i in range(width):
for j in range(height):
r,g,b = pix[i,j]
pix[i,j] = (g,r,b)
im.save("output.png")
Or you can save as BMP and convert to PNG after the fact:
from PIL import Image
im = Image.open("screenshot.bmp")
im.save("screenshot.png")
for future reference, this trick worked for me:
from PIL import Image
imgdata = pygame.surfarray.array3d(screen).transpose([1,0,2])[:,:,::-1]
Image.fromarray(imgdata).save('output.png')

PIL Image mode "P" -> "RGBA"

This is my issue:
import Image
im = Image.open("1.png")
im.show()
print im.mode
im.convert("RGBA").save("2.png")
Well, with my image you can see the difference.
My question is: how do I convert it properly?
Image:
Result:
NOTE: The original image has a semi-transparent glow, the result has a solid green "glow"
This issue was reported here:
https://bitbucket.org/effbot/pil-2009-raclette/issue/8/corrupting-images-in-palette-mode
In March 2012, a comment says it's now fixed in development version of PIL. The most recent released version is 1.1.7, so the fix won't be available until 1.2 comes out. PIL updates very slowly, so don't expect this to come out soon.
Unfortunately your PNG image is a type that PIL doesn't handle very well - a paletted image with an alpha channel. When you open the image, the alpha is thrown away and there's no way to get it back.
This is different from the usual palette transparency where one index of the palette is used to denote fully transparent pixels.
You could use scipy.misc.imread:
img = scipy.misc.imread(filename, mode='RGBA')
img = Image.fromarray(img)
Your problem is that you do not provide info about what PIL should use as source of ALPHA channel.
PIL will not on its own add transparency to your image.
What part of your image you want to be transparent?

Drawing Hebrew text to and image using Image module (python)

This is an issue I already asked about and several got answers but the problem remained.
when I try to write in hebrew to an image using Image module I get instead of the hebrew lettring some other (ascii??) lettering. if I convert to unicode or ascii I get an error that it doesn't support. I got here a reference to a code that does what I want in chinese:
import sys
import Imag
import ImageDraw
import ImageFont
import _imaging
txt = '你好,世界!'
font = ImageFont.truetype('c:/test/simsun.ttc',24)
im = Image.new("RGBA",(300,200),(0,0,0))
draw = ImageDraw.Draw(im)
#draw.text( (0,50), u'你好,世界!', font=font)
draw.text( (0,50), unicode(txt,'UTF-8'), font=font)
but then I get an error:ImportError:
The _imagingft C module is not installed.
the same goes when I try to use standrad hebrew font 'arial.ttf' (with hebrew string ofcourse). as you can see I have imported _imaging succsefuly so the problem doesn't lay there as suggested by effbot.org.
it seem that the problem is with the Imagefont.truetype(...).
any help will be very appriciated
Sounds like PIL was built without FreeType support. Install the FreeType dev files and rebuild PIL again.
the problem was the PIL 1.1.7 doesn't work well with windows XP. the same code runs well under linux or with XP but with PIL 1.1.6
mystory is solved

Categories