Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add support for loading image from stdin #414

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

Closed
williamd1k0 opened this issue Aug 24, 2020 · 12 comments
Closed

Add support for loading image from stdin #414

williamd1k0 opened this issue Aug 24, 2020 · 12 comments
Assignees

Comments

@williamd1k0
Copy link

Hello, I have been using ksnip for a while, but I use it only as a quick editor.
I often generate images using some programs that have support for dumping the image directly into stdout and I would like to use ksnip to edit those images.

For now, I am using mktemp as a workaround, but ksnip does not allow saving it to a new file using the default filename template.

I tried to search for related docs and found nothing about this feature (I also tried to run ksnip - and ksnip -e -).

@DamirPorobic
Copy link
Member

DamirPorobic commented Aug 24, 2020 via email

@DamirPorobic
Copy link
Member

DamirPorobic commented Aug 24, 2020 via email

@williamd1k0
Copy link
Author

You would like to skip creating a temporary file and pass the image (not the path to the image) to ksnip and ksnip should open it up as a new image just as if you created a screenshot with knsip (the image should be shown ad not saved without a path)?

Yes, exactly!

My previous comment is also not correct, passing the path to image as single parameter works like the -f flag, not the -e flag. The -e flag is only relevant for taking screenshots currently.

Hm? Isn't the -f flag for --fullscreen? I tried using -e with any image path and it works the same way as passing the image path as a single parameter.

@DamirPorobic
Copy link
Member

DamirPorobic commented Aug 24, 2020 via email

@williamd1k0
Copy link
Author

I think the simplest setup is to use image magick commands:

# Show magick logo using feh image viewer
magick logo: png:- | feh -

# Show a red block using feh image viewer
magick -size 128x128 xc:rgb\(255,0,0\) png:- | feh -

@DamirPorobic
Copy link
Member

Thanks, we'll into it but as said, can't promise any time frame.

@williamd1k0
Copy link
Author

No problem, take your time.

@williamd1k0
Copy link
Author

Hi, I had some free time today, so I tried to deal with it a little bit (although I'm not good with C++ and Qt).
I managed to make it work at least on Linux.

I implemented it using this info as reference: https://stackoverflow.com/questions/25711302/how-to-read-stdin-to-end-in-qt/25715691#25715691

I cannot open a PR because I cannot confirm that the code is 100% correct, but I will paste the diff/patch here, so that I can help you in some way:

diff --git a/src/bootstrapper/StandAloneBootstrapper.cpp b/src/bootstrapper/StandAloneBootstrapper.cpp
index 32c9667..8bfe0e9 100644
--- a/src/bootstrapper/StandAloneBootstrapper.cpp
+++ b/src/bootstrapper/StandAloneBootstrapper.cpp
@@ -17,6 +17,7 @@
  * Boston, MA 02110-1301, USA.
  */
 
+#include <iostream>
 #include "StandAloneBootstrapper.h"
 
 StandAloneBootstrapper::StandAloneBootstrapper() :
@@ -122,7 +123,20 @@ CaptureModes StandAloneBootstrapper::getCaptureMode() const
 int StandAloneBootstrapper::startKsnipAndEditImage(const QApplication &app)
 {
 	auto pathToImage = getImagePath();
-	QPixmap pixmap(pathToImage);
+	QPixmap pixmap;
+	if (pathToImage == "-") {
+		qWarning("Reading image from stdin.");
+		QByteArray stdinImage;
+		while(!std::cin.eof()) {
+			char arr[1024];
+			std::cin.read(arr, sizeof(arr));
+			int s = std::cin.gcount();
+			stdinImage.append(arr, s);
+		}
+		pixmap.loadFromData(stdinImage);
+	} else {
+		pixmap = QPixmap(pathToImage);
+	}
 
 	if (pixmap.isNull()) {
 		qWarning("Unable to open image file %s.", qPrintable(pathToImage));

Kazam_screencast_00023

@DamirPorobic
Copy link
Member

DamirPorobic commented Aug 31, 2020

Thanks for providing this, it will definitely speed up the implementation. I'll plan it for 1.8.0.
I'll take care of adding it to the application.

@DamirPorobic DamirPorobic self-assigned this Oct 11, 2020
@DamirPorobic DamirPorobic changed the title Add support for stdin when using -e/--edit Add support for loading image from stdin Oct 11, 2020
@DamirPorobic
Copy link
Member

This has been implemented. Thanks for providing the logic for this feature!

@michael-betz
Copy link

This works for me, but when I run ksnip as a single instance, it fails the second time.
For example:

# Opens ksnip, works as expected
 convert logo: - | ksnip -e - &
# Fails with  `Warning: Unable to open image file -.`
 convert logo: - | ksnip -e - &

When I disable running as a single instance it works as expected, however I'd loose the nice tabbed interface.

@DamirPorobic
Copy link
Member

@yetifrisstlama this issue should be fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants