Skip to content

Instantly share code, notes, and snippets.

@lov3catch
Created August 17, 2015 11:25
Show Gist options
  • Save lov3catch/0f21069960da0f596ad7 to your computer and use it in GitHub Desktop.
Save lov3catch/0f21069960da0f596ad7 to your computer and use it in GitHub Desktop.
import base64
from PIL import Image
def img_2_base64(img_path):
"""
Convert img to base64
"""
with open(img_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
return encoded_string
def base64_2_img(img_path, base64_obj):
"""
Convert base64 to img
"""
img = base64.b64decode(base64_obj)
with open(img_path, 'wb') as image_file:
image_file.write(img)
def cropp_it(img_path, x_crop=None, y_crop=None):
"""
Cropping image
"""
img = Image.open(img_path)
w, h = img.size
top = 881
left = 833
right = 2833
buttom = 1881
print left, top, right, buttom
crop_img = img.crop((left, top, right, buttom))
crop_img.save('crop.jpg')
# crop_img.show()
if __name__ == '__main__':
img_path = '/home/igor/PycharmProjects/img_cropping/img_name.jpg'
copy_img_path = '/home/igor/PycharmProjects/img_cropping/img_name_copy.jpg'
test_img_base64 = img_2_base64(img_path)
base64_2_img(copy_img_path, test_img_base64)
cropp_it(copy_img_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment