PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 27, 2022

[FIXED] How to crop from one image and paste into another with PIL?

 July 27, 2022     crop, paste, python, python-imaging-library     No comments   

Issue

With PIL, I am trying to copy a rectangle out of an image, and paste it into another. This is my code:

import Image
ii = Image.open("ramza.png")
box = (70, 70, 30, 30)
region = ii.crop(box)
io = Image.open("template.png")
io.paste(region, box)
io.save("output.png")

And I am getting this error:

ValueError: images do not match

Do any of you know a fix to this?


Solution

A PIL crop box is defined as a 4-tuple of pixel coordinates: (left, upper, right, lower).

To fix your code to get a 30x30 crop:

box = (70, 70, 100, 100)

Broken down into components:

x, y, w, h = (70, 70, 30, 30)
box = (x, y, x + w, y + h)


Answered By - samplebias
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing