I'm writing a little desktop app using wxPython that does a bit of image manipulation, but I'm finding it's running rather slowly.
One of the biggest problems at the moment is that I am using three channels of data (RGB) where I only really need one - greyscale images are fine for my purposes.
At the moment I'm manipulating the images by loading them into a numpy array. Once the processing is done, they're converted into a wx Image object (via the ImageFromBuffer() function, which is then loaded into a StaticBitmap for the user interface. This seems like a lot of steps...
So I have two questions - the first is how would you load a numpy array directly into a greyscale wx Image? Is this even possible?
The second is more general - what is the fastest way of dealing with images in wxPython? I don't have much choice but to loop over the numpy arrays (I need the mathematical functionality), but any way of speeding things up before and after this process would be welcome!
you could do a pingpong with PIL like this :)
def convertToGrayscale(img):
import Image, ImageOps
orig = Image.new("RGB", img.GetSize())
orig.fromstring(img.GetData())
pil = ImageOps.grayscale(pil)
ret = wx.EmptyImage(pil.size[0], pil.size[1])
ret.SetData(pil.convert('RGB').tostring())
return ret
refer this link link
Related
from torchvision.utils import save_image
...
save_image(im, f'im_name.png')
In my case (standard mnist), using code from here, im is a Tensor:96, and save_image works.
I want that image in memory to show it in other plots, and I don't want to read it back after saving it, which seems kind of stupid.
Is there a way to separate the functionality of generating the image and of saving it?
Edit
clarification:
I want an equivalent to
save_image(im, f'im_name.png')
reread = plt.imread(f'im_name.png')
without saving the image and reading it back.
I just want the image, and I want to save it later.
the save_image function does some work, like stacking multiple images into one, converting the tensor to images of correct sizes and so on. I want only that part without the saving to disk.
About 2 weeks later, I stumbled upon the solution by accident.
grid = torchvision.utils.make_grid(im)
grid will be the image save_image was just saving.
I use python pillow to do a easy gif image reverse,but I found that the image has become more size(10m) than before(1m). Anyone know how to make it?
And here is my code:
from PIL import Image, ImageSequence
from PIL import ImagePalette
with Image.open('sd.gif') as im:
if im.is_animated:
frames = [f.copy() for f in ImageSequence.Iterator(im)]
frames.reverse()
frames[0].save('out.gif', save_all=True, append_images = frames[1:])
I can't tell for sure without examining the actual images, but I can guess what likely happened:
Some gifs are optimized with a method that finds pixels in each frame where nothing is changing (or changing only very slightly) from frame to frame, and make them transparent instead of storing the color for each pixel, to reduce the amount of data. For some gifs with large static areas in many consecutive frames this can be very efficient way to reduce file size.
When you are reversing the GIF, the frames must be unoptimized first, otherwise there would be transparent areas without any data. This can increase file size quite a bit. The difference may vary from one image to another.
You probably can solve this by running some gif optimization algorithm after the new image is created.
I'm making live video GUI using Python and Glade-3, but I'm finding it hard to convert the Numpy array that I have into something that can be displayed in Glade. The images are in black and white with just a single value giving the brightness of each pixel. I would like to be able to draw over the images in the GUI so I don't know whether there is a specific format I should use (bitmap/pixmap etc) ?
Any help would be much appreciated!
In the end i decided to create a buffer for the pixels using:
self.pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,0,8,1280,1024)
I then set the image from the pixel buffer:
self.liveImage.set_from_pixbuf(self.pixbuf)
I think these are the steps you need:
use scipy.misc.toimage to convert your array to a PIL image
check out the answer to this question to convert your PIL image to a cairo surface
use gdk_pixbuf_get_from_surface to convert this to a pixbuf (I don't know it's name in the python api)
make a Gtk.Image out of this using Gtk.Image.new_from_pixbuf
I'm sorry it needs so many conversion steps.
I want to change size of GIF animation image using python and PIL or PythonMagick. I can't find solution. PIL and thumbnail method works for jpg and png but not for gif. ImageMagick has command mogrify/convert -resize '1280x1024>' but i can't find documentation and i don't know how to do it with pythonmagick.
Anyone knows solution?
In the worst case i use os/subprocess and convert ;-S
Thanks.
You can use PIL and images2gif, a short PIL based module linked to on this blog page, and available here. Code used to process this rose.gif is below. I set the images2gif.readGif 'read as numpy array' property to false in order to get a list of PIL images so as I could use the PIL thumbnail function.
Orignial: Processed:
import Image
import images2gif
frames = images2gif.readGif("rose.gif",False)
for frame in frames:
frame.thumbnail((100,100), Image.ANTIALIAS)
images2gif.writeGif('rose99.gif', frames)
I'm not sure how to preserve transparency, my attempts to do so have failed (so far).
Some amazing person made an updated version of images2gif.py that accounts for transparency:
https://bitbucket.org/bench/images2gif.py/overview
There are still some artifacts, but it's way better than the original!
I am writing a photomosaic python application.
I want to know how to create the final image (.jpg) using multiple smaller images from a pool of images with Python/PIL. I have the order in which I want the various images to be in a 2-dimensional list.
Documented on the first PIL documenation page (paste() method):
http://effbot.org/imagingbook/image.htm
In addition:
http://www.daniweb.com/software-development/python/threads/128112
http://bytes.com/topic/python/answers/22566-overlaying-transparent-images-pil