Save Numpy array graphs and images as images - python

I cannot find a way to save my graphs and images. I have tried from
from PIL import Image
im = Image.fromarray()
im.save("your_file.jpeg")
but doesn't work!

Please format your code correctly, it is important for readability and python is intend-sensitive. Also, there is already a post for this problem:
How can I save an image with PIL?

Related

Image not able to show in Google colab using PIL library

from PIL import Image
visa_file='/content/visa-logo-mastercard.jpg'
img_visa= Image.open(visa_file,mode='r')
img_visa.show()
I am not getting the output of the image in Google colab.
Also the output of type(img_visa) is 'PIL.JpegImagePlugin.JpegImageFile'.
So the image object has been created, but the image is not displaying. And I only want to use PIL to show Image. How to do this?
Just
img_visa
is enough. You don't need
img_visa.show()
To be more explicit, you can use
display(img_visa)

Display image with format .mat in OpenCV Python

I am trying to replicate a code taht I did in MATLAB to Python, using OpenCV.
I have an image with .mat format that I managed to load, however I cannot displayed it using cv2.imshow(), since it does not supports it.
So does anyone know how I can visualize my image??
#Load image
import scipy.io as sio
matimage='img1.mat'
img = sio.loadmat(matimage)
cv2.imshow('Image', img)
Thanks in advance!

How to find the last modified png file using python or django

I want to find which file is last modified.
I am modifying or updating the 'png' image on each request.
For example: piechart.png is an image; I am modifying this same image to create a new image (having same image name 'piechart.png'). I will be saving & modifying 5 images: (piechart1.png,piechart2.png,... ,piechart5.png).
How can I find which image was modified last ?
Well, this doesn't really answer your question, but it come close.
What you can do, is first download the PIL module, or the Python Imaging Library.
After that, get it installed, and then do the following:
from PIL import Image
Jpeg = Image.open("Yourfile.jpg")
print(Jpeg.info, Jpeg.format, Jpeg.mode, Jpeg.size)
This gives you some of the properties of the photo.
The same thing you can do for PNG images.
For more information about using the PIL module and getting your desired results you can go here:
http://python.developpez.com/cours/pilhandbook/php/image.php
Hope this helps!

Loading greyscale images in wxPython

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

Resize GIF animation, pil/imagemagick, python

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!

Categories

Resources