python - 无法通过 `python.subprocess` 管道子进程调整大小和转换 Django `model.save()` 中的图像

标签 python linux django-models subprocess

我想创建和修改网站图片,现在可以按照下面的代码在正确的位置创建图片,但我无法修改和格式化这些图片。

class Bookmark(models.Model):

    title = models.CharField(max_length=200)
    user = models.ForeignKey(User)       
    link = models.ForeignKey(Link)

    def __unicode__(self):
        return "%s, %s" % (self.user, self.link.url)
    def save_image(self):
        import subprocess
        import os
        url = self.link.url.replace("http://","").replace("https://","")\
                  .replace("/","|")
        image_png = './teststatic/url_image/' + url + ".png"
        image_jpg = './teststatic/url_image/' + url + ".jpg"
        command_line = "python", "webkit2png.py","-o", image_png, self.link.url
        image_crop = "mogrify", "-crop", "1280x1024+0+0", image_png
        image_convert = "mogrify", "-format", "jpg", image_png 
        image_del = "rm", image_png
        image_resize = "mogrify", "-resize", "150", "-quality", "80", image_jpg
        p1=subprocess.Popen(command_line, stdout=subprocess.PIPE)
        p2=subprocess.Popen(image_crop, stdin=p1.stdout, stdout=subprocess.PIPE)
        p3=subprocess.Popen(image_convert, stdin=p2.stdout, stdout=subprocess.PIPE)
        p4=subprocess.Popen(image_del, stdin=p3.stdout, stdout=subprocess.PIPE)
        subprocess.Popen(image_resize, stdin=p4.stdout, stdout=subprocess.PIPE)

这里是错误跟踪(为了便于阅读而重新格式化):

mogrify: unable to open image `./teststatic/url_image/z.cn.png': \
   png.la @ error/blob.c/OpenBlob/2489mogrify: unable to open image \
   `./teststatic/url_image/z.cn.png': png.la @ error/blob.c/OpenBlob/2489rm: \
    cannot remove `./teststatic/url_image/z.cn.png': No such file or directory
.
mogrify: unable to open image `./teststatic/url_image/z.cn.png':  \
    @ error/blob.c/OpenBlob/2489.
mogrify: unable to open file `./teststatic/url_image/z.cn.png' \
    @ error/png.c/ReadPNGImage/2951.
.
mogrify: unable to open image `./teststatic/url_image/z.cn.png': \
    @ error/blob.c/OpenBlob/2489.
mogrify: unable to open file `./teststatic/url_image/z.cn.png' \
    @ error/png.c/ReadPNGImage/2951.
mogrify: unable to open image `./teststatic/url_image/z.cn.jpg': \
    jpeg.la @ error/blob.c/OpenBlob/2489.
mogrify: unable to open image `./teststatic/url_image/z.cn.jpg': \
     @ error/blob.c/OpenBlob/2489.
rm: cannot remove `./teststatic/url_image/www.igoogle.com|.png': \
    No such file or directory
mogrify: unable to open image `./teststatic/url_image/www.igoogle.com|.png': \
    png.la @ error/blob.c/OpenBlob/2489.
mogrify: unable to open image `./teststatic/url_image/www.igoogle.com|.png': \
     @ error/blob.c/OpenBlob/2489.
mogrify: unable to open file `./teststatic/url_image/www.igoogle.com|.png'\
     @ error/png.c/ReadPNGImage/2951.
mogrify: unable to open image `./teststatic/url_image/www.igoogle.com|.png':\
     png.la @ error/blob.c/OpenBlob/2489mogrify: unable to open image \
    `./teststatic/url_image/www.igoogle.com|.jpg': \
    jpeg.la @ error/blob.c/OpenBlob/2489.
mogrify: unable to open image `./teststatic/url_image/www.igoogle.com|.png': \
     @ error/blob.c/OpenBlob/2489.
mogrify: unable to open file `./teststatic/url_image/www.igoogle.com|.png' \
    @ error/png.c/ReadPNGImage/2951.
rm: .
mogrify: unable to open image `./teststatic/url_image/www.igoogle.com|.jpg':  \
    @ error/blob.c/OpenBlob/2489.
cannot remove `./teststatic/url_image/www.z.cn|.png': No such file or directory
mogrify: unable to open image `./teststatic/url_image/www.z.cn|.png':\
     png.la @ error/blob.c/OpenBlob/2489.
mogrify: unable to open image `./teststatic/url_image/www.z.cn|.png': \
     @ error/blob.c/OpenBlob/2489.
mogrify: unable to open file `./teststatic/url_image/www.z.cn|.png'\
     @ error/png.c/ReadPNGImage/2951.
mogrify: unable to open image `./teststatic/url_image/www.z.cn|.png': \
    png.la @ error/blob.c/OpenBlob/2489.
mogrify: unable to open image `./teststatic/url_image/www.z.cn|.png':  \
    @ error/blob.c/OpenBlob/2489.
mogrify: unable to open file `./teststatic/url_image/www.z.cn|.png'\
     @ error/png.c/ReadPNGImage/2951.
rm: mogrify: unable to open image `./teststatic/url_image/www.z.cn|.jpg': \
    jpeg.la @ error/blob.c/OpenBlob/2489cannot remove `./teststatic/url_image
...
...

管道出现问题,如何解决?

最佳答案

您的第二个进程取决于第一个进程生成的文件。然而,当第二个过程开始时,第一个过程还没有完成,所以 png 图像还不存在。使用 subprocess.call()wait() 方法:

p1=subprocess.Popen(command_line, stdout=subprocess.PIPE)
return_code = p1.wait()
if return_code > 0:
    raise Exception('First process failed!')

...

关于python - 无法通过 `python.subprocess` 管道子进程调整大小和转换 Django `model.save()` 中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8492848/

相关文章:

python - 如何检查嵌套在循环内的字典中是否存在值

Python Decimal 模块不适用于 uint64

python - 了解 Python 中的生成器

linux - 重新编辑 netif_Rx 函数并启动相应的模块

c++ - 使用 C++ 获取 linux 机器的屏幕截图

python - Django 查菲尔德正则表达式

python - 如何继续处理车牌裁剪?

java - 如何使用 eclipse 的注释和 java 来编译我的程序?

python - 使用 Django 的 ORM 的模型继承方法

Django 在模型字段之间使用简单算术查询并与来自另一个模型的字段进行比较