Je suis tombé récemment sur quelque chose qui m'a intrigué ! Alors que je cherchais comment faire pour mettre une image en fond d'écran sur une fenêtre dans le language python, je suis tombé sur un site avec le code suivant qui génère sa propre et image et qui l'a répète à l'infini en arrière plan sur une fenêtre (vous pouvez l'essayer, il marche) :
# wallpaper/tile a wxPython panel using a base 64 encoded image string
"""
# the image string is created with this simple Python code
# and the string is then copied and pasted to the program
import base64
jpg_file = "BG_Egypt.jpg"
print \
"jpg_b64='''\\\n" + base64.encodestring(open(jpg_file,"rb").read()) + "'''"
"""
# tested with Python24 and wxPython26 vegaseat 06mar2007
import wx
import cStringIO
import base64
BG_Egypt_jpg_b64='''\
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcU
FhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgo
KCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAgACADASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx
BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK
U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3
uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwB2taqm
lRI7pAsCxQDi0WVizqo7KzEln9+v55f/AAlsZ/5dp+f+oI//AMa9/wDPc8c8wW3f97p3v/HD9f8A
P66mtaqmlRI7pAsCxQDi0WVizqo7KzEln9+v5+J9+9tDQy/+EtjP/LtPz/1BH/8AjXv/AJ7x67q0
9xo8Lxp5AlurUc2ggk2mdAeCoYAgn6g9wea2r+Inv7aK3tILpZZLm3yV0t4fl85C2X8sYG3OeeQS
O/N3xzzBbd/3une/8cP1/wA/qbWab36gHjnmC27/AL3Tvf8Ajh+v+f1PHPMFt3/e6d7/AMcP1/z+
uXr2sDV1hRIb553urU86fLGoVJYz3QKAFH+e+hresafcXapJDfSiHyDxYTsvmRhCOQhDAMo9QfcH
k1te3W4Fu98UW0E/kQW8xWNEQkaQ7gsFAb5vLO75ieckHtnvia9rA1dYUSG+ed7q1POnyxqFSWM9
0CgBR/nvqf8ACWxn/l2n5/6gj/8Axr3/AM9z/hLYz/y7T8/9QR//AI17/wCe58mB/9k=
'''
jpg1 = base64.b64decode(BG_Egypt_jpg_b64)
# save the image to a file ...
# (couldn't figure out any other way to use with wx.Bitmap)
tile_file = "zzz777.jpg"
fout = open(tile_file, "wb")
fout.write(jpg1)
fout.close()
class Panel1(wx.Panel):
"""
class Panel1 creates a panel for the tile image
fw and fh are the width and height of the base frame
"""
def __init__(self, parent, id, fw, fh, tile_file):
# create the panel
wx.Panel.__init__(self, parent, id)
# frame/panel width and height
self.fw = fw
self.fh = fh
# load the wallpaper/tile file
self.bmp1 = wx.Bitmap(tile_file)
# do the wall papering ...
wx.EVT_PAINT(self, self.on_paint)
# now put a button on the panel, on top of wallpaper
self.button1 = wx.Button(self, -1, label='Button1', pos=(10, 5))
def on_paint(self, event=None):
# create paint surface
dc = wx.PaintDC(self)
dc.Clear()
#dc.SetBackgroundMode(wx.TRANSPARENT)
# get image width and height
iw = self.bmp1.GetWidth()
ih = self.bmp1.GetHeight()
# tile/wallpaper the image across the canvas
for x in range(0, self.fw, iw):
for y in range(0, self.fh, ih):
dc.DrawBitmap(self.bmp1, x, y, True)
app = wx.PySimpleApp()
# create a window/frame instance, no parent, -1 is default ID
fw = 400
fh = 300
frame1 = wx.Frame(None, -1, "wall-papering a wx.PaintDC surface", size=(fw, fh))
# create a panel class instance
panel1 = Panel1(frame1, -1, fw, fh, tile_file)
frame1.Show(True)
# start the event loop
app.MainLoop()
Est-ce que quelqu'un saurait comment est-ce qu'on peut se procurer (ou générer par nous même) du code qui est à l'origine de ces images ?
... et en l'encodant en base 64 pour "compresser" les données, apparemment.
Par exemple:
>>> import base64
>>> with open('lolsmiley.png', 'rb') as img:
... png = base64.b64encode(img.read())
...
>>> png
# ... données de ton image encodées en base 64 ...
Ceci n'est évidemment utile que si ton image à la base est de toute petite taille, sinon il va de soi qu'il vaut mieux fournir le fichier image, plutôt que de l'embarquer dans ton code.
Là, j'ai sauvegardé les données pour écrire l'image jpg, puis je l'ai chargée et réencodée pour vérifier que j'obtiens bien le même code.
Tu peux vérifier par toi-même le fichier "test.jpg" qui est créé, il correspond bien à l'image dans ton code. La seule différence entre les données chargées réencodées et les données de base sont que les données de base contiennent des sauts de ligne pour éviter de rendre le code illisible, mais cela n'a aucune incidence sur l'image finale.
Ok, merci beaucoup pour l'info ! J'ai essayé et ça marche bien.
Sympas cette otion tout de même
Comment faire une "tile image" ?
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
Python c'est bon, mangez-en.