Is there a way to "auto-crop" an image in PIL? I have some PNG images
with white backgrounds that I want cropped to show only the non-white
portion in the center, but I don't see how I can do this.
--
Kevin Smith Ke*********@sas.com 5 9338
On Thu, 30 Oct 2003 14:42:36 +0000, Kevin Smith wrote: Is there a way to "auto-crop" an image in PIL? I have some PNG images with white backgrounds that I want cropped to show only the non-white portion in the center, but I don't see how I can do this.
Disclaimer: I've never used PIL, so I'm not familiar with it's interface.
Here's a basic algo:
From each of the four directions, scan towards the center until you
encouter a non-white pixel. Then use those 4 intercepts as your corners.
That is, the image is in the rectangle (left non-white, top non-white),
(right non-white, top non-white), (right non-white, bottom non-white),
(left non-white, bottom non-white)
Tyler Eaves wrote: That is, the image is in the rectangle (left non-white, top non-white), (right non-white, top non-white), (right non-white, bottom non-white), (left non-white, bottom non-white)
This only works if you actually know that the object you want to crop in
the middle is rectangular. If it's amorphous, you'll have to scan in
stripes.
--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ After a thousand years or so you go native.
\__/ Camden Benares
On Thu, 30 Oct 2003 12:54:37 -0800, Erik Max Francis wrote: Tyler Eaves wrote:
That is, the image is in the rectangle (left non-white, top non-white), (right non-white, top non-white), (right non-white, bottom non-white), (left non-white, bottom non-white)
This only works if you actually know that the object you want to crop in the middle is rectangular. If it's amorphous, you'll have to scan in stripes..
I dunno, to me a crop is by definition rectangular, and that's the way
it's worked in every image editor I've ever used.
Tyler Eaves wrote: I dunno, to me a crop is by definition rectangular, and that's the way it's worked in every image editor I've ever used.
I think I misunderstood your intention, you had the right algorithm. I
thought you were suggesting that, for example, to find the left bounds
of the cropping rectangle you pick an arbitrary y coordinate and then
start increment x from 0 until you hit on a non-white pixel. That
wouldn't have worked, since you have to check all y coordinates in
strips in case the shape of the figure you wish to crop is irregular.
But on rereading your response, that probably wasn't what you were
suggesting, so never mind.
--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ We are victims of our circumstance.
\__/ Sade Adu
Kevin Smith wrote: Is there a way to "auto-crop" an image in PIL? I have some PNG images with white backgrounds that I want cropped to show only the non-white portion in the center, but I don't see how I can do this.
import Image, ImageChops
##
# Crop borders off an image.
#
# @param im Source image.
# @param bgcolor Background color, using either a color tuple or
# a color name (1.1.4 only).
# @return An image without borders, or None if there's no actual
# content in the image.
def autocrop(im, bgcolor):
if im.mode != "RGB":
im = im.convert("RGB")
bg = Image.new("RGB", im.size, bgcolor)
diff = ImageChops.difference(im, bg)
bbox = diff.getbbox()
if bbox:
return im.crop(bbox)
return None # no contents
</F> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Manlio Perillo |
last post by:
Hi.
This post follows "does python have useless destructors".
I'm not an expert, so I hope what I will write is meaningfull and
clear.
Actually in Python there is no possibility to write code...
|
by: Glabbeek |
last post by:
I'm changing the layout of my site. Instead of using tables, I will use
DIVs. It's working fine, except for 1 thing: In IE6 some DIVs are not
the correct width. Mozilla and Opera are showing the...
|
by: Robert Downes |
last post by:
I'm using the following in a page that I'm testing in Mozilla:
p.actionLinkBlock {border: 1px #000000 dashed; padding: 0.2cm; width: auto}
But the dashed border is extending to the right-edge...
|
by: Vijay Kumar R. Zanvar |
last post by:
Hello,
Unlike register, auto keyword can not be used to
declare formal parameter(s). Is there any specific
reason for this?
Kind regards,
Vijay Kumar R. Zanvar
|
by: Alpha |
last post by:
I retrieve a table with only 2 columns. One is a auto-generated primary key
column and the 2nd is a string. When I add a new row to the dataset to be
updated back to the database. What should I...
|
by: Samuel |
last post by:
Hi,
I am running into a problem of mixing UICulture = auto and allowing users to
select culture using a dropdown list.
I am detecting a querystring, "setlang", and when found, setting the...
|
by: maya |
last post by:
at work they decided to center divs thus:
body {text-align:center}
#content {width: 612px; text-align:left; margin: 0 auto 0 auto; }
this works fine in IE & FF, EXCEPT in FF it doesn't work if...
|
by: nospam_news |
last post by:
I currently get asked about my usage of "auto". What is it for?
The keyword is clearly superflous here.
In contrast to the huge majority of C/C++ developers I write
definitions very explicitly...
|
by: Piotr K |
last post by:
Hi,
I've encountered a strange problem with Firefox which I don't have any
idea how to resolve. To the point: I've <divelement with a style
"height: auto" and I want to retrieve this value...
|
by: JOYCE |
last post by:
Look the subject,that's my problem!
I hope someone can help me, thanks
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
|
by: DizelArs |
last post by:
Hi all)
Faced with a problem, element.click() event doesn't work in Safari browser.
Tried various tricks like emulating touch event through a function:
let clickEvent = new Event('click', {...
| |