Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

final changes #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions console_picsart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from gimpfu import *
import gtk
import webbrowser

def show_styled_message_dialog_with_link():
dialog = gtk.Dialog(
"My account",
None,
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
(gtk.STOCK_OK, gtk.RESPONSE_OK)
)

label = gtk.Label()
label.set_markup("<span font='12' >To manage your API key, click the button below or visit:</span> \n\n"
"<a href='https://console.picsart.io/'>https://console.picsart.io/</a>")

label.set_padding(10, 10)

button = gtk.Button("Go to Console")

button.set_size_request(100, 40)
button.set_property("can-default", True)

button.connect("clicked", lambda w: webbrowser.open("https://console.picsart.io/"))

dialog.vbox.pack_start(label, True, True, 10)
dialog.vbox.pack_start(button, True, True, 10)

label.show()
button.show()

dialog.run()
dialog.destroy()

def goToConsole(i, d):
show_styled_message_dialog_with_link()
register(
"python_fu_console_picsart",
"Console Picsart Io",
"Show API key balance.",
"Erik",
"Torosyan",
"2024",
"<Image>/Picsart/My Account",
"*",
[],
[],
goToConsole
)

main()
134 changes: 134 additions & 0 deletions remove_background_picsart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# -*- coding: utf-8 -*-
from gimpfu import *
import urllib2
import json
import tempfile
import os
def read_api_key():
file_name = ".picsart_api_key.txt"
home_directory = os.path.expanduser("~")
file_path = os.path.join(home_directory, file_name)

if os.path.exists(file_path):
with open(file_path, "r") as f:
api_key = f.read().strip()
if api_key:
return api_key
else:
return False
else:
return False

def remove_background_from_current_image(image, drawable):
api_key = read_api_key()

supported_types = ['.jpg', '.png', '.tiff', '.webp', '.mpo']

image_filename = pdb.gimp_image_get_filename(image)
if image_filename:
file_extension = os.path.splitext(image_filename.lower())[1]
if file_extension not in supported_types:
gimp.message("\nUnsupported file type: {}. Supported types are: JPEG, PNG, TIFF, WEBP, MPO.\n".format(file_extension.upper()))
return
else:
gimp.message("\nError: Cannot determine the file type.\n")
return

if api_key:
url = "https://api.picsart.io/tools/1.0/removebg"
headers = {
"X-Picsart-API-Key": api_key,
"accept": "application/json",
"User-Agent": "Mozilla/5.0",
"Referer": "https://www.picsart.com/",
"Origin": "https://www.picsart.com"
}

temp_image_path = tempfile.mktemp(suffix=file_extension)
pdb.gimp_file_save(image, drawable, temp_image_path, temp_image_path)

with open(temp_image_path, "rb") as image_file:
file_content = image_file.read()

boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW'
data = (
'--' + boundary + '\r\n' +
'Content-Disposition: form-data; name="image"; filename="image' + file_extension + '"\r\n' +
'Content-Type: image/' + file_extension[1:] + '\r\n\r\n' +
file_content +
'\r\n--' + boundary + '--\r\n'
)

request = urllib2.Request(url, data)
request.add_header('Content-Type', 'multipart/form-data; boundary={}'.format(boundary))
for key, value in headers.items():
request.add_header(key, value)

try:
response = urllib2.urlopen(request)
response_data = json.loads(response.read())
print("Response of API:", response_data)

if response_data.get("data"):
image_url = response_data["data"]["url"]

try:
request_image = urllib2.Request(image_url)
request_image.add_header('User-Agent', 'Mozilla/5.0')
request_image.add_header('Referer', 'https://www.picsart.com/')

response_1 = urllib2.urlopen(request_image)
image_data = response_1.read()

except urllib2.HTTPError as e:
gimp.message("HTTP Error: " + str(e.code))
print("Error HTTP:", e.code, e.read())
return
except urllib2.URLError as e:
gimp.message("URL Error: " + str(e.reason))
print("Error URL:", e.reason)
return

temp_file_out = tempfile.NamedTemporaryFile(delete=False, suffix=".png")#this momnt
temp_file_out.write(image_data)
temp_file_out.close()

new_image = pdb.gimp_file_load(temp_file_out.name, temp_file_out.name)

if new_image:
gimp.Display(new_image)
gimp.displays_flush()
else:
gimp.message("\nError loading image in GIMP\n")

os.remove(temp_file_out.name)
else:
gimp.message("\nError in response: " + str(response_data))

except urllib2.HTTPError as e:
gimp.message("\nThere is an issue with the API key. Please use the 'Set API Key' button to update it, or click the 'My Account' button to retrieve your API key.\n")

except urllib2.URLError as e:
gimp.message("\nURL error: " + str(e.reason))
finally:
if os.path.exists(temp_image_path):
os.remove(temp_image_path)
else:
gimp.message("\nThere is an issue with the API key. Please use the 'Set API Key' button to update it, or click the 'My Account' button to retrieve your API key.\n")
return

register(
"python_fu_remove_bg",
"Remove Background from Current Image",
"Remove the background of the current image using Picsart API",
"Erik",
"Torosyan",
"2024",
"<Image>/Picsart/Remove Background",
"*",
[],
[],
remove_background_from_current_image
)

main()
69 changes: 69 additions & 0 deletions support.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-
from gimpfu import *
import gtk # GTK is used to create custom dialog windows
import webbrowser # Import webbrowser to open the URL in the default browser

def show_styled_message_dialog_with_link():
# Create a dialog window
dialog = gtk.Dialog(
"Support", # Window title
None,
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
(gtk.STOCK_OK, gtk.RESPONSE_OK)
)

# Create a label with instructions
label = gtk.Label()
label.set_markup("<span font='12'>To support, click the button below or visit:</span> \n\n"
"<a href='https://help.picsart.io/'>https://help.picsart.io/</a>")

# Set some padding around the label
label.set_padding(10, 10)

# Create a button that opens the URL
button = gtk.Button("Go to Console")

# Style the button with padding
button.set_size_request(100, 40) # Set the button size (width, height)

# Connect button click to open the URL
button.connect("clicked", lambda w: webbrowser.open("https://help.picsart.io/"))

# Create a VBox and add the label and button to it
vbox = gtk.VBox(spacing=10)
vbox.pack_start(label, True, True, 0)
vbox.pack_start(button, True, True, 0)

# Add the VBox to the dialog
dialog.vbox.pack_start(vbox)

# Show the components
label.show()
button.show()
vbox.show()

# Display the dialog window
dialog.run()
dialog.destroy() # Destroy the dialog after user interaction

def goToConsole(i, d):
# Show the styled message dialog with a clickable button and link
show_styled_message_dialog_with_link()

# Register the plugin in GIMP
register(
"python_fu_support_picsart",
"Support Picsart Io",
"Show API key balance.",
"Erik",
"Torosyan",
"2024",
"<Image>/Picsart/Support", # Menu path
"*", # Image type, leave empty
[],
[],
goToConsole # Function that handles the API key
)

# This runs the script
main()