繁体   English   中英

使用python保存在脚本中编辑的JPG图像

[英]Using python to save a JPG image that was edited in the script

参考此问题的答案,我尝试在进行一些基本的图像处理后保存自己的JPG图像文件。 我只应用了旋转和剪切。 这是我的代码:

        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)

我知道图像处理很好,因为如果在保存之前显示它,它就是原始图像,并且具有预期的旋转和剪切效果。 但是保存时我做错了事。 我尝试了没有astype(np.uint8))部分,但出现了错误。 然后我从上面提到的链接中删除了一些代码,因为我猜想它特别适用于傅立叶变换,因为当我包含它们的一些代码时,我得到的图像全是灰色的,但在剪切方向上有白线我申请了。 但是现在保存的图像只有2KB,只有黑度。

当我尝试一些简单的事情时:

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

然后我得到这个错误:

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

我真的不需要使用PIL,如果还有另一种方法可以简单地保存我的图像,那么我很好。

查看PIL分支Pillow并没有过时,您可能应该为此使用什么。

另外,根据您的操作系统,可能还需要一些其他库来正确编译具有JPEG支持的PIL, 请参见此处

这也可能有助于说保存之前需要将图像转换为RGB模式。

Image.open('old.jpeg').convert('RGB').save('new.jpeg')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM