python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.23k stars 2.23k forks source link

Xdg open not available #8444

Closed Botgamerns10s closed 3 weeks ago

Botgamerns10s commented 3 weeks ago

What did you do?

Trying to run a bot on render

What did you expect to happen?

It should open an image and then merge them into one and the final one to be posted on twitter

What actually happened?

Getting xdg error and the code stops

What are your OS, Python and Pillow versions?

Everything to latest version on Render

Please paste here the output of running:

python3 -m PIL.report
or
python3 -m PIL --report

Or the output of the following Python code:

from PIL import report
# or
from PIL import features
features.pilinfo(supported_formats=False)
code goes here

Orignal code https://github.com/FortniteFevers/Fortnite-Shop-Bot/blob/main/bot.py Error on render IMG_20241006_114237

radarhere commented 3 weeks ago

The code would be trying to open up an image, so that you can see it on your screen. It is running xdg-open something.png on the command line. xdg-open is installed, but it isn't connected to any application.

What application are you expecting to open up with the image?

Botgamerns10s commented 3 weeks ago

Ahh I am not sure but I know this that the main code (which I attached) receives data from an api and saves it into images they it opens another python file which merges them into one in the whole process the image is not displayed to me instead after all the images are merged it is uploaded as a tweet on twitter

hugovk commented 3 weeks ago

It sounds like this is code for a bot running on a server, so there's no point in showing the image to the user.

I suggest removing the img.show()

Botgamerns10s commented 3 weeks ago

Let me try that but I won't be able to check if it's working or not because the bot auto updates at 8 pm et when the shop resets so will update you after that . Also is their anything I can do about this -- UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images

aclark4life commented 3 weeks ago

Palette images with Transparency expressed in bytes should be converted to RGBA images

Convert palette images with transparency expressed in bytes to RGBA images or disable warnings (not sure if we support that?) or redirect stderr to stdout and stdout to /dev/null.

radarhere commented 3 weeks ago

Let me try that

I don't understand where show() is being called on a PNG image - the only time https://github.com/FortniteFevers/Fortnite-Shop-Bot/blob/main/bot.py calls show() it is on a JPG. I thought that you might be saying that tweepy calls it, but I don't see any evidence of Pillow use in their codebase.

Are you just running bot.py, or is there other code?

If you can't find a show() to remove that makes any difference, a quick and dirty way to stop show() from doing anything would be to remove all of the registered viewers at the start of your code.

from PIL import ImageShow
ImageShow._viewers = []

I think if you would like further assistance with this, and with the UserWarning, it would be help if you could provide tracebacks, so that we can see how your code has reached that point.

You can easily get a traceback for the warning by running python -W error bot.py instead of python bot.py. That will cause all warnings to be treated as errors instead, showing the code from proceeding further, and showing how the code reached that point.

or disable warnings (not sure if we support that?)

No, there's no functionality in Image.py for that. The warning is straightforward.

Botgamerns10s commented 3 weeks ago

The show command is on line 259 in main code And here is the second python file which is automatically imported to merge the images https://github.com/FortniteFevers/Fortnite-Shop-Bot/blob/main/merger.py

Botgamerns10s commented 3 weeks ago

IMG_20241006_233551 If this provides more information

nulano commented 3 weeks ago

I don't understand where show() is being called on a PNG image

When calling img.show() on an image, Pillow converts it to PNG for displaying: https://github.com/python-pillow/Pillow/blob/96f1a6e8b141934ffbda2df47e30fa4c8334c64e/src/PIL/ImageShow.py#L195-L224

hugovk commented 3 weeks ago

If this provides more information

Thanks, it does. This is unrelated to Pillow.

You're running with -W error which turns warnings into error. Python 3.11 has deprecation warnings about imghdr. You're using Tweepy which uses imghdr. See https://github.com/tweepy/tweepy/issues/2177. You should take this up with Tweepy, don't run with -W error, or add an exception for this warning.

radarhere commented 3 weeks ago

When calling img.show() on an image, Pillow converts it to PNG for displaying:

Ah, of course! Thanks.

Botgamerns10s commented 3 weeks ago

Thanks guys but removing show command fixed everything just the. UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA imagesis left but I don't care about it as it doesn't interfere the output of the code

Botgamerns10s commented 3 weeks ago

Btw guys you can see in the image that some of the items/merged images have a black background but Instead they should have a specific coloured according to their type can you tell me from the mai code where is the colour defined which is making few images with black background. I am new to python and all this stuff and couldn't find a solution on Google etc so thought to ask you guys 😅😅 IMG_20241008_000509

radarhere commented 3 weeks ago

https://github.com/FortniteFevers/Fortnite-Shop-Bot/blob/951d1d7f104aa03c25ffa0961f55dbdf9c9084d1/bot.py#L187-L188 pastes overlay.png, which is the lower translucent section, onto img. So the background colour would happen before that.

But before that is just loading images from URLs and pasting them - https://github.com/FortniteFevers/Fortnite-Shop-Bot/blob/951d1d7f104aa03c25ffa0961f55dbdf9c9084d1/bot.py#L141-L183

I suggest printing some of those URLs and loading them individually in your browser.

It is possible that the 'black background' images don't actually have a black background, and are just transparent images being pasted onto the already black img. In that case, you can set the initial colour of img at https://github.com/FortniteFevers/Fortnite-Shop-Bot/blob/951d1d7f104aa03c25ffa0961f55dbdf9c9084d1/bot.py#L145 by changing it to img=Image.new("RGB",(512,512), "#f00") or whatever you like.

If the images do actually have a black background in the URLs though, then that is more interesting. I could suggest using Pillow to filter out all black pixels, but the foreground of the images also have black in some cases. You could try using floodfill() at the edge of the images, and seeing if that doesn't interfere with the foreground.

Botgamerns10s commented 3 weeks ago

I actually checked the URLs generated by the api which the code is receiving manually but api is sending 2 links for each item, 1 with no background and another with background and the code is picking the one with no background to generate images for (but only for a few items) idk how to make it correct 😭

radarhere commented 3 weeks ago

Are you sure this shouldn't be a question for https://github.com/FortniteFevers/Fortnite-Shop-Bot?

I found that changing https://github.com/FortniteFevers/Fortnite-Shop-Bot/blob/951d1d7f104aa03c25ffa0961f55dbdf9c9084d1/bot.py#L145-L146 to

img=Image.new("RGB",(512,512),'#0f0')
img.paste(background, (0, 0), background.convert('RGBA'))

making use of the paste() mask argument, worked to give a green background to some images.

For one of the images that it didn't give a green background to, I found that changing https://github.com/FortniteFevers/Fortnite-Shop-Bot/blob/951d1d7f104aa03c25ffa0961f55dbdf9c9084d1/bot.py#L96-L99 to always use 'OfferImage' fixed it.

url = i['newDisplayAsset']['materialInstances'][0]['images']['OfferImage']

That makes sense, that avoiding the 'Background' image stops the image from having a background.

Botgamerns10s commented 2 weeks ago

Okay so sorry for the late reply but the things marked by me are some values which the api gives to make images IMG_20241010_220856 Here the lines with [renderimage] variables are the ones which are getting images with no/black background and also the api sends another variable 'rarities' which actually decides the color of the background but when I edit the code and add it to the lines with [renderimages] the code gives a string value error . The lines with [materialinstance] and [offerimage] are making images with correct background. I tried adding [materialinstance] and [offerimage] variables to the other lines also but it messes up and the generated images are repeated. Though I found another code which is totally different but I found this which is preventing it from making images with no background here is a pic of the other code IMG_20241010_220909

radarhere commented 2 weeks ago

Again, you're not asking questions about how to use Pillow. You're asking questions about how to modify the code at https://github.com/FortniteFevers/Fortnite-Shop-Bot. Users at that repository would be better able to help you than we are - before this conversation, I didn't even know Fortnite had an API.

when I edit the code and add it to the lines with [renderimages] the code gives a string value error

I tried adding [materialinstance] and [offerimage] variables to the other lines also but it messes up and the generated images are repeated.

This is hard to debug without knowing exactly what code you wrote. It would also be easier to have the complete script that you are trying to run each time as a text file, rather than portions of it as screenshots.

If you're still just trying to set the background colour for each square, then I don't see what the problem is with the code from my last comment. With the script I'm attaching here, I'm able to get a green background for all of the squares.

bot.py.zip

Botgamerns10s commented 2 weeks ago

Yeah I went out of topic sorry won't ask again actually the person who wrote script wasn't interested in helping and I am new to python all this so thought to ask you guys sorry for the trouble