Skip to content
This repository has been archived by the owner on Jul 22, 2020. It is now read-only.

GIF File display #4

Closed
sloumdrone opened this issue Oct 27, 2018 · 6 comments
Closed

GIF File display #4

sloumdrone opened this issue Oct 27, 2018 · 6 comments

Comments

@sloumdrone
Copy link
Contributor

Hi there. It doesn't look like this is too actively maintained, but since I am working on a similar project I thought I might be able to help out with GIF image display:

I had been having trouble with GIFs as well. The byte string was coming back from the server solidly, but I couldn't get it to display or download properly. I then tried a PNG and found that using PIL or Pillow (depending on python version) worked. The added bonus is that it will display ALL of your images pretty cleanly (including GIFs). So you can treat type 'g' and type 'I' files the same (as well as type 'p' which I found to be PNG on floodgap).

from PIL import Image, ImageTk
from io import BytesIO


def build_image(bytes_str):
    # pass the response string (as a bytes string)

    stream = BytesIO(bytes_str)
    print(type(stream))
    pil_image = Image.open(stream)
    tkimage = ImageTk.PhotoImage(pil_image)
    return tkimage



def show_image(data):
    # 'site_display' is a tk Text widget

    current_image = self.build_image(data)
    site_display.config(state=tk.NORMAL)
    site_display.delete(1.0, tk.END)
    site_display.image_create(tk.END, image = self.current_image)
    site_display.config(state=tk.DISABLED)

io is in the standard library, but you'll need to pip or easy_install pillow (which gets imported as PIL in python3).

Hopefully that is useful to you.

I have been having trouble with search for my project. I cannot find documentation anywhere for how an item type '7' expects the query to be transmitted. I looked at your code, but couldnt quite figure out what form a querystring is to take (or if it is actually a querystring?). Any help you can provide would be much appreciated!

@felixp7
Copy link
Owner

felixp7 commented Oct 28, 2018

Hi there! Thanks for the suggestion, but one of my goals is to avoid major dependencies outside of the Python standard library. A single-file module I could ship with Gophersnake might work. A major library with native components, not so much.

As for searches, a query string is just plain text. You send the server a selector as usual, followed by a tab character, then your search. You can see it at work in load_as_directory. It should be explained in the RFC, but for some reason I don't have a copy. Hope this helps!

@felixp7 felixp7 closed this as completed Oct 28, 2018
@sloumdrone
Copy link
Contributor Author

Hello :) Thanks for getting back to me. I was trying to avoid that too. Sadly it just didnt seem possible in python to offer image display without PIL. I generally also try to avoid dependencies in that way. You could still distribute by freezing if need be...but that is a whole other pain.

I'm not certain, as I have not tried, but you may be able to get it to work without PIL for GIF images specifically by using the io module (a built in) like so:

from io import BytesIO

stream = BytesIO(bytes_string_from_request_response)
gif_image = tk.PhotoImage(stream)

# then put gif_image where you need it:
# text_widget.image_create(tk.END, image = gif_image)

Since tk has native support for GIFs, passing the file-like object ('stream' above) seems like it should work.

Just as an FYI: on Ubuntu specifically, but possibly also on other debian distros, you already have a dependency in that Ubuntu does not come with Tk support and you have to get it through apt:
sudo apt-get install python3-tk

I did see you doing that in your code, but it confused me. I had seen the RFC...I guess it just felt weird to send something like:

host = 'blahblah.org'
port = 70
resource = '/search-server/\tkittens'

Am I thinking about that wrong?

@felixp7
Copy link
Owner

felixp7 commented Oct 29, 2018

Thanks for the alternate solution, I'll be sure to try it whenever Gophersnake returns to active development. And it's not the same thing: Tkinter is preinstalled on the Mac, included with Python for Windows and there by default on plenty of Linux distributions. It's by and large considered a standard part of Python. Also, it can't be avoided if Gophersnake is to exist at all. Not all dependencies are equal!

Last but not least, dunno if it's weird or not, but that's really how Gopher does it. Seems to work, too.

@felixp7 felixp7 removed the wontfix label Oct 29, 2018
@felixp7 felixp7 reopened this Oct 29, 2018
@sloumdrone
Copy link
Contributor Author

Definitely agree that it isnt the same thing, just thought I'd mention in case you ever decided to build a .deb or the like.

Cool. Hopefully that approach to inline GIFs works. If you'd like (I get this isn't a particularly active project, so it may not matter), I could try it out and submit a PR. Let me know.

Cool! Thank you for the info, I'll try that on my client :)

@felixp7
Copy link
Owner

felixp7 commented Oct 30, 2018

A pull request would be much appreciated. Thank you!

@felixp7
Copy link
Owner

felixp7 commented Nov 5, 2018

Mostly resolved through this pull request.

@felixp7 felixp7 closed this as completed Nov 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants