Python subprocess: swipe image frame in imagemagick shell command

I have an image in memory and I want to execute the convertimagemagick method using Python subprocess. Although this line works well with the Ubuntu terminal:

cat image.png | convert - new_image.jpg

This piece of code does not work using Python:

jpgfile = Image.open('image.png');
proc = Popen(['convert', '-', 'new_image.jpg'], stdin=PIPE, shell=True)
print proc.communicate(jpgfile.tostring())

I also tried to read the image as a regular file without using PIL, I tried to switch between methods subprocessand different ways of writing to stdin.

The best part is that nothing happens, but I am not getting a real error. When you type stdout, I can see help using imagemagick on the terminal, and then the following:

By default, the image format of the `file 'is determined by its magic number. To specify a specific image format, precede the file name with the image format name and a colon (i.e. ps: image) or specify the image type as a suffix for the file name (i.e. image.ps). Specify 'file' as '-' for standard input or output. (No no)

Maybe there’s a hint here that I don’t get. Please point me in the right direction, I'm new to Python, but from my experience with PHP this should be a very simple task, or I hope.

Edit:

This is the solution that I ultimately used to process the PIL image object without saving the temporary file. Hope this helps someone. (in the example, I am reading a file from a local drive, but the idea is to read the image from a remote location)

out = StringIO()
jpgfile = Image.open('image.png')
jpgfile.save(out, 'png', quality=100);
out.seek(0);
proc = Popen(['convert', '-', 'image_new.jpg'], stdin=PIPE)
proc.communicate(out.read())
+4
1

, - , , imagemagick, , tostring() imagemagick. linux, :

from subprocess import Popen,PIPE
proc = Popen(['cat', 'image.jpg'], stdout=PIPE)

p2 = Popen(['convert', '-', 'new_image.jpg'],stdin=proc.stdout)

proc.stdout.close()
out,err = proc.communicate()

print(out)

, shell = True, shell = True, :

from subprocess import check_call
check_call('cat image.jpg | convert - new_image.jpg',shell=True)

shell=True. , shell = True.

stdin:

with open('image.jpg') as jpgfile:
    proc = Popen(['convert', "-", 'new_image.jpg'], stdin=jpgfile)

out, err = proc.communicate()

print(out)

, check_call, CalledProcessError, , :

from subprocess import check_call, CalledProcessError


with open('image.jpg') as jpgfile:
    try:
        check_call(['convert', "-", 'new_image.jpg'], stdin=jpgfile)
    except CalledProcessError as e:
        print(e.message)

stdin, , , .read:

with  open('image.jpg') as jpgfile:
     proc = Popen(['convert', '-', 'new_image.jpg'], stdin=PIPE)
     proc.communicate(jpgfile.read())

, , tempfile:

import tempfile
import requests

r = requests.get("http://www.reptileknowledge.com/images/ball-python.jpg")
out = tempfile.TemporaryFile()
out.write(r.content)
out.seek(0)

from subprocess import check_call
check_call(['convert',"-", 'new_image.jpg'], stdin=out)

CStringIo.StringIO:

import requests

r = requests.get("http://www.reptileknowledge.com/images/ball-python.jpg")
out = cStringIO.StringIO(r.content)

from subprocess import check_call,Popen,PIPE
p = Popen(['convert',"-", 'new_image.jpg'], stdin=PIPE)

p.communicate(out.read())
+3

All Articles