Skip to content

Commit 50bc286

Browse files
committed
support for image from url
1 parent 6f773fa commit 50bc286

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

run.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@ func getRunCommand() *cobra.Command {
1212
Short: "Upscayl single image using command line options",
1313
Run: func(cmd *cobra.Command, args []string) {
1414
image, _ := cmd.Flags().GetString("input")
15-
outputPath, err := upscayl.Upscayl(upscayl.Input{ImagePath: image})
15+
url, _ := cmd.Flags().GetString("url")
16+
input := upscayl.Input{
17+
ImagePath: image,
18+
ImageURL: url,
19+
}
20+
outputPath, err := upscayl.Upscayl(input)
1621
if err != nil {
1722
log.Fatal("error while upscayling", err.Error())
1823
}
1924
log.Println("output image at", outputPath)
2025
},
2126
}
2227
cmd.Flags().StringP("input", "i", "", "Input image path (jpg/png/webp) or directory")
28+
cmd.Flags().StringP("url", "u", "", "Input image url (jpg/png/webp)")
2329
cmd.Flags().StringP("output", "o", "", "Output image path (jpg/png/webp) or directory")
2430
cmd.Flags().IntP("model-scale", "z", 4, "Scale according to the model (can be 2, 3, 4)")
2531
cmd.Flags().IntP("output-scale", "s", 4, "Custom output scale (can be 2, 3, 4)")
@@ -34,6 +40,5 @@ func getRunCommand() *cobra.Command {
3440
cmd.Flags().BoolP("tta", "x", false, "Enable TTA mode")
3541
cmd.Flags().StringP("format", "f", "ext/png", "Output image format (jpg/png/webp)")
3642
cmd.Flags().BoolP("verbose", "v", false, "Verbose output")
37-
_ = cmd.MarkFlagRequired("input")
3843
return cmd
3944
}

upscayl/upscayl.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package upscayl
22

33
import (
4+
"errors"
45
"fmt"
56
"io"
67
"log"
78
"net/http"
9+
"net/url"
810
"os"
911
"os/exec"
1012
"os/user"
13+
"path"
1114
"path/filepath"
1215
"runtime"
1316
"strings"
@@ -106,6 +109,7 @@ func init() {
106109

107110
type Input struct {
108111
ImagePath string `json:"imagePath"`
112+
ImageURL string `json:"imageUrl"`
109113
OutputPath string `json:"outputPath"`
110114
ModelPath string `json:"modelPath"`
111115
Model string `json:"model"`
@@ -121,6 +125,33 @@ type Input struct {
121125
}
122126

123127
func Upscayl(input Input) (string, error) {
128+
if input.ImagePath == "" && input.ImageURL == "" {
129+
return "", errors.New("input path or url not set")
130+
}
131+
if input.ImagePath != "" && input.ImageURL != "" {
132+
return "", errors.New("only 1 of the input allowed")
133+
}
134+
if input.ImageURL != "" {
135+
parsedURL, err := url.Parse(input.ImageURL)
136+
if err != nil {
137+
return "", err
138+
}
139+
ext := path.Ext(parsedURL.Path)
140+
if ext != ".png" && ext != ".jpg" && ext != ".jpeg" && ext != ".webp" {
141+
return "", errors.New("invalid extension in url expected .png, .jpg, .jpeg, .webp")
142+
}
143+
tmpFilePath := filepath.Join(rootDir, "input"+ext)
144+
err = download(input.ImageURL, tmpFilePath)
145+
if err != nil {
146+
return "", err
147+
}
148+
defer os.Remove(tmpFilePath)
149+
input.ImagePath = tmpFilePath
150+
}
151+
return upscaylImage(input)
152+
}
153+
154+
func upscaylImage(input Input) (string, error) {
124155
args := make([]string, 0)
125156
args = append(args, fmt.Sprintf("-i %s", input.ImagePath))
126157
if input.OutputPath == "" {

0 commit comments

Comments
 (0)