Using python to save a jpg image that has been edited in a script

Referring to the answer to this question , I tried to save my own JPG image files after some basic image processing. I applied only rotation and shift. This is my code:

import numpy as np import sys from skimage import data, io, filter, color, exposure import skimage.transform as tf from skimage.transform import rotate, setup, AffineTransform from PIL import Image mypath = PATH_TO_FILENAME readfile = FILENAME img = color.rgb2gray(io.imread(mypath + readfile)) myimg = rotate(img, angle=10, order=2) afine_tf = tf.AffineTransform(shear=0.1) editedimg = tf.warp(myimg, afine_tf) # IF I UNCOMMENT THE TWO LINES BELOW, I CAN SEE THE EDITED IMAGE AS EXPECTED #io.imshow(editedimg) #io.show() saveimg= np.array(editedimg) result = Image.fromarray((saveimg).astype(np.uint8)) newfile = "edited_" + readfile result.save(path+newfile) 

I know that image processing was great because if I show it before saving it is just the original image with a little rotation and shift, as expected. But I am doing something wrong, keeping it. I tried without the astype(np.uint8)) part astype(np.uint8)) but got an error. Then I removed some of the code from the link mentioned above because I guessed that this was especially true for Fourier transforms, because when I included some of their code, I got an image that was gray, but with white lines in the shift direction I filed application. But now the image that is being saved is only 2 Kbytes, but only black.

And when I tried something simple:

 result = Image.fromarray(editedimg) result.save(path+newfile) 

then I got this error:

  raise IOError("cannot write mode %s as JPEG" % im.mode) IOError: cannot write mode F as JPEG 

I do not need to use PIL, if there is another way to just save my image, I am fine with that.

0
python numpy image-processing save image-formats
Jun 17 '14 at 2:08
source share
1 answer

Look at the PIL plug, Pillow is not so outdated and what you should probably use for this.

In addition, depending on your operating system, you may need several other libraries to properly build PIL with JPEG support, see here

It may also help. Says that before saving you need to convert the image to RGB mode.

 Image.open('old.jpeg').convert('RGB').save('new.jpeg') 
0
Jun 17 '14 at 14:21
source share



All Articles