Python PIL裁剪没有裁剪图像

时间:2015-12-07 20:48:14

标签: python django image python-2.7 python-imaging-library

我有以下功能来裁剪图像:

def crop(original_image):


    original_image = Image.open(original_image)
    original_image.crop((25, 25, 50, 50))
    #original_image.load()

    thumb_io = StringIO.StringIO()
    original_image.save(thumb_io, format='JPEG')

    thumb_file = InMemoryUploadedFile(thumb_io, None, 'foo2.jpg', 'image/jpeg',
                                  thumb_io.len, None)


    return thumb_file

保存的图像只是没有任何尺寸编辑的原始图像。我尝试了负载而不是负载,但这并没有什么不同。

原始图像为300 x 450。

如果图像不能通过任何裁剪编辑进行保存,我做错了什么?

3 个答案:

答案 0 :(得分:4)

更改此行:

$.ajax({
    type     : "POST",
    url      : "https://apitest.authorize.net/xml/v1/request.api",
    data     : "<createTransactionRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
  <merchantAuthentication>
    <name>xxxxxxxxxxx</name>
    <transactionKey>xxxxxxxxxxx</transactionKey>
   </merchantAuthentication>
  <refId>123456</refId>
  <transactionRequest>
    <transactionType>authCaptureTransaction</transactionType>
    <amount>5</amount>
    <payment>
      <creditCard>
        <cardNumber>5424000000000015</cardNumber>
        <expirationDate>1220</expirationDate>
        <cardCode>999</cardCode>
      </creditCard>
    </payment>
    <order>
     <invoiceNumber>INV-12345</invoiceNumber>
     <description>Product Description</description>
    </order>
    <lineItems>
      <lineItem>
        <itemId>1</itemId>
        <name>vase</name>
        <description>Cannes logo </description>
        <quantity>18</quantity>
        <unitPrice>45.00</unitPrice>
      </lineItem>
    </lineItems>
    <tax>
      <amount>4.26</amount>
      <name>level2 tax name</name>
      <description>level2 tax</description>
    </tax>
    <duty>
      <amount>8.55</amount>
      <name>duty name</name>
      <description>duty description</description>
    </duty>
    <shipping>
      <amount>4.26</amount>
      <name>level2 tax name</name>
      <description>level2 tax</description>
    </shipping>
    <poNumber>456654</poNumber>
    <customer>
      <id>99999456654</id>
    </customer>
    <billTo>
      <firstName>Ellen</firstName>
      <lastName>Johnson</lastName>
      <company>Souveniropolis</company>
      <address>14 Main Street</address>
      <city>Pecan Springs</city>
      <state>TX</state>
      <zip>44628</zip>
      <country>USA</country>
    </billTo>
    <shipTo>
      <firstName>China</firstName>
      <lastName>Bayles</lastName>
      <company>Thyme for Tea</company>
      <address>12 Main Street</address>
      <city>Pecan Springs</city>
      <state>TX</state>
      <zip>44628</zip>
      <country>USA</country>
    </shipTo>
    <customerIP>192.168.1.1</customerIP>
    <transactionSettings>
      <setting>
        <settingName>testRequest</settingName>
        <settingValue>false</settingValue>
      </setting>
    </transactionSettings>
    <userFields>
      <userField>
        <name>MerchantDefinedFieldName1</name>
        <value>MerchantDefinedFieldValue1</value>
      </userField>
      <userField>
        <name>favorite_color</name>
        <value>blue</value>
      </userField>
    </userFields>
  </transactionRequest>
</createTransactionRequest>",
    dataType : "xml",
    success  : function(msg){
        console.log("success");
    }
    error    : function(msg) {
        console.log("fail");
    }
});

因为crop函数的返回值是一个rect区域,但是你没有保存它!

答案 1 :(得分:0)

看起来我需要将裁剪返回值提取到变量:

img_io = StringIO.StringIO()
    original_image = Image.open(original_image)
    new_image = original_image.crop((20, 20, 125, 125))
    new_image.save(img_io, format='JPEG', quality=100)
    img_content = ContentFile(img_io.getvalue(), 'foo6.jpg')

    return img_content

答案 2 :(得分:0)

您可能需要更改行

thumb_file = InMemoryUploadedFile(thumb_io, None, 'foo2.jpg', 'image/jpeg',
                              thumb_io.len, None)

thumb_file = ContentFile(thumb_io.getvalue(), 'foo2.jpg')

我猜您计划将图像文件保存到数据库中