import cloudinary
import cloudinary.uploader
import cloudinary.api
from IPython.display import display, HTML
# Configure Cloudinary with your credentials
cloudinary.config(
cloud_name='dtfszqiev',
api_key='515185372763299',
api_secret='HZ3CoCHlJB6XpT7RXWp8aJEqr-c'
)
# 1. Upload an image
def upload_image():
file_path = input("Enter the path to your image file: ")
try:
result = cloudinary.uploader.upload(file_path, resource_type="image")
return result['public_id']
except Exception as e:
print(f"Error uploading image: {e}")
return None
# 2. Edit image function
def edit_image(public_id):
print("What would you like to do with the image?")
print("1. Resize")
print("2. Crop")
print("3. Add text overlay")
print("4. Adjust saturation")
print("5. Adjust hue")
print("6. Apply grayscale filter")
choice = input("Enter your choice (1-6): ")
transformation = []
if choice == '1':
width = input("Enter new width: ")
height = input("Enter new height: ")
transformation.append({"width": width, "height": height, "crop": "scale"})
elif choice == '2':
width = input("Enter crop width: ")
height = input("Enter crop height: ")
x = input("Enter x coordinate: ")
y = input("Enter y coordinate: ")
transformation.append({"crop": "crop", "width": width, "height": height,
"x": x, "y": y})
elif choice == '3':
text = input("Enter text to overlay: ")
font_size = input("Enter font size: ")
color = input("Enter text color (e.g., white, black, etc.): ")
transformation.append({"overlay": {"font_family": "Arial", "font_size":
int(font_size), "text": text, "color": color}, "gravity": "south", "y": 10})
elif choice == '4':
saturation = input("Enter saturation level (-100 to 100): ")
transformation.append({"effect": f"saturation:{saturation}"})
elif choice == '5':
hue = input("Enter hue degree (0 to 360): ")
transformation.append({"effect": f"hue:{hue}"})
elif choice == '6':
transformation.append({"effect": "grayscale"})
else:
print("Invalid choice")
return None
try:
# Apply transformation using CloudinaryImage
image_url =
cloudinary.CloudinaryImage(public_id).build_url(transformation=transformation)
return image_url
except Exception as e:
print(f"Error editing image: {e}")
return None
# 3. Main function to upload and edit the image
def main():
public_id = upload_image()
if public_id:
image_url = edit_image(public_id)
if image_url:
display(HTML(f'<img src="{image_url}" alt="Edited Image"/>'))
if __name__ == "__main__":
main()