flip image horizontally

Just starting out? Need help? Post your questions and find answers here.
Chuf
User
User
Posts: 11
Joined: Sun Jun 14, 2015 8:25 pm

flip image horizontally

Post by Chuf »

Hello,

I would like to flip the image horizontally on OSX and Windows
As an example I want a helmet is photographed by the left side, so pointing to the left
then points to the right so than when it is photographed by the right
I have been looking for a while in all the forums but found nothing
can anyone help me?
when it comes it should not be Windows-specific because I'm just about a program
writing under Windows and OSX

many Greetings
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: flip image horizontally

Post by IdeasVacuum »

What you are looking for is this really excellent code by Luis: http://www.purebasic.fr/english/viewtop ... 12&t=38975
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: flip image horizontally

Post by davido »

@chuf,

You might find this excellent code interesting, too:
http://www.purebasic.fr/english/viewtop ... 28#p445225
DE AA EB
Chuf
User
User
Posts: 11
Joined: Sun Jun 14, 2015 8:25 pm

Re: flip image horizontally

Post by Chuf »

many thanks for the answer, im using Luis code the first test by OS X Yosemite works very fine, my next test is by windows
infratec
Always Here
Always Here
Posts: 7587
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: flip image horizontally

Post by infratec »

Hi,

try this too:

Code: Select all

UseJPEGImageDecoder()
UseJPEGImageEncoder()

Filename$ = OpenFileRequester("Choose an image", "", "JPG|*.jpg", 0)
If Len(Filename$)
  OrgImg = LoadImage(#PB_Any, Filename$)
  If OrgImg
    
    Width = ImageWidth(OrgImg)
    Height = ImageHeight(OrgImg)
    
    NewImg = CreateImage(#PB_Any, Width, Height)
    If NewImg
      
      If StartDrawing(ImageOutput(NewImg))
        
        For x = Width - 1 To 0 Step -1
          StripeImg = GrabImage(OrgImg, #PB_Any, x, 0, 1, Height)
          If StripeImg
            DrawImage(ImageID(StripeImg), Width - x - 1, 0)
            FreeImage(StripeImg)
          EndIf
        Next x
        
        StopDrawing()
        
        SaveImage(NewImg, Filename$ + ".mirrored.jpg", #PB_ImagePlugin_JPEG)
        
      EndIf
      
      FreeImage(NewImg)
    EndIf
    
    FreeImage(OrgImg)
  EndIf
EndIf
It's short in code and I hope it's fast enough.
Since it uses only PB internal commands, it is crossplatform.

Bernd
Chuf
User
User
Posts: 11
Joined: Sun Jun 14, 2015 8:25 pm

Re: flip image horizontally

Post by Chuf »

Hi Bernd, this is a very fine short code, i am have testing this with a .png file by UsePNGImageDecoder()
one problem by this solution is: the picture has a black background color can you help me to fix this?
i love your short code

Best Regards
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: flip image horizontally

Post by netmaestro »

You just need two quick changes to support PNG transparency:

Code: Select all

UsePNGImageDecoder()
UsePNGImageEncoder()

Filename$ = OpenFileRequester("Choose an image", "", "PNG|*.png", 0)
If Len(Filename$)
  OrgImg = LoadImage(#PB_Any, Filename$)
  If OrgImg
    
    Width = ImageWidth(OrgImg)
    Height = ImageHeight(OrgImg)
    
    NewImg = CreateImage(#PB_Any, Width, Height, 32, #PB_Image_Transparent) ; <---------------- Changed line
    If NewImg
      
      If StartDrawing(ImageOutput(NewImg))
        
        For x = Width - 1 To 0 Step -1
          StripeImg = GrabImage(OrgImg, #PB_Any, x, 0, 1, Height)
          If StripeImg
            DrawAlphaImage(ImageID(StripeImg), Width - x - 1, 0) ; <--------------------Changed line
            FreeImage(StripeImg)
          EndIf
        Next x
        
        StopDrawing()
        SaveImage(NewImg, Filename$ + ".mirrored.png", #PB_ImagePlugin_JPEG)
        
      EndIf
      
      FreeImage(NewImg)
    EndIf
    
    FreeImage(OrgImg)
  EndIf
EndIf
BERESHEIT
infratec
Always Here
Always Here
Posts: 7587
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: flip image horizontally

Post by infratec »

Hi,

netmaestro's code should fix this, else you can specify a backgroundcolor at CreateImage().

netmaestro made a typo:
use

Code: Select all

SaveImage(NewImg, Filename$ + ".mirrored.png", #PB_ImagePlugin_PNG)
Bernd
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: flip image horizontally

Post by netmaestro »

Ha, I was checking results with the LibraryViewer so I didn't notice the oversight. One more failure to attend to detail to go with the other 117 for today! Thanks inf
BERESHEIT
Chuf
User
User
Posts: 11
Joined: Sun Jun 14, 2015 8:25 pm

Re: flip image horizontally

Post by Chuf »

many many Thanks now works fine for me
Post Reply