gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:39:45 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com