Python Forum
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Image : center and resizing
#1
Hi All,

i am trying to resizing the handwritten letter to 30x30 pixel using python.

In mspaint , i draw a letter "A" and try to resize the letter using the below code and it works.

I want to center the image  if my letter is on the left side then program should crop the right side and unwanted blank space]and resize the image. How can i achieve this.

from PIL import Image

input_img = Image.open('C:\Users\Admin\Desktop\letterA.jpg')
input_img.size

new_width  = 30
new_height = 30
input_img = input_img.resize((new_width, new_height), Image.ANTIALIAS)

input_img.save('C:\Users\Admin\Desktop\output.jpg')

output_img = Image.open('C:\Users\Admin\Desktop\output.jpg')
output_img.size
Reply
#2
Two good answers here: http://stackoverflow.com/questions/14211...python-pil

You can also use ImageMagick "trim" operator: http://www.imagemagick.org/Usage/crop/#trim

(You can use imagemagick by calling command line processes, but it also has several Python bindings: https://randomfoo.net/2013/02/21/python-and-imagemagick
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter Pillow Resize Image Looks Choppy While Resizing AaronCatolico1 2 1,399 Jan-03-2023, 05:41 PM
Last Post: AaronCatolico1
  (OpenCV) Help to improve code for object detection and finding center of it saoko 0 1,209 May-14-2022, 05:34 PM
Last Post: saoko
  [S0LVED] [Tkinter] Center dialog? Winfried 8 4,223 Mar-01-2022, 07:17 PM
Last Post: Winfried
  Program to move a dot towards a circle center plumberpy 10 4,223 Dec-03-2021, 12:20 PM
Last Post: BashBedlam
Question How to make a button always stay on a point when resizing the window? _ShevaKadu 5 4,227 Nov-04-2020, 03:59 PM
Last Post: deanhystad
  Center align Kristenl2784 1 1,969 Aug-03-2020, 04:25 PM
Last Post: bowlofred
  rotating and resizing objects jenya56 3 2,379 Jul-26-2019, 12:06 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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