Re: [python-win32] windows images on clipboard

2021-08-16 Thread Konovalov, Vadim
Here is the better tcl/tk version of script which then could be reused with 
tkinter to solve the task

#
# rotate image in clipboard
#
# TBD - user-friendly - message box if no image in clipboard, also - "ready" 
message
#
# most code from here - http://wiki.tcl.tk/15647
#

package require Tk
package require img::bmp
package require base64
package require twapi

#twapi::open_clipboard
#foreach {i} [twapi::get_clipboard_formats] {
#  puts "$i"
#  # [twapi::get_registered_clipboard_format_name $i]
#}
#twapi::close_clipboard

# Copy the contents of the Windows clipboard into a photo image.
# Return the photo image identifier.
proc Clipboard2Img {} {
twapi::open_clipboard

# Assume clipboard content is in format 8 (CF_DIB)
set retVal [catch {twapi::read_clipboard 8} clipData]
if { $retVal != 0 } {
error "Invalid or no content in clipboard"
}

# First parse the bitmap data to collect header information
binary scan $clipData "iiissii" \
   size width height planes bitcount compression sizeimage \
   xpelspermeter ypelspermeter clrused clrimportant

# We only handle BITMAPINFOHEADER right now (size must be 40)
if {$size != 40} {
error "Unsupported bitmap format. Header size=$size"
}

# We need to figure out the offset to the actual bitmap data
# from the start of the file header. For this we need to know the
# size of the color table which directly follows the BITMAPINFOHEADER
if {$bitcount == 0} {
error "Unsupported format: implicit JPEG or PNG"
} elseif {$bitcount == 1} {
set color_table_size 2
} elseif {$bitcount == 4} {
# TBD - Not sure if this is the size or the max size
set color_table_size 16
} elseif {$bitcount == 8} {
# TBD - Not sure if this is the size or the max size
set color_table_size 256
} elseif {$bitcount == 16 || $bitcount == 32} {
if {$compression == 0} {
# BI_RGB
set color_table_size $clrused
} elseif {$compression == 3} {
# BI_BITFIELDS
set color_table_size 3
} else {
error "Unsupported compression type '$compression' for bitcount 
value $bitcount"
}
} elseif {$bitcount == 24} {
set color_table_size $clrused
} else {
error "Unsupported value '$bitcount' in bitmap bitcount field"
}

set phImg [image create photo]
set filehdr_size 14 ; # sizeof(BITMAPFILEHEADER)
set bitmap_file_offset [expr {$filehdr_size+$size+($color_table_size*4)}]
set filehdr [binary format "a2 i x2 x2 i" \
 "BM" [expr {$filehdr_size + [string length $clipData]}] \
 $bitmap_file_offset]

append filehdr $clipData
$phImg put $filehdr -format bmp

twapi::close_clipboard
return $phImg
}

# Copy photo image "phImg" into Windows clipboard.
proc Img2Clipboard { phImg } {
# First 14 bytes are bitmapfileheader - get rid of this
set data [string range [base64::decode [$phImg data -format bmp]] 14 end]
twapi::open_clipboard
twapi::empty_clipboard
twapi::write_clipboard 8 $data
twapi::close_clipboard
}

proc rotate { phImg angle } {
set w [image width  $phImg]
set h [image height $phImg]

switch -- $angle {
180 {
set tmp [image create photo -width $w -height $h]
$tmp copy $phImg -subsample -1 -1
return $tmp
}
270 - 90 - -90 {
set tmp [image create photo -width $h -height $w]
set matrix [string repeat "{[string repeat {0 } $h]} " $w]
if { $angle == -90 || $angle == 270 } {
set x0 0; set y [expr {$h-1}]; set dx 1; set dy -1
} else {
set x0 [expr {$w-1}]; set y 0; set dx -1; set dy 1
}
foreach row [$phImg data] {
set x $x0
foreach pixel $row {
lset matrix $x $y $pixel
incr x $dx
}
incr y $dy
}
$tmp put $matrix
return $tmp
}
default {
error "Invalid angle $angle specified"
}
}
}

Img2Clipboard [rotate [Clipboard2Img] [lindex $argv 0]]

destroy .



Internal Use - Confidential
From: Konovalov, Vadim
Sent: Friday, August 13, 2021 10:36 AM
To: Mriswithe; Tim Roberts
Cc: python-win32@python.org
Subject: RE: [python-win32] windows images on clipboard

There are 2 ways to solve this task, actually.

1st way is a bit more difficult but more efficient.

Read 
https://docs.microsoft.com/en-us/windows/win32/dataxchg/using-the-clipboard#copying-information-to-the-clipboard
 and implement this in python module.



This was done for perl and worked for me in Win32::Clipboard module

https://git

Re: [python-win32] windows images on clipboard

2021-08-16 Thread Konovalov, Vadim
d_text "$s"
twapi::close_clipboard
}

set skip 0
proc lowercase {} {
global skip
if {$skip != 0} {
  set skip 0
  return
}
before_get
twapi::open_clipboard
set s [twapi::read_clipboard_text]
twapi::empty_clipboard
twapi::write_clipboard_text [string tolower "$s"]
twapi::close_clipboard
after_put
}
proc uppercase {} {
before_get
twapi::open_clipboard
set s [twapi::read_clipboard_text]
twapi::empty_clipboard
twapi::write_clipboard_text [string toupper "$s"]
twapi::close_clipboard
set skip 1
after_put
}

# Copy the contents of the Windows clipboard into a photo image.
# Return the photo image identifier.
proc Clipboard2Img {} {
twapi::open_clipboard

# Assume clipboard content is in format 8 (CF_DIB)
set retVal [catch {twapi::read_clipboard 8} clipData]
if { $retVal != 0 } {
error "Invalid or no content in clipboard"
}

# First parse the bitmap data to collect header information
binary scan $clipData "iiissii" \
   size width height planes bitcount compression sizeimage \
   xpelspermeter ypelspermeter clrused clrimportant

# We only handle BITMAPINFOHEADER right now (size must be 40)
if {$size != 40} {
error "Unsupported bitmap format. Header size=$size"
}

# We need to figure out the offset to the actual bitmap data
# from the start of the file header. For this we need to know the
# size of the color table which directly follows the BITMAPINFOHEADER
if {$bitcount == 0} {
error "Unsupported format: implicit JPEG or PNG"
} elseif {$bitcount == 1} {
set color_table_size 2
} elseif {$bitcount == 4} {
# TBD - Not sure if this is the size or the max size
set color_table_size 16
} elseif {$bitcount == 8} {
# TBD - Not sure if this is the size or the max size
set color_table_size 256
} elseif {$bitcount == 16 || $bitcount == 32} {
if {$compression == 0} {
# BI_RGB
set color_table_size $clrused
} elseif {$compression == 3} {
# BI_BITFIELDS
set color_table_size 3
} else {
error "Unsupported compression type '$compression' for bitcount 
value $bitcount"
}
} elseif {$bitcount == 24} {
set color_table_size $clrused
} else {
error "Unsupported value '$bitcount' in bitmap bitcount field"
}

set phImg [image create photo]
set filehdr_size 14 ; # sizeof(BITMAPFILEHEADER)
set bitmap_file_offset [expr {$filehdr_size+$size+($color_table_size*4)}]
set filehdr [binary format "a2 i x2 x2 i" \
 "BM" [expr {$filehdr_size + [string length $clipData]}] \
 $bitmap_file_offset]

append filehdr $clipData
$phImg put $filehdr -format bmp

twapi::close_clipboard
return $phImg
}

# Copy photo image "phImg" into Windows clipboard.
proc Img2Clipboard { phImg } {
# First 14 bytes are bitmapfileheader - get rid of this
set data [string range [base64::decode [$phImg data -format bmp]] 14 end]
twapi::open_clipboard
twapi::empty_clipboard
twapi::write_clipboard 8 $data
twapi::close_clipboard
}

proc rotate { phImg angle } {
set w [image width  $phImg]
set h [image height $phImg]

switch -- $angle {
180 {
set tmp [image create photo -width $w -height $h]
$tmp copy $phImg -subsample -1 -1
return $tmp
}
270 - 90 - -90 {
set tmp [image create photo -width $h -height $w]
set matrix [string repeat "{[string repeat {0 } $h]} " $w]
if { $angle == -90 || $angle == 270 } {
set x0 0; set y [expr {$h-1}]; set dx 1; set dy -1
} else {
set x0 [expr {$w-1}]; set y 0; set dx -1; set dy 1
}
foreach row [$phImg data] {
set x $x0
foreach pixel $row {
lset matrix $x $y $pixel
incr x $dx
}
incr y $dy
}
$tmp put $matrix
return $tmp
}
default {
error "Invalid angle $angle specified"
}
}
}


set fhwnd 0
#proc tcl::mmotion {} {$fhwnd = Win32::GuiTest::GetForegroundWindow}
proc before_get {} {
#Win32::GuiTest::SendKeys("%{TAB}{PAUSE 100}^{C}");
#пока не работает :(
#$fhwnd = Win32::GuiTest::GetForegroundWindow;
global fhwnd
set fhwnd [twapi::get_foreground_window]
}
proc after_put {} {
global fhwnd
twapi::set_foreground_window $fhwnd
##Win32::GuiTest::SendKeys("%{TAB}");
##Win32::GuiTest::SendKeys("^{V}");
##Win32::GuiTest::SendMessage($fhwnd,0x100,VK_HOME,0);
#  

Re: [python-win32] windows images on clipboard

2021-08-12 Thread Mriswithe
Awesome, thank you so much for that info. That makes my tail chasing make
more sense! Thanks for saving me from additional posterior extension
pursuit.

Last question I think, how do I put a file path reference on the clipboard
similar to in explorer selecting a few files and hitting control c ?

I think I had tested that with discord and it worked, but not 100% on that.

On Thu, Aug 12, 2021, 11:57 AM Tim Roberts  wrote:

> Mriswithe wrote:
> >
> > I was looking at making a little helper app for Windows that will take
> > an image on your clipboard and ensure it is under 8MB for posting to
> > discord, and if it isn't, use Pillow to resize it until it is the
> > right size.
> >
> > I can use Pillow's ImageGrab.grabclipboard() to get the image off the
> > clipboard, but I am a little confused about writing it back. I have
> > been back and forth between the pywin32 source and the windows docs
> > for the windows C API, but I don't have any previous experience or
> > context to know what some pieces are intended to do.
> >
> > I found an example to write to it from StackOverflow
> > (https://stackoverflow.com/questions/34322132/copy-image-to-clipboard
> > ),
>
> > but I was wanting to dig a little deeper to see what formats other
> > than BMP I could use to put on the clipboard. My ignorance of C++ and
> > the Windows APIs and hell the Python C API is really biting me here.
>
> You can't.  You have to write it as a DIB (Device Independent Bitmap),
> which is the format in a .BMP file.
>
> The Windows clipboard was designed in roughly 1986, before GIF, before
> JPEG and way, way before PNG.  The clipboard is designed for universal
> interchange, so it really does need to spec the lowest common
> denominator.  If they allowed PNGs, then all of the graphics application
> in the world would have to be modified to decode PNG data.
>
> So, to make your app work, save the result as a BMP.
>
>
> > Is there a bit of an idiots example guide for how to say, put a PNG on
> > the clipboard or a JPG? Some example code? There is a ton of useful
> > info in the pywin32 repo, but I haven't found anything that has made
> > this click for me.
>
> There is no idiots guide, because it cannot be done.  Well, technically
> speaking you can put arbitrary binary data into the clipboard, but other
> graphics applications will not be able to read it.  When they look for
> image data, they look for format CF_DIB, and that means a .BMP.
>
> --
> Tim Roberts, t...@probo.com
> Providenza & Boekelheide, Inc.
>
>
> ___
> python-win32 mailing list
> python-win32@python.org
> https://mail.python.org/mailman/listinfo/python-win32
>
___
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32


Re: [python-win32] windows images on clipboard

2021-08-12 Thread Dennis Lee Bieber
On Wed, 11 Aug 2021 18:33:14 -0500, Mriswithe 
declaimed the following:

>
>Is there a bit of an idiots example guide for how to say, put a PNG on the
>clipboard or a JPG? Some example code? There is a ton of useful info in the
>pywin32 repo, but I haven't found anything that has made this click for me.
>
From a quick browse in Google -- IMAGES are only in one of the Windows
bitmap formats when on the clipboard; ie, they've been "rendered" to a
bitmap memory structure. PNG or JPG -- those are file formats, not rendered
images. There does appear to be a CF_TIFF format.


-- 
Wulfraed Dennis Lee Bieber AF6VN
wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/

___
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32


Re: [python-win32] windows images on clipboard

2021-08-12 Thread Tim Roberts

Mriswithe wrote:


I was looking at making a little helper app for Windows that will take 
an image on your clipboard and ensure it is under 8MB for posting to 
discord, and if it isn't, use Pillow to resize it until it is the 
right size.


I can use Pillow's ImageGrab.grabclipboard() to get the image off the 
clipboard, but I am a little confused about writing it back. I have 
been back and forth between the pywin32 source and the windows docs 
for the windows C API, but I don't have any previous experience or 
context to know what some pieces are intended to do.


I found an example to write to it from StackOverflow 
(https://stackoverflow.com/questions/34322132/copy-image-to-clipboard 
), 
but I was wanting to dig a little deeper to see what formats other 
than BMP I could use to put on the clipboard. My ignorance of C++ and 
the Windows APIs and hell the Python C API is really biting me here.


You can't.  You have to write it as a DIB (Device Independent Bitmap), 
which is the format in a .BMP file.


The Windows clipboard was designed in roughly 1986, before GIF, before 
JPEG and way, way before PNG.  The clipboard is designed for universal 
interchange, so it really does need to spec the lowest common 
denominator.  If they allowed PNGs, then all of the graphics application 
in the world would have to be modified to decode PNG data.


So, to make your app work, save the result as a BMP.


Is there a bit of an idiots example guide for how to say, put a PNG on 
the clipboard or a JPG? Some example code? There is a ton of useful 
info in the pywin32 repo, but I haven't found anything that has made 
this click for me.


There is no idiots guide, because it cannot be done.  Well, technically 
speaking you can put arbitrary binary data into the clipboard, but other 
graphics applications will not be able to read it.  When they look for 
image data, they look for format CF_DIB, and that means a .BMP.


--
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.




smime.p7s
Description: S/MIME Cryptographic Signature
___
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32


[python-win32] windows images on clipboard

2021-08-11 Thread Mriswithe
I was looking at making a little helper app for Windows that will take an
image on your clipboard and ensure it is under 8MB for posting to discord,
and if it isn't, use Pillow to resize it until it is the right size.

I can use Pillow's ImageGrab.grabclipboard() to get the image off the
clipboard, but I am a little confused about writing it back. I have been
back and forth between the pywin32 source and the windows docs for the
windows C API, but I don't have any previous experience or context to know
what some pieces are intended to do.

I found an example to write to it from StackOverflow (
https://stackoverflow.com/questions/34322132/copy-image-to-clipboard), but
I was wanting to dig a little deeper to see what formats other than BMP I
could use to put on the clipboard. My ignorance of C++ and the Windows APIs
and hell the Python C API is really biting me here.

Is there a bit of an idiots example guide for how to say, put a PNG on the
clipboard or a JPG? Some example code? There is a ton of useful info in the
pywin32 repo, but I haven't found anything that has made this click for me.

Thanks for any help!
Andrew
___
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32