Pypy load and show picture - image

How do I load a picture (file) and save it into multidimensional numpy array and show it? I need example for pypy.
In python I have source code:
from scipy import misc
import matplotlib.pyplot as plt
picture = misc.imread('parrot.jpg')
plt.imshow(newPicture)
plt.show()

Related

modify jpeg pixel data and keep all metadata intact

I'm editing a JPEG image (blurring faces) and want to modify as little data as possible, namely I'm trying to keep the metadata as intact as possible. However, all the libraries for reading and writing image files I've tried so far leave some of the metadata behind.
Here's what I'm currently doing in Python (with Pillow):
import PIL.Image
import numpy as np
image = PIL.Image.open(input_fpath)
blurred_data = blur_image(np.array(image_data))
image.frombytes(blurred_data.tobytes())
image.save(output_fpath, quality="keep", sampling="keep", qtables="keep", **image.info)
Which loses at least the comment and JFIF blocks. I'm thinking that maybe I need to interact directly with libjpeg but maybe there's some tool or library that I'm missing.

kivy image and PIL image

I can read an image using kivy camera. I wish to interpret a QR code in that image using code similar to this
texture = self.cameraObject.texture
size=texture.size
frame=texture.pixels
pil_image = Image.frombytes(mode='RGBA', size=size,data=frame)
pix=numpy.array(pil_image)
im=Image.fromarray(pix)
qrCodeDetector =cv2.QRCodeDetector()
decodedText, points, _ = qrCodeDetector.detectAndDecode(pix)
Kivy camera allows me to scan an image but to use the QR detector the image has to be converted to a numpy array
I have an import and then a class that starts off
from kivy.uix.image import Image
class KivyCamera(Image):
def __init__(self, **kwargs):
super(KivyCamera, self).__init__(**kwargs)
self.capture = None
# and so on
The program runs but methods that are in the PIL Image class are not available. For example Image.frombytes and Image.fromarray which is needed to convert an image to another.
Changing the Image import to
from PIL import Image
The program crashes with error message
class KivyCamera(Image):
TypeError: module.init() takes at most 2 arguments (3 given)
How do I either do the image conversion using the kivy Image which seems impossible or how do I get the init module to accept the PIL Image class?
You can import both Image classes like this:
from kivy.uix.image import Image
from PIL import Image as Pimage
Then, wherever you want to use the PIL Image, use Pimage:
pil_image = Pimage.frombytes(mode='RGBA', size=size,data=frame)

How to get image format using skimage?

from PIL import Image
im = Image.open('abc.jpg')
print(im.format) //output: JPEG
How can I write the above code using skimage?
I tried:
import skimage
from PIL import Image
im = skimage.io.imread('abc.jpg')
print(Image.fromarray(im).format)
Did not work for me.
I don't think you can do that with skimage.
When you load an image with PIL like this:
im = Image.open(path)
it returns a PIL Image object which stores the width, height, colourspace, palette and EXIF data.
When you load an image with skimage it just returns a pure Numpy array corresponding to the pixel colours so there is no place to store the format or anything else.

image plotted using matplotlib seems to have different colors than original JPEG image

The screenshot below compares the same image plotted using matplotlib on the left and Mac preview on the right.
Code for plotting the image using matplotlib is also fairly simple.
import matplotlib.pyplot as plt
import argparse
import skimage
parser = argparse.ArgumentParser(description='Color check.')
parser.add_argument('--image', required=False,metavar="path or URL to image")
args = parser.parse_args()
image = skimage.io.imread(args.image)
plt.imshow(image)
plt.show()
As you can see the colors are visibly different in the two images. Why is this happening and which one should I trust to be the correct color representation?
EDIT:
I plotted the image using opencv's imshow and it looks fine.
and here is the code:
import argparse
import cv2
windowName = "image"
cv2.namedWindow(windowName,cv2.WINDOW_NORMAL)
cv2.resizeWindow(windowName, 600,600)
parser = argparse.ArgumentParser(description='Color check.')
parser.add_argument('--image', required=False,metavar="path or URL to image")
args = parser.parse_args()
image = cv2.imread(args.image)
cv2.imshow(windowName, image)
cv2.waitKey(0)
The disagreement probably comes from the difference in colorspace used by the two programs. While Mac Preview is colorspace aware, and therefore should display images using the proper colorspace in which they are created/tagged, matplotlib uses the sRGB colorspace.
I would say that the color representation used by Mac Preview is most likely the one also used by the creator of the image, therefore the one I would pick.

How to convert an image that is in binary format to something analyzable?

I am studying some simple image processing techniques and came across a question. A task that I want to carry out is to find circles of fixed size in a given image. I'd like to write my own code for learning purposes.
The image that I have is in JPEG format which is in binary, how can I quantize the image so that I can do quantitative analysis on it? I want to calculate the cross-correlation between the template and the image to match the circle.
Thanks a lot for your help.
Can you try converting the jpeg binary into a matrix data-structure?
For ex: in Java you could use code like this to get matrix out of an image
using javax imageio package..
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
File file = new File("file.jpg");
BufferedImage img;
img = ImageIO.read(file);

Resources