Is the following function correct, this code is meant to add a phrase to image. Note that i cannot use image.text function or any other but can only use getpixel, putpixel, load, and save.
def insertTxtImage(srcImage, phrase):
pixel = srcImage.getpixel(30,30);
srcImage.putpixel(pixel,phrase);
srcImage.save;
pass
Yes it is homework which can only use getpixel, putpixel, load, and save to insert a phrase in to the image.
I tried to do this with this code but it is giving system error (argument is not a tuple)
def insertTxtImage(srcImage, phrase):
pix = srcImage.load()
pix[0,0] = phrase
srcImage.save()
pass
Thanks for the comments.
No, the functions you are using modify pixels.
To draw font you want to use something like following:
f= pygame.font.Font(None, 12)
surf= f.render(phrase)
srcImage.blit(surf, (30,30))
for more documentation see here: (scroll down a bit)
http://www.pygame.org/docs/ref/font.html
EDIT: nvm, I don't even know what you're doing or trying to do
Related
I new to python and stuck in for loop function can someone help on on how can I get value of "i" in next function.
formula
from pixelate import pixelate
for i in range(197):
pixelate('/Users/amanrai/Desktop/lady/%i.png', '/Users/amanrai/Desktop/lady/rotate/%i.png', 20)
I want the "i" value in after the directory name
would appreciate if some expert can help me.
you can use
for i in range(197):
img = f'/Users/amanrai/Desktop/lady/{i}.png'
img_rotate = f'/Users/amanrai/Desktop/lady/{i}.png'
pixelate(img, img_rotate, 20)
You can use format:
for i in range(197):
pixelate('/Users/amanrai/Desktop/lady/{}.png'.format(i), '/Users/amanrai/Desktop/lady/rotate/{}.png'.format(i), 20)
See this wonderful cheat sheet for more information.
Use an f-string:
pixelate(f'/Users/amanrai/Desktop/lady/{i}.png', f'/Users/amanrai/Desktop/lady/rotate/{i}.png', 20)
String interpolation or f-string for short allow you to put variables inside a string. This feature was added in Python 3.6. If you are a previous version you must use str.format instead.
Hope this helps:
for i in range(197):
pixelate(f"/Users/amanrai/Desktop/lady/{i}.png', '/Users/amanrai/Desktop/lady/rotate/{i}.png', 20")
I am trying to get the output of the function into a variable instead of just putting it into the console
output = pywhatkit.info(search, lines=15)
but it is not working,
it just displays the texts in the cmd console instead of putting the result into the variable.
any kind of help is much appreciated
If you look at the source code, you could see that pywhatkit.info function is just delegating it's parameters to wikipedia.summary and displaying it's result to the console.
def info(topic,lines=3):
'''Gives information on a topic from wikipedia.'''
spe = wikipedia.summary(topic, sentences = lines)
print(spe)
So if you want to get the result of pywhatkit.info into a variable all you need to implement the following
output = wikipedia.summary(search, sentences=15)
I want to change the text color of msgbox, ynbox and the output text in easyGUI.
Please help!
Quoting from this site:
EasyGUI won;t help change colour, it is just a way of making
a command line script act like a GUI - by popping up dialog
boxes to capture input and dsplay messages. It replaces
raw_input and print with dialogs.
--------code--------
from WConio import textcolor
apples_left = 5
print "There are",(textcolor(4)),apples_left,(textcolor(7)),"left in the basket."
The output works, but there is one snag. I think that I am leaving
something out because it has the word "None" on both sides of the
variable.
This could be an easy solution for your problem.
You may get WConio from here:
http://newcenturycomputers.net/projects/wconio.html
If you face python not found error at the time of installation then you may try following this answer.
UPDATE:
There is one more package named Colorama, which is cross-platform for colored terminal text and pretty much stable. You may give it a try as well.
I have recently needed to get the icon for any file type, and I want a big one, so I am using SHImageList, as this C# answer suggested.
I have had difficulty getting anything to work. I was using SHGetFileInfo, but that only returns 32x32px icons, and I would prefer the 256x256 ones.
Based on the reference I can figure out the first two arguments, but I have yet to figure out what the third argument is. Am I just supposed to put None? Though based on this blog post the third argument is an HImageList, but I have yet to find the structure of any HImageList.The relavent snippet of my code is:
SHIL_JUMBO = 0x000000004
iidImageList=GUID("{46EB5926-582E-4017-9FDF-E8998DAA0950}")
hico=??
hres=ctypes.windll.shell32.SHGetImageList(SHIL_JUMBO,byref(iidImageList),hico)
hdc.DrawIcon( (0,0), hico )
hbmp.SaveBitmapFile( hdc, tempDirectory + "\Icontemp.bmp")
How should I initialize hico and of what type??
Also, to avoid the XY problem, an answer to this question can give an alternative way of getting the icon of any file type. (Preferably using Pillow or PyQt)
EDIT: Well, I guess I wasn't clear about what I need. I need to initiate an HImageList type. I changed the title, to reflect the edits. Also, to the XY problem. A requirement is that the program is in Python (unless it can be shown that that is impossible).
SHGetImageList returns an image list, not an icon.
You need to use it in conjunction with SHGetFileInfo:
SHGetFileInfo gives you the icon index within the system image list (use the SHGFI_SYSICONINDEX flag)
SHGetImageList gives you the jumbo image list (use SHIL_JUMBO).
You can then draw the icon using ImageList_DrawEx, or extract it from the icon list as an HICON using the ImageList_ExtractIcon macro or by calling ImageList_GetIcon. Note that although SHGetImageList returns an IImageList interface, a pointer to these is freely convertible to HIMAGELIST.
Psuedo-code:
SHFILEINFO sfi;
SHGetFileInfo(L"c:\file.txt", 0, &sfi, sizeof(sfi),
SHGFI_SYSICONINDEX);
HIMAGELIST hil;
SHGetImageList(SHIL_JUMBO, IID_IImageList, &hil);
ImageList_DrawEx(hil, sfi.iIcon, hdc, x, y, 0, 0,
CLR_NONE, CLR_NONE, ILD_NORMAL);
I was wondering whether it is possible to process a string in python, using sphinx. Basically, I would like to pass a string containing restructured text to sphinx, and then generate the corresponding HTML output. I meant something like this
import sphinx.something
s = 'some restructured text'
html_out = sphinx.something(s)
However, I could not find anything along these lines. So, is this possible, and if so, how would one do this?
The quickest solution I've seen is:
from docutils.examples import html_body
def rst(rstStr):
return html_body(rstStr, input_encoding='utf-8',
output_encoding='utf-8').strip()
I'd be interested myself in better solutions..