Python Forum
cropping a picture (always square)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cropping a picture (always square)
#1
Hello,
I'am new to this forum and new into Python .

I want to cropping a picture automatic with a python script.
On the picture are flowers and i want only the flowers on without white edges.
I've found the perfect code to do it.


from PIL import Image, ImageChops

def trim(im) :
    print ('Foto Bewerking gestart')
    bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
    diff = ImageChops.difference(im, bg)
    diff = ImageChops.add(diff, diff, 1.8, -100)
    bbox = diff.getbbox()
    print (bbox)
    if bbox:
        return im.crop(bbox)
    
    
    
    

if __name__ == "__main__":
    bg = Image.open("test.jpg")  # The image to be cropped
    new_im = trim(bg)
    new_im.show ()
    new_im.save ("geknipt.jpg")  # result cropping
Its works great the only thing is the picture must Always be square without streching or disform the picture.

So i've set the print function to show the coordinates print (bbox)

Whit this data i can calculate how to crop the picture in the best way.

So the starting picture is 4000x4000 pixels.
If i do the cropping cycle it become 2000x3000 pixels.
Simpel math let see there is a difference of 1000.
So i need o add on bothe side 500 pixels to get the right cropping and make it square.


How can i do it ?
Reply
#2
Nobody see a sollution for my problem ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Im at square one even with trying to install python origen 1 358 Jan-12-2024, 05:39 AM
Last Post: ndc85430
Question image manipulation (cropping and MetaData) SpongeB0B 4 1,167 Jul-03-2023, 06:35 PM
Last Post: SpongeB0B
  openpyxl insert picture problem cools0607 2 1,563 May-03-2023, 06:48 AM
Last Post: cools0607
  How to increase the size of a png picture for the heatmap of the correlation? lulu43366 9 3,528 Oct-06-2021, 04:15 PM
Last Post: deanhystad
  Picture changing triggered by GPIO q_nerk 2 2,589 Dec-14-2020, 03:32 PM
Last Post: DeaD_EyE
  How to scrolling Picture in x axis kalihotname 1 2,257 Jun-16-2020, 12:18 PM
Last Post: DeaD_EyE
  square root of 5 input numbers mrityunjoy 1 2,030 Jun-10-2020, 11:08 AM
Last Post: perfringo
  square root of 6 input numbers mrityunjoy 3 2,609 Jun-07-2020, 06:35 AM
Last Post: ndc85430
  How to simplify square finding program? meknowsnothing 3 2,919 Jun-11-2019, 08:20 PM
Last Post: meknowsnothing
  How to get the picture from gender dataset vokoyo 1 2,459 May-03-2019, 12:20 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020