-
Notifications
You must be signed in to change notification settings - Fork 31
/
upscaleuploadimages.py
83 lines (72 loc) · 2.81 KB
/
upscaleuploadimages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import requests
import pandas as pd
import base64
import os
# Set your API credentials
access_token = "your_printful_api_key"
# Find your shop ID by running this: curl -X GET https://api.printify.com/v1/shops.json --header "Authorization: Bearer YOUR_PRINTIFY_API_KEY"
shop_id = "your_shop_Id"
# Set the URL for the API endpoints
base_url = "https://api.printify.com/v1"
upload_url = f"{base_url}/uploads/images.json"
product_url = f"{base_url}/shops/{shop_id}/products.json"
# Load the CSV file
csv_path = "product_information.csv" # Update this to your CSV file path
image_df = pd.read_csv(csv_path)
# Set headers for requests
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json"
}
for idx, row in image_df.iterrows():
# Convert the image to Base64
with open(row['local_path'], "rb") as img_file:
img_b64 = base64.b64encode(img_file.read()).decode('utf-8')
# Upload the image to the Printify media library
data = {
"file_name": row['file_name'],
"contents": img_b64
}
response = requests.post(upload_url, headers=headers, json=data)
image_id = response.json()["id"]
# To change the print object, use this to find the variant id curl -X GET "https://api.printify.com/v1/catalog/blueprints/852/print_providers/73/variants.json" -H "Authorization: Bearer YOUR_PRINTIFY_KEY"
# Current settings are for cork art
# Create the product with the uploaded image
data = {
"title": row['title'],
"description": row['description'],
"tags": row['tags'].split(', '), # Assuming tags are comma-separated in the CSV
"blueprint_id": 480, # Replace with the actual blueprint ID
"print_provider_id": 70,
"variants": [
{
"id": 71689, # Replace with the actual variant ID
"price": 3999,
"is_enabled": True
}
],
"print_areas": [
{
"variant_ids": [71689], # Replace with the actual variant ID
"placeholders": [
{
"position": "front",
"images": [
{
"id": image_id,
"x": 0.5,
"y": 0.5,
"scale": 1.0,
"angle": 0
}
]
}
]
}
]
}
response = requests.post(product_url, headers=headers, json=data)
if response.status_code >= 200 and response.status_code < 300:
print(f"Product {idx+1} created successfully!")
else:
print(f"Failed to create product {idx+1}. Server responded with: {response.text}")