18890

How to resize an image an cut the excess area of it in Python?

Question:

I have a string which contains the base64 code of an image. I need to resize this image to 591x608px, but I don't want it to be deformed if it has a different scale.

I don't know if this is the best way (please, correct me if there is a better one), but I'm trying to add a rectangle of 591x608px to the image through the PIL library.

I tried to do this with an image file and it worked, so I was trying to repeat the process with a base64 string instead of the name of the file. But I didn't achieve my purpose.

from cStringIO import StringIO from PIL import Image, ImageOps, ImageDraw size = (591, 608) mask = Image.new('L', size, 0) draw = ImageDraw.Draw(mask) draw.rectangle((0, 0) + size, fill=255) # From base64 to PIL image_string = StringIO(my_image.decode('base64')) im = Image.open(image_string) output = ImageOps.fit(im, mask.size, centering=(0.5, 0.5)) output.putalpha(mask) # From PIL to base64? output = base64.b64encode(output)

This is one of my many failed attempts. It gives me an error:

output = base64.b64encode(output) TypeError: must be string or buffer, not instance

Can anyone help me, please?

Answer1:

Finally, I found a solution.

This isn't actually adding a transparent area (so I'll edit the title of the question), but it's resizing the image to the specified scale cutting the excess part of the image if necessary:

from cStringIO import StringIO from PIL import Image, ImageOps, ImageDraw size = (591, 608) mask = Image.new('L', size, 0) draw = ImageDraw.Draw(mask) draw.rectangle((0, 0) + size, fill=255) # From base64 to PIL image_string = StringIO(my_image.decode('base64')) im = Image.open(image_string) output = ImageOps.fit(im, mask.size, centering=(0.5, 0.5)) output.putalpha(mask) # From PIL to base64 output2 = StringIO() output.save(output2, format='PNG') im_data = output2.getvalue() im_data = im_data.encode('base64') # data_url = 'data:image/png;base64,' + im_data.encode('base64')

Note that if you print <em>im_data</em>, you'll see weird symbols (in my case, I send this string to an URL and works). If you want to see the <em>im_data</em> without weird symbols, uncomment the last line.

Recommend

  • PIL: can't save the jpg pasted with a png
  • Generating color gradient in Python
  • A watermark inside a rectangle which filled the certain color
  • How to change the root element and list-item elements name? (django-rest-framework)
  • lxml.objectify and leading zeros
  • Python adding extra area to image
  • Bitshift operators description in Java language specification [closed]
  • sigprocmask during signal's execution
  • Linux Reading Data from UART
  • VB.Net Custom Mask for Date/Time AM/PM
  • Animate using a pixmap or image sequence in Python with QT4
  • DropShadowPanel adapt to button template style
  • Excel rendered from SSRS loses currency symbold when viewed on iPhone in UK
  • D3 Tree layout visualization - Inherit child with multiple parents
  • got OSError when use pillow to deal with my jpg image
  • Rendering a google line chart, curveType not setting and animation not working as expected
  • Replace any string in columns with 1
  • Difficulties implementing the Hysteresis step of Canny Algorithm in Halide without define_extern fun
  • Exactly what should happen in a CALayer's display/drawRect methods?
  • Displaying pdf files using the PDFKit interface
  • Smiles in output C++
  • WPF Image control clipping
  • Why do GeoJSON features appear like a negative photo of the features themselves?
  • Thread synchronization with syncwarp
  • Python PIL to extract number from image
  • Moving Android View and preventing onDraw to be called over and over again
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • Blackberry - Custom EditField Cursor
  • Python urlparse: small issue
  • Change multiple background-images with jQuery
  • FileReader+canvas image loading problem
  • Update CALayer sublayers immediately
  • Retrieving value from sql ExecuteScalar()
  • Android screen density dpi vs ppi
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Change an a tag attribute in JavaScript based on screen width
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Matrix multiplication with MKL
  • python draw pie shapes with colour filled