Guest User

Untitled

a guest
Mar 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. # -*-coding:utf-8 -*-
  2. from PIL import Image
  3.  
  4. def resize(png_path, base_width):
  5. img = Image.open(png_path)
  6. wpercent = (base_width / float(img.size[0]))
  7. hsize = int((float(img.size[1]) * float(wpercent)))
  8. resized_img = img.resize((base_width, hsize), Image.ANTIALIAS)
  9. return resized_img
  10.  
  11. def png_to_jpg(png_path, target_img):
  12. fill_color = None
  13. img = target_img
  14. jpg_path = png_path[:-4]+'.jpg'
  15.  
  16. if img.mode in ('RGBA', 'LA'):
  17. background = Image.new(img.mode[:-1], img.size, fill_color)
  18. background.paste(img, img.split()[-1])
  19. img = background
  20.  
  21. img.save(jpg_path, 'JPEG')
  22.  
  23. def resize_png2jpg(png_path, base_width):
  24. img = resize(png_path, base_width)
  25. png_to_jpg(png_path, img)
  26.  
  27. # 사용
  28. resize_png2jpg("people.png", 1600)
Add Comment
Please, Sign In to add comment