from PIL import Image, ImageFilter
import os
def open_image(path):
try:
img = Image.open(path)
img.show()
return img
except Exception as e:
print(f"Lỗi khi mở ảnh: {e}")
return None
def resize_image(img, new_width, new_height):
return img.resize((new_width, new_height))
def rotate_image(img, degrees):
return img.rotate(degrees)
def apply_filter(img, filter_type):
if filter_type == 'BLUR':
return img.filter(ImageFilter.BLUR)
elif filter_type == 'CONTOUR':
return img.filter(ImageFilter.CONTOUR)
elif filter_type == 'DETAIL':
return img.filter(ImageFilter.DETAIL)
else:
print("Loại bộ lọc không hợp lệ.")
return img
def save_image(img, path):
try:
img.save(path)
print(f"Ảnh đã được lưu tại {path}")
except Exception as e:
print(f"Lỗi khi lưu ảnh: {e}")
def main():
print("Phần mềm xử lý ảnh đơn giản")
image_path = input("Nhập đường dẫn tới ảnh: ")
img = open_image(image_path)
if not img:
return
while True:
print("\nCác thao tác có thể thực hiện:")
print("1. Thay đổi kích thước")
print("2. Xoay ảnh")
print("3. Áp dụng bộ lọc")
print("4. Lưu ảnh")
print("5. Thoát")
choice = input("Chọn thao tác (1-5): ")
if choice == '1':
width = int(input("Nhập chiều rộng mới: "))
height = int(input("Nhập chiều cao mới: "))
img = resize_image(img, width, height)
img.show()
elif choice == '2':
degrees = float(input("Nhập số độ xoay (ví dụ: 90): "))
img = rotate_image(img, degrees)
img.show()
elif choice == '3':
print("Các bộ lọc có sẵn: BLUR, CONTOUR, DETAIL")
filter_type = input("Chọn bộ lọc: ").upper()
img = apply_filter(img, filter_type)
img.show()
elif choice == '4':
save_path = input("Nhập đường dẫn lưu ảnh (ví dụ: edited_image.jpg): ")
save_image(img, save_path)
elif choice == '5':
print("Thoát chương trình.")
break
else:
print("Lựa chọn không hợp lệ. Vui lòng thử lại.")
if __name__ == "__main__":
main()