Discussion:
image conversion in memory
diliup gabadamudalige
2014-08-08 09:16:19 UTC
Permalink
Hi all!

Is there a way to convert a pygame surface to a png or jpeg IN MEMORY
instead of saving to disk and loading again?
something like
get_screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH) # grab the screen

my_image = get_screen.convert(jpeg) # now convert the image to jpeg

and now my_image is a jpeg image of get_screen

I searched the net but couldn't find any other way other than save to disk
as jpeg and reload.

Any positive OR negative help is very much appreciated.

Thanks in advance
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
Christopher Night
2014-08-08 14:24:14 UTC
Permalink
It depends on what precisely you mean when you say that a python variable
is a jpeg image. Do you mean that it's a string containing the contents of
a jpeg image file? So if you wrote it to disk you'd get a jpeg image file?

If that's what you mean, you can do it with PIL and StringIO. (There might
be an easier way.)

import pygame
from PIL import Image
from cStringIO import StringIO

# create an example pygame image
img = pygame.Surface((100, 100))
pygame.draw.circle(img, (255, 0, 0), (50, 50), 40)
# convert to PIL format
imgstr = pygame.image.tostring(img, "RGB", False)
pimg = Image.fromstring("RGB", img.get_size(), imgstr)
# save to string
s = StringIO()
pimg.save(s, "JPEG")
r = s.getvalue()
s.close()

# r is a string with the contents of a jpeg file. to confirm:
open("img.jpg", "wb").write(r)

However, this doesn't seem like a very useful thing to have (and if I
needed it so badly I would just write it to disk and read it back) so I may
be misunderstanding you.
Post by diliup gabadamudalige
Hi all!
Is there a way to convert a pygame surface to a png or jpeg IN MEMORY
instead of saving to disk and loading again?
something like
get_screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH) # grab the screen
my_image = get_screen.convert(jpeg) # now convert the image to jpeg
and now my_image is a jpeg image of get_screen
I searched the net but couldn't find any other way other than save to disk
as jpeg and reload.
Any positive OR negative help is very much appreciated.
Thanks in advance
Diliup Gabadamudalige
http://www.diliupg.com
http://soft.diliupg.com/
**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
diliup gabadamudalige
2014-08-10 13:31:46 UTC
Permalink
Hi Christopher!

Thanks for the response. Below is my code

pygame.image.save(get_screen, image_filename) # save the file
as jpeg
## reopen image and convert to text file
with open(image_filename, "rb") as imageFile:
imagestr = base64.b64encode(imageFile.read())
## create the text file name to save to disk
text_filename = newpath + "s" + str(va.page) + ".dat"
## save imagestr(image as a textfle) to disk
with open(text_filename, "wb") as f:
f.write(imagestr)

myscreen = pygame.image.load(image_filename).convert()

I capture the screen image
Is there any way that it can be converted directly to a text file like
above without writing it to disk first?
When you capture a screen it is a pygame surface. It is NOT a jpeg or a
png. The only way to cojnvert it to a jpeg or png is to write it to disk
and then reload it and then convert it to a text fle using base64.

What I would like to do is to capture the screen, covert to jpeg and then
convert to text file WITHOUT writing to disk first.

Any ideas?
Thanks for your support.
Post by Christopher Night
It depends on what precisely you mean when you say that a python variable
is a jpeg image. Do you mean that it's a string containing the contents of
a jpeg image file? So if you wrote it to disk you'd get a jpeg image file?
If that's what you mean, you can do it with PIL and StringIO. (There might
be an easier way.)
import pygame
from PIL import Image
from cStringIO import StringIO
# create an example pygame image
img = pygame.Surface((100, 100))
pygame.draw.circle(img, (255, 0, 0), (50, 50), 40)
# convert to PIL format
imgstr = pygame.image.tostring(img, "RGB", False)
pimg = Image.fromstring("RGB", img.get_size(), imgstr)
# save to string
s = StringIO()
pimg.save(s, "JPEG")
r = s.getvalue()
s.close()
open("img.jpg", "wb").write(r)
However, this doesn't seem like a very useful thing to have (and if I
needed it so badly I would just write it to disk and read it back) so I may
be misunderstanding you.
Post by diliup gabadamudalige
Hi all!
Is there a way to convert a pygame surface to a png or jpeg IN MEMORY
instead of saving to disk and loading again?
something like
get_screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH) # grab the screen
my_image = get_screen.convert(jpeg) # now convert the image to jpeg
and now my_image is a jpeg image of get_screen
I searched the net but couldn't find any other way other than save to
disk as jpeg and reload.
Any positive OR negative help is very much appreciated.
Thanks in advance
Diliup Gabadamudalige
http://www.diliupg.com
http://soft.diliupg.com/
**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you
are not the intended recipient or have received it in error, please delete
it and all copies from your system and notify the sender immediately by
return e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
--
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
diliup gabadamudalige
2014-08-10 13:32:29 UTC
Permalink
Sorry I forgot to mention that I need to do this WITHOUT loading ajny more
modules or libraries.
Is it possible?
Post by diliup gabadamudalige
Hi Christopher!
Thanks for the response. Below is my code
pygame.image.save(get_screen, image_filename) # save the file
as jpeg
## reopen image and convert to text file
imagestr = base64.b64encode(imageFile.read())
## create the text file name to save to disk
text_filename = newpath + "s" + str(va.page) + ".dat"
## save imagestr(image as a textfle) to disk
f.write(imagestr)
myscreen = pygame.image.load(image_filename).convert()
I capture the screen image
Is there any way that it can be converted directly to a text file like
above without writing it to disk first?
When you capture a screen it is a pygame surface. It is NOT a jpeg or a
png. The only way to cojnvert it to a jpeg or png is to write it to disk
and then reload it and then convert it to a text fle using base64.
What I would like to do is to capture the screen, covert to jpeg and then
convert to text file WITHOUT writing to disk first.
Any ideas?
Thanks for your support.
Post by Christopher Night
It depends on what precisely you mean when you say that a python variable
is a jpeg image. Do you mean that it's a string containing the contents of
a jpeg image file? So if you wrote it to disk you'd get a jpeg image file?
If that's what you mean, you can do it with PIL and StringIO. (There
might be an easier way.)
import pygame
from PIL import Image
from cStringIO import StringIO
# create an example pygame image
img = pygame.Surface((100, 100))
pygame.draw.circle(img, (255, 0, 0), (50, 50), 40)
# convert to PIL format
imgstr = pygame.image.tostring(img, "RGB", False)
pimg = Image.fromstring("RGB", img.get_size(), imgstr)
# save to string
s = StringIO()
pimg.save(s, "JPEG")
r = s.getvalue()
s.close()
open("img.jpg", "wb").write(r)
However, this doesn't seem like a very useful thing to have (and if I
needed it so badly I would just write it to disk and read it back) so I may
be misunderstanding you.
Post by diliup gabadamudalige
Hi all!
Is there a way to convert a pygame surface to a png or jpeg IN MEMORY
instead of saving to disk and loading again?
something like
get_screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH) # grab the screen
my_image = get_screen.convert(jpeg) # now convert the image to jpeg
and now my_image is a jpeg image of get_screen
I searched the net but couldn't find any other way other than save to
disk as jpeg and reload.
Any positive OR negative help is very much appreciated.
Thanks in advance
Diliup Gabadamudalige
http://www.diliupg.com
http://soft.diliupg.com/
**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you
are not the intended recipient or have received it in error, please delete
it and all copies from your system and notify the sender immediately by
return e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
--
Diliup Gabadamudalige
http://www.diliupg.com
http://soft.diliupg.com/
**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
--
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
Radomir Dopieralski
2014-08-10 18:51:38 UTC
Permalink
Post by diliup gabadamudalige
Hi all!
Is there a way to convert a pygame surface to a png or jpeg IN MEMORY
instead of saving to disk and loading again?
something like
get_screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH) # grab the screen
my_image = get_screen.convert(jpeg) # now convert the image to jpeg
and now my_image is a jpeg image of get_screen
I searched the net but couldn't find any other way other than save to
disk as jpeg and reload.
Any positive OR negative help is very much appreciated.
Sure, just use StringIO to use memory instead of a file.

import pygame
import StringIO

s = pygame.image.load("yourimage.png")
f = StringIO.StringIO()
f.name = 'yourimage.jpg'
pygame.image.save(s, f)

yourimage = f.getvalue()
--
Radomir Dopieralski
diliup gabadamudalige
2014-08-11 09:40:48 UTC
Permalink
get_screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH) # grab the screen

how can you concert get_screen to a jpeg or a png WITHOUT saving to disk?
In Pygame..

Thanks
Post by Radomir Dopieralski
Post by diliup gabadamudalige
Hi all!
Is there a way to convert a pygame surface to a png or jpeg IN MEMORY
instead of saving to disk and loading again?
something like
get_screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH) # grab the screen
my_image = get_screen.convert(jpeg) # now convert the image to jpeg
and now my_image is a jpeg image of get_screen
I searched the net but couldn't find any other way other than save to
disk as jpeg and reload.
Any positive OR negative help is very much appreciated.
Sure, just use StringIO to use memory instead of a file.
import pygame
import StringIO
s = pygame.image.load("yourimage.png")
f = StringIO.StringIO()
f.name = 'yourimage.jpg'
pygame.image.save(s, f)
yourimage = f.getvalue()
--
Radomir Dopieralski
--
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
Radomir Dopieralski
2014-08-11 10:30:24 UTC
Permalink
Post by diliup gabadamudalige
get_screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH) # grab the screen
how can you concert get_screen to a jpeg or a png WITHOUT saving to
disk? In Pygame..
I just told you. Use the example I gave you, it works.
--
The Sheep
diliup gabadamudalige
2014-08-11 11:05:59 UTC
Permalink
thanks I'll try it tonight
Post by Radomir Dopieralski
Post by diliup gabadamudalige
get_screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH) # grab the screen
how can you concert get_screen to a jpeg or a png WITHOUT saving to
disk? In Pygame..
I just told you. Use the example I gave you, it works.
--
The Sheep
--
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
diliup gabadamudalige
2014-08-11 19:34:31 UTC
Permalink
Dear Radomir,

s = pygame.image.load("88keykbd.png")
f = StringIO.StringIO()
f.name = '88keykbd.jpg'
pygame.image.save(s, f)
pygame.image.load("88keykbd.jpg")


yourimage = f.getvalue()

why does the line pygame.image.load("88keykbd.jpg") throw an error? It
throws pygame.error: Couldn't open 88keykbd.jpg
I cant find the88keykbd.jpeg in the path
Post by Radomir Dopieralski
Post by diliup gabadamudalige
Hi all!
Is there a way to convert a pygame surface to a png or jpeg IN MEMORY
instead of saving to disk and loading again?
something like
get_screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH) # grab the screen
my_image = get_screen.convert(jpeg) # now convert the image to jpeg
and now my_image is a jpeg image of get_screen
I searched the net but couldn't find any other way other than save to
disk as jpeg and reload.
Any positive OR negative help is very much appreciated.
Sure, just use StringIO to use memory instead of a file.
import pygame
import StringIO
s = pygame.image.load("yourimage.png")
f = StringIO.StringIO()
f.name = 'yourimage.jpg'
pygame.image.save(s, f)
yourimage = f.getvalue()
--
Radomir Dopieralski
--
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
Radomir Dopieralski
2014-08-11 19:38:31 UTC
Permalink
Post by diliup gabadamudalige
Dear Radomir,
s = pygame.image.load("88keykbd.png")
f = StringIO.StringIO()
f.name <http://f.name> = '88keykbd.jpg'
pygame.image.save(s, f)
pygame.image.load("88keykbd.jpg")
yourimage = f.getvalue()
why does the line pygame.image.load("88keykbd.jpg") throw an error? It
throws pygame.error: Couldn't open 88keykbd.jpg
I cant find the88keykbd.jpeg in the path
Of course you can't -- no such file exists. It's only in memory, in the
f variable. That's the whole point. You can get to the *contents* of
that variable with f.getvalue().
--
The Sheep
diliup gabadamudalige
2014-08-11 19:44:07 UTC
Permalink
so if i need to save that to memory i do with open('writetomyfile.dat',
'w') as f.

Thank you very much for that explanation.

the "image save" part confused me.
How would you convert the file back to a jpg or png image?
Please pardon me for asking all these questions.

Thank you very much in advance
Post by Radomir Dopieralski
Post by diliup gabadamudalige
Dear Radomir,
s = pygame.image.load("88keykbd.png")
f = StringIO.StringIO()
f.name <http://f.name> = '88keykbd.jpg'
pygame.image.save(s, f)
pygame.image.load("88keykbd.jpg")
yourimage = f.getvalue()
why does the line pygame.image.load("88keykbd.jpg") throw an error? It
throws pygame.error: Couldn't open 88keykbd.jpg
I cant find the88keykbd.jpeg in the path
Of course you can't -- no such file exists. It's only in memory, in the
f variable. That's the whole point. You can get to the *contents* of
that variable with f.getvalue().
--
The Sheep
--
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
diliup gabadamudalige
2014-08-11 19:53:37 UTC
Permalink
type(f) gives <type 'instance'>
yourimage = f.getvalue()
with open('myscrambledimage.dat', 'w') as f:
f.write(yourimage)
so i coukld write this file to disk

now how can i convert this back to an image?
Thanks in advance
Post by diliup gabadamudalige
so if i need to save that to memory i do with open('writetomyfile.dat',
'w') as f.
Thank you very much for that explanation.
the "image save" part confused me.
How would you convert the file back to a jpg or png image?
Please pardon me for asking all these questions.
Thank you very much in advance
Post by Radomir Dopieralski
Post by diliup gabadamudalige
Dear Radomir,
s = pygame.image.load("88keykbd.png")
f = StringIO.StringIO()
f.name <http://f.name> = '88keykbd.jpg'
pygame.image.save(s, f)
pygame.image.load("88keykbd.jpg")
yourimage = f.getvalue()
why does the line pygame.image.load("88keykbd.jpg") throw an error? It
throws pygame.error: Couldn't open 88keykbd.jpg
I cant find the88keykbd.jpeg in the path
Of course you can't -- no such file exists. It's only in memory, in the
f variable. That's the whole point. You can get to the *contents* of
that variable with f.getvalue().
--
The Sheep
--
Diliup Gabadamudalige
http://www.diliupg.com
http://soft.diliupg.com/
**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
--
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
Radomir Dopieralski
2014-08-12 05:42:08 UTC
Permalink
Post by diliup gabadamudalige
type(f) gives <type 'instance'>
yourimage = f.getvalue()
f.write(yourimage)
so i coukld write this file to disk
now how can i convert this back to an image?
Thanks in advance
What are you actually tryig to do?
I'm sure there is a simpler way.
--
The Sheep
diliup gabadamudalige
2014-08-12 16:23:23 UTC
Permalink
Hi Radomir,

Thanks for the response.
ok. This is what i want to do
i want to try to convert images into a string format and store them on
disk. Better if they are unprintable characters.
then when i want to see them i will run them thrugh a decryption python
script and viola they will be displayed in pygame
when i close the program again i will have only the encoded 'imageds' on
the hd.
I did this using

get_screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH)
with open(image_filename, "rb") as imageFile:
imagestr = base64.b64encode(imageFile.read())
## create the text file name to save to disk
text_filename = newpath + "s" + str(va.page) + ".dat"
## save imagestr(image as a textfle) to disk
with open(text_filename, "wb") as f:
f.write(imagestr)

I wanted a better way and hence the question.

stringio or cstringio is fine. i can save the string to disk but how can i
convert it back to an image file?

Thank you for your help.
Post by Radomir Dopieralski
Post by diliup gabadamudalige
type(f) gives <type 'instance'>
yourimage = f.getvalue()
f.write(yourimage)
so i coukld write this file to disk
now how can i convert this back to an image?
Thanks in advance
What are you actually tryig to do?
I'm sure there is a simpler way.
--
The Sheep
--
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
diliup gabadamudalige
2014-08-13 14:00:52 UTC
Permalink
can any good soul out there tell me why these two files are not the same
size?

import pygame
import StringIO

myimage = pygame.image.load("88keykbd.png")
buff = StringIO.StringIO()
buff.name = '88keykbd.png'
pygame.image.save(myimage, buff)

putimage = buff.getvalue()
print "buff:", type(buff), "myimage:", type(myimage), "getimage:",
type(putimage)

print len(getimage)
with open('myscrambledimage.dat', 'w') as newfile:
newfile.write(putimage)

with open('myscrambledimage.dat', 'r') as newfile:
getimage= newfile.read()
print len(getimage)

len(putimage) and len(getimage) are hugely different in size and I can't
see why as it is putimage that is written to file and then read back as
getimage.

Clarification on this would be immensly welcome.

Thank you in advance
Post by diliup gabadamudalige
Hi Radomir,
Thanks for the response.
ok. This is what i want to do
i want to try to convert images into a string format and store them on
disk. Better if they are unprintable characters.
then when i want to see them i will run them thrugh a decryption python
script and viola they will be displayed in pygame
when i close the program again i will have only the encoded 'imageds' on
the hd.
I did this using
get_screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH)
imagestr = base64.b64encode(imageFile.read())
## create the text file name to save to disk
text_filename = newpath + "s" + str(va.page) + ".dat"
## save imagestr(image as a textfle) to disk
f.write(imagestr)
I wanted a better way and hence the question.
stringio or cstringio is fine. i can save the string to disk but how can i
convert it back to an image file?
Thank you for your help.
Post by Radomir Dopieralski
Post by diliup gabadamudalige
type(f) gives <type 'instance'>
yourimage = f.getvalue()
f.write(yourimage)
so i coukld write this file to disk
now how can i convert this back to an image?
Thanks in advance
What are you actually tryig to do?
I'm sure there is a simpler way.
--
The Sheep
--
Diliup Gabadamudalige
http://www.diliupg.com
http://soft.diliupg.com/
**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
--
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
diliup gabadamudalige
2014-08-13 14:02:32 UTC
Permalink
Sorry there was a small error in the previous message. My apologies.

import pygame
import StringIO

myimage = pygame.image.load("88keykbd.png")
buff = StringIO.StringIO()
buff.name = '88keykbd.png'
pygame.image.save(myimage, buff)

putimage = buff.getvalue()
print "buff:", type(buff), "myimage:", type(myimage), "getimage:",
type(putimage)

print len(putimage)
with open('myscrambledimage.dat', 'w') as newfile:
newfile.write(putimage)

with open('myscrambledimage.dat', 'r') as newfile:
getimage= newfile.read()
print len(getimage)
Post by diliup gabadamudalige
can any good soul out there tell me why these two files are not the same
size?
import pygame
import StringIO
myimage = pygame.image.load("88keykbd.png")
buff = StringIO.StringIO()
buff.name = '88keykbd.png'
pygame.image.save(myimage, buff)
putimage = buff.getvalue()
print "buff:", type(buff), "myimage:", type(myimage), "getimage:",
type(putimage)
print len(getimage)
newfile.write(putimage)
getimage= newfile.read()
print len(getimage)
len(putimage) and len(getimage) are hugely different in size and I can't
see why as it is putimage that is written to file and then read back as
getimage.
Clarification on this would be immensly welcome.
Thank you in advance
Post by diliup gabadamudalige
Hi Radomir,
Thanks for the response.
ok. This is what i want to do
i want to try to convert images into a string format and store them on
disk. Better if they are unprintable characters.
then when i want to see them i will run them thrugh a decryption python
script and viola they will be displayed in pygame
when i close the program again i will have only the encoded 'imageds' on
the hd.
I did this using
get_screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH)
imagestr = base64.b64encode(imageFile.read())
## create the text file name to save to disk
text_filename = newpath + "s" + str(va.page) + ".dat"
## save imagestr(image as a textfle) to disk
f.write(imagestr)
I wanted a better way and hence the question.
stringio or cstringio is fine. i can save the string to disk but how can
i convert it back to an image file?
Thank you for your help.
On Tue, Aug 12, 2014 at 11:12 AM, Radomir Dopieralski <
Post by Radomir Dopieralski
Post by diliup gabadamudalige
type(f) gives <type 'instance'>
yourimage = f.getvalue()
f.write(yourimage)
so i coukld write this file to disk
now how can i convert this back to an image?
Thanks in advance
What are you actually tryig to do?
I'm sure there is a simpler way.
--
The Sheep
--
Diliup Gabadamudalige
http://www.diliupg.com
http://soft.diliupg.com/
**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you
are not the intended recipient or have received it in error, please delete
it and all copies from your system and notify the sender immediately by
return e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
--
Diliup Gabadamudalige
http://www.diliupg.com
http://soft.diliupg.com/
**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
--
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
Noel Garwick
2014-08-13 14:16:18 UTC
Permalink
since myimage is using pygame.image.load, it might be indexed? On the
other hand, it's also possible that pygame.image.save is actually
performing some compression when it saves to a png format? I'm not sure
offhand, but something to check out.
Post by diliup gabadamudalige
Sorry there was a small error in the previous message. My apologies.
import pygame
import StringIO
myimage = pygame.image.load("88keykbd.png")
buff = StringIO.StringIO()
buff.name = '88keykbd.png'
pygame.image.save(myimage, buff)
putimage = buff.getvalue()
print "buff:", type(buff), "myimage:", type(myimage), "getimage:",
type(putimage)
print len(putimage)
newfile.write(putimage)
getimage= newfile.read()
print len(getimage)
Post by diliup gabadamudalige
can any good soul out there tell me why these two files are not the same
size?
import pygame
import StringIO
myimage = pygame.image.load("88keykbd.png")
buff = StringIO.StringIO()
buff.name = '88keykbd.png'
pygame.image.save(myimage, buff)
putimage = buff.getvalue()
print "buff:", type(buff), "myimage:", type(myimage), "getimage:",
type(putimage)
print len(getimage)
newfile.write(putimage)
getimage= newfile.read()
print len(getimage)
len(putimage) and len(getimage) are hugely different in size and I can't
see why as it is putimage that is written to file and then read back as
getimage.
Clarification on this would be immensly welcome.
Thank you in advance
Post by diliup gabadamudalige
Hi Radomir,
Thanks for the response.
ok. This is what i want to do
i want to try to convert images into a string format and store them on
disk. Better if they are unprintable characters.
then when i want to see them i will run them thrugh a decryption python
script and viola they will be displayed in pygame
when i close the program again i will have only the encoded 'imageds' on
the hd.
I did this using
get_screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH)
imagestr = base64.b64encode(imageFile.read())
## create the text file name to save to disk
text_filename = newpath + "s" + str(va.page) + ".dat"
## save imagestr(image as a textfle) to disk
f.write(imagestr)
I wanted a better way and hence the question.
stringio or cstringio is fine. i can save the string to disk but how can
i convert it back to an image file?
Thank you for your help.
On Tue, Aug 12, 2014 at 11:12 AM, Radomir Dopieralski <
Post by Radomir Dopieralski
Post by diliup gabadamudalige
type(f) gives <type 'instance'>
yourimage = f.getvalue()
f.write(yourimage)
so i coukld write this file to disk
now how can i convert this back to an image?
Thanks in advance
What are you actually tryig to do?
I'm sure there is a simpler way.
--
The Sheep
--
Diliup Gabadamudalige
http://www.diliupg.com
http://soft.diliupg.com/
**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you
are not the intended recipient or have received it in error, please delete
it and all copies from your system and notify the sender immediately by
return e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
--
Diliup Gabadamudalige
http://www.diliupg.com
http://soft.diliupg.com/
**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you
are not the intended recipient or have received it in error, please delete
it and all copies from your system and notify the sender immediately by
return e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
--
Diliup Gabadamudalige
http://www.diliupg.com
http://soft.diliupg.com/
**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
diliup gabadamudalige
2014-08-13 14:35:25 UTC
Permalink
This is the complete and correct code. Can anyone say what;s wrong? Thanks
in advance.


import pygame
import StringIO

putimage = pygame.image.load("88keykbd.png")
buff = StringIO.StringIO()
buff.name = '88keykbd.png'

pygame.image.save(putimage, buff)

putimage = buff.getvalue()
print "buff:", type(buff), "myimage:", type(putimage), "getimage:",
type(putimage)

print len(putimage)
with open('myscrambledimage.dat', 'w') as newfile:
newfile.write(putimage)

with open('myscrambledimage.dat', 'r') as newfile:
getimage= newfile.read()
print len(getimage)
Post by Noel Garwick
since myimage is using pygame.image.load, it might be indexed? On the
other hand, it's also possible that pygame.image.save is actually
performing some compression when it saves to a png format? I'm not sure
offhand, but something to check out.
Post by diliup gabadamudalige
Sorry there was a small error in the previous message. My apologies.
import pygame
import StringIO
myimage = pygame.image.load("88keykbd.png")
buff = StringIO.StringIO()
buff.name = '88keykbd.png'
pygame.image.save(myimage, buff)
putimage = buff.getvalue()
print "buff:", type(buff), "myimage:", type(myimage), "getimage:",
type(putimage)
print len(putimage)
newfile.write(putimage)
getimage= newfile.read()
print len(getimage)
Post by diliup gabadamudalige
can any good soul out there tell me why these two files are not the same
size?
import pygame
import StringIO
myimage = pygame.image.load("88keykbd.png")
buff = StringIO.StringIO()
buff.name = '88keykbd.png'
pygame.image.save(myimage, buff)
putimage = buff.getvalue()
print "buff:", type(buff), "myimage:", type(myimage), "getimage:",
type(putimage)
print len(getimage)
newfile.write(putimage)
getimage= newfile.read()
print len(getimage)
len(putimage) and len(getimage) are hugely different in size and I can't
see why as it is putimage that is written to file and then read back as
getimage.
Clarification on this would be immensly welcome.
Thank you in advance
On Tue, Aug 12, 2014 at 9:53 PM, diliup gabadamudalige <
Post by diliup gabadamudalige
Hi Radomir,
Thanks for the response.
ok. This is what i want to do
i want to try to convert images into a string format and store them on
disk. Better if they are unprintable characters.
then when i want to see them i will run them thrugh a decryption python
script and viola they will be displayed in pygame
when i close the program again i will have only the encoded 'imageds'
on the hd.
I did this using
get_screen = SCREEN.subsurface(0, 0, SCREENW, SCREENH)
imagestr = base64.b64encode(imageFile.read())
## create the text file name to save to disk
text_filename = newpath + "s" + str(va.page) + ".dat"
## save imagestr(image as a textfle) to disk
f.write(imagestr)
I wanted a better way and hence the question.
stringio or cstringio is fine. i can save the string to disk but how
can i convert it back to an image file?
Thank you for your help.
On Tue, Aug 12, 2014 at 11:12 AM, Radomir Dopieralski <
Post by Radomir Dopieralski
Post by diliup gabadamudalige
type(f) gives <type 'instance'>
yourimage = f.getvalue()
f.write(yourimage)
so i coukld write this file to disk
now how can i convert this back to an image?
Thanks in advance
What are you actually tryig to do?
I'm sure there is a simpler way.
--
The Sheep
--
Diliup Gabadamudalige
http://www.diliupg.com
http://soft.diliupg.com/
**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you
are not the intended recipient or have received it in error, please delete
it and all copies from your system and notify the sender immediately by
return e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
--
Diliup Gabadamudalige
http://www.diliupg.com
http://soft.diliupg.com/
**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you
are not the intended recipient or have received it in error, please delete
it and all copies from your system and notify the sender immediately by
return e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
--
Diliup Gabadamudalige
http://www.diliupg.com
http://soft.diliupg.com/
**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you
are not the intended recipient or have received it in error, please delete
it and all copies from your system and notify the sender immediately by
return e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
--
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
Radomir Dopieralski
2014-08-13 16:40:39 UTC
Permalink
Post by diliup gabadamudalige
This is the complete and correct code. Can anyone say what;s wrong?
Thanks in advance.
import pygame
import StringIO
putimage = pygame.image.load("88keykbd.png")
buff = StringIO.StringIO()
buff.name <http://buff.name> = '88keykbd.png'
pygame.image.save(putimage, buff)
putimage = buff.getvalue()
print "buff:", type(buff), "myimage:", type(putimage), "getimage:",
type(putimage)
print len(putimage)
newfile.write(putimage)
getimage= newfile.read()
print len(getimage)
Open both files in binary mode, with 'wb' and 'rb' respectively.
Especially if you are on some funny operating system, like Windows.

Have you considered just putting all your images in a zip file, instead
of encoding them in strange ways?
--
The Sheep
diliup gabadamudalige
2014-08-14 17:07:22 UTC
Permalink
I am reading up on creating and using zip files and may possibly use it,
What I am trying to do here is to hide the images on disk so that a user of
the program will not be able to manipulate or change the images. What other
way can you suggest?
Thanks for all the help so far.
Post by Radomir Dopieralski
Post by diliup gabadamudalige
This is the complete and correct code. Can anyone say what;s wrong?
Thanks in advance.
import pygame
import StringIO
putimage = pygame.image.load("88keykbd.png")
buff = StringIO.StringIO()
buff.name <http://buff.name> = '88keykbd.png'
pygame.image.save(putimage, buff)
putimage = buff.getvalue()
print "buff:", type(buff), "myimage:", type(putimage), "getimage:",
type(putimage)
print len(putimage)
newfile.write(putimage)
getimage= newfile.read()
print len(getimage)
Open both files in binary mode, with 'wb' and 'rb' respectively.
Especially if you are on some funny operating system, like Windows.
Have you considered just putting all your images in a zip file, instead
of encoding them in strange ways?
--
The Sheep
--
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
Carlos Zuniga
2014-08-14 17:31:22 UTC
Permalink
On Thu, Aug 14, 2014 at 12:07 PM, diliup gabadamudalige
Post by diliup gabadamudalige
I am reading up on creating and using zip files and may possibly use it,
What I am trying to do here is to hide the images on disk so that a user of
the program will not be able to manipulate or change the images. What other
way can you suggest?
Thanks for all the help so far.
That's a pretty hard thing to do. If the user is supposed to be able
to open and see the images from the program, then they can still
PrintScreen and grab the image. All I can suggest there is adding a
watermark, so you at least know if they are grabbing it from the
screen.

If you zip the images with a password, then that password will still
be in the script, the user will be able to obtain it. You can try to
obfuscate the password but that only goes so far, anyone with some
python knowledge would be able to find it out.

Maybe compile the sensitive parts with cython and hope the users are
not curious?
Noel Garwick
2014-08-14 17:33:29 UTC
Permalink
Password protected zip would work.

You can also look into what renpy's .rpa format does.
Post by diliup gabadamudalige
I am reading up on creating and using zip files and may possibly use it,
What I am trying to do here is to hide the images on disk so that a user
of the program will not be able to manipulate or change the images. What
other way can you suggest?
Thanks for all the help so far.
Post by Radomir Dopieralski
Post by diliup gabadamudalige
This is the complete and correct code. Can anyone say what;s wrong?
Thanks in advance.
import pygame
import StringIO
putimage = pygame.image.load("88keykbd.png")
buff = StringIO.StringIO()
buff.name <http://buff.name> = '88keykbd.png'
pygame.image.save(putimage, buff)
putimage = buff.getvalue()
print "buff:", type(buff), "myimage:", type(putimage), "getimage:",
type(putimage)
print len(putimage)
newfile.write(putimage)
getimage= newfile.read()
print len(getimage)
Open both files in binary mode, with 'wb' and 'rb' respectively.
Especially if you are on some funny operating system, like Windows.
Have you considered just putting all your images in a zip file, instead
of encoding them in strange ways?
--
The Sheep
--
Diliup Gabadamudalige
http://www.diliupg.com
http://soft.diliupg.com/
**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
diliup gabadamudalige
2014-08-14 17:38:45 UTC
Permalink
Thank you very much Carlos Zuniga and Noel Garwick for your suggestions. I
will look into both matters.
Post by Noel Garwick
Password protected zip would work.
You can also look into what renpy's .rpa format does.
Post by diliup gabadamudalige
I am reading up on creating and using zip files and may possibly use it,
What I am trying to do here is to hide the images on disk so that a user
of the program will not be able to manipulate or change the images. What
other way can you suggest?
Thanks for all the help so far.
On Wed, Aug 13, 2014 at 10:10 PM, Radomir Dopieralski <
Post by Radomir Dopieralski
Post by diliup gabadamudalige
This is the complete and correct code. Can anyone say what;s wrong?
Thanks in advance.
import pygame
import StringIO
putimage = pygame.image.load("88keykbd.png")
buff = StringIO.StringIO()
buff.name <http://buff.name> = '88keykbd.png'
pygame.image.save(putimage, buff)
putimage = buff.getvalue()
print "buff:", type(buff), "myimage:", type(putimage), "getimage:",
type(putimage)
print len(putimage)
newfile.write(putimage)
getimage= newfile.read()
print len(getimage)
Open both files in binary mode, with 'wb' and 'rb' respectively.
Especially if you are on some funny operating system, like Windows.
Have you considered just putting all your images in a zip file, instead
of encoding them in strange ways?
--
The Sheep
--
Diliup Gabadamudalige
http://www.diliupg.com
http://soft.diliupg.com/
**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you
are not the intended recipient or have received it in error, please delete
it and all copies from your system and notify the sender immediately by
return e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
--
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**********************************************************************************************
Loading...